Skip to content

Commit

Permalink
Fix GitHub CI.
Browse files Browse the repository at this point in the history
* Add msgspec dep.
* Fix lint issues.
* Massage node.py.
* Change type annotations that aren't compatible with 3.8.
  • Loading branch information
rchen152 committed Mar 4, 2024
1 parent dcd8daf commit 9c8debe
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 22 deletions.
3 changes: 2 additions & 1 deletion pytype/abstract/_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,8 @@ def __hash__(self):
for name, val in self.formal_type_parameters.items():
# The 'is not True' check is to prevent us from incorrectly caching
# the hash when val.resolved == LateAnnotation._RESOLVING.
if val.is_late_annotation() and val.resolved is not True: # pylint: disable=g-bool-id-comparison # pytype: disable=attribute-error
if (val.is_late_annotation() and
val.resolved is not True): # pylint: disable=g-bool-id-comparison # pytype: disable=attribute-error
cache = False
items.append((name, val.full_name))
hashval = hash((self.base_cls, tuple(items)))
Expand Down
12 changes: 9 additions & 3 deletions pytype/imports/pickle_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,20 @@

import gzip
import os
import sys
from typing import Iterable, Optional, Tuple, TypeVar, Union

import msgspec

from pytype.pytd import pytd
from pytype.pytd import serialize_ast


Path = Union[str, os.PathLike[str]]
# In Python 3.8 and below, os.PathLike isn't subscriptable.
if sys.version_info[:2] >= (3, 9):
_StrPathLike = os.PathLike[str]
else:
_StrPathLike = os.PathLike
Path = Union[str, _StrPathLike]


class LoadPickleError(Exception):
Expand All @@ -60,7 +65,8 @@ def __init__(self, filename: Path):


def _Load(
dec: _Dec[_DecT], filename: Path, compress: bool = False, open_function=open
dec: "_Dec[_DecT]", filename: Path, compress: bool = False,
open_function=open,
) -> _DecT:
"""Loads a serialized file.
Expand Down
4 changes: 2 additions & 2 deletions pytype/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ def check_py(src, options=None, loader=None):

def generate_pyi_ast(
src: str,
options: config.Options | None = None,
loader: load_pytd.Loader | None = None,
options: Optional[config.Options] = None,
loader: Optional[load_pytd.Loader] = None,
) -> analyze.Analysis:
"""Run the inferencer on a string of source code, producing output.
Expand Down
13 changes: 11 additions & 2 deletions pytype/pytd/parse/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,20 @@
For examples of visitors, see pytd/visitors.py
"""

from typing import Any, ClassVar, Type, TYPE_CHECKING

import msgspec

from pytype import metrics

if TYPE_CHECKING:
_Struct: Type[Any]
else:
_Struct = msgspec.Struct


class Node(
msgspec.Struct,
_Struct,
frozen=True,
tag=True,
tag_field="_struct_type",
Expand All @@ -28,7 +35,9 @@ class Node(
):
"""Base Node class."""

name: str = ""
# We pretend that `name` is a ClassVar so that msgspec treats it as a struct
# field only when it is defined in a subclass.
name: ClassVar[str] = ""

def __iter__(self):
for name in self.__struct_fields__:
Expand Down
26 changes: 13 additions & 13 deletions pytype/pytd/pytd.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def Get(self, name):
def __contains__(self, name):
return bool(self.Get(name))

def IterChildren(self) -> Generator[tuple[str, Any | None], None, None]:
def IterChildren(self) -> Generator[Tuple[str, Any | None], None, None]:
for name, child in super().IterChildren():
if name == '_name2item':
continue
Expand All @@ -113,7 +113,7 @@ class Constant(Node):
# (bytes is excluded because it serializes the same as str.)
# We can't use just `value: Any` because msgspec isn't able to decode
# AnythingType in that case, since it's indistinguishable from a dict.
value: Optional[Union[AnythingType, int, str, bool, tuple[str, ...]]] = None
value: Optional[Union[AnythingType, int, str, bool, Tuple[str, ...]]] = None


class Alias(Node):
Expand Down Expand Up @@ -150,13 +150,13 @@ class Class(Node):
"""
name: str
keywords: Tuple[Tuple[str, TypeU], ...]
bases: Tuple[Union['Class', TypeU], ...]
methods: Tuple['Function', ...]
bases: Tuple[Union[Class, TypeU], ...]
methods: Tuple[Function, ...]
constants: Tuple[Constant, ...]
classes: Tuple['Class', ...]
classes: Tuple[Class, ...]
decorators: Tuple[Alias, ...]
slots: Optional[Tuple[str, ...]]
template: Tuple['TemplateItem', ...]
template: Tuple[TemplateItem, ...]
# _name2item is the lookup cache. It should not be treated as a child or used
# in equality or hash operations.
_name2item: Dict[str, Any] = {}
Expand Down Expand Up @@ -204,7 +204,7 @@ def __hash__(self):
nohash = self.Replace(_name2item=None)
return super(Class, nohash).__hash__()

def IterChildren(self) -> Generator[tuple[str, Any | None], None, None]:
def IterChildren(self) -> Generator[Tuple[str, Any | None], None, None]:
for name, child in super().IterChildren():
if name == '_name2item':
continue
Expand Down Expand Up @@ -252,7 +252,7 @@ class Function(Node):
flags: A bitfield of flags like is_abstract
"""
name: str
signatures: Tuple['Signature', ...]
signatures: Tuple[Signature, ...]
kind: MethodKind
flags: MethodFlag = MethodFlag.NONE
decorators: Tuple[Alias, ...] = ()
Expand Down Expand Up @@ -290,12 +290,12 @@ class Signature(Node):
exceptions: List of exceptions for this function definition.
template: names for bindings for bounded types in params/return_type
"""
params: Tuple['Parameter', ...]
starargs: Optional['Parameter']
starstarargs: Optional['Parameter']
params: Tuple[Parameter, ...]
starargs: Optional[Parameter]
starstarargs: Optional[Parameter]
return_type: TypeU
exceptions: Tuple[TypeU, ...]
template: Tuple['TemplateItem', ...]
template: Tuple[TemplateItem, ...]

@property
def has_optional(self):
Expand Down Expand Up @@ -462,7 +462,7 @@ class ClassType(Type, frozen=False, eq=False):
# treated as if it doesn't exist.
cls: Optional[Any] = None

def IterChildren(self) -> Generator[tuple[str, Any | None], None, None]:
def IterChildren(self) -> Generator[Tuple[str, Any | None], None, None]:
# It is very important that visitors do not follow the cls pointer. To avoid
# this, we claim that `name` is the only child.
yield 'name', self.name
Expand Down
2 changes: 1 addition & 1 deletion pytype/pytd/pytd_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def test_class_type_equality(self):

def test_iter(self):
n = pytd.NamedType("int")
fields = [f for f in n]
fields = list(n)
self.assertEqual(fields, [n.name])

def test_union_type_eq(self):
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ importlab>=0.8
immutabledict>=4.1.0
jinja2>=3.1.2
libcst>=1.0.1
msgspec>=0.18.6
networkx<3.2
ninja>=1.10.0.post2
pybind11>=2.10.1
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ install_requires =
immutabledict>=4.1.0
jinja2>=3.1.2
libcst>=1.0.1
msgspec>=0.18.6
networkx<3.2
ninja>=1.10.0.post2
pycnite>=2023.10.11
Expand Down

0 comments on commit 9c8debe

Please sign in to comment.