Skip to content

Commit

Permalink
CI: Include all modules into pytype checking
Browse files Browse the repository at this point in the history
Fix some type annotations

PiperOrigin-RevId: 688522741
  • Loading branch information
oprypin authored and copybara-github committed Nov 3, 2024
1 parent 03f2821 commit cb04097
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 37 deletions.
15 changes: 5 additions & 10 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,14 @@ build-backend = "setuptools.build_meta"

[tool.pytype]
inputs = [
"pytype/*.py",
"pytype/overlays/",
"pytype/pyc/",
"pytype/pyi/",
"pytype/pytd/",
"pytype/tools/",
"pytype/typegraph/",
"pytype/**/*.py",
"pytype_extensions/**/*.py",
]
exclude = [
"**/*_test.py",
"**/test_*.py",
"**/*_test_*.py",
"pytype/tools/merge_pyi/test_data/",
"pytype/tools/xref/testdata/",
]
"**/typeshed/",
"**/test_data/",
"**/testdata/",
]
4 changes: 3 additions & 1 deletion pytype/abstract/_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,9 @@ class FunctionPyTDClass(PyTDClass):
"""

def __init__(
self, func: "_function_base.Function", ctx: "context.Context"
self,
func: "_function_base.Function | _function_base.BoundFunction",
ctx: "context.Context",
) -> None:
super().__init__(
"typing.Callable",
Expand Down
38 changes: 24 additions & 14 deletions pytype/abstract/_function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import inspect
import itertools
import logging
from typing import Any, TYPE_CHECKING
from typing import Any, Generic, TYPE_CHECKING, TypeVar

from pytype.abstract import _base
from pytype.abstract import _classes
Expand All @@ -26,6 +26,7 @@
from pytype import datatypes # pylint: disable=g-bad-import-order,g-import-not-at-top
from pytype import matcher # pylint: disable=g-bad-import-order,g-import-not-at-top
from pytype.abstract import _interpreter_function # pylint: disable=g-bad-import-order,g-import-not-at-top
from pytype.abstract import _pytd_function # pylint: disable=g-bad-import-order,g-import-not-at-top
from pytype.pyc import opcodes # pylint: disable=g-bad-import-order,g-import-not-at-top
from pytype.typegraph import cfg # pylint: disable=g-bad-import-order,g-import-not-at-top

Expand All @@ -39,6 +40,10 @@ class Function(_instance_base.SimpleValue, types.Function):
"""

bound_class: type["BoundFunction"]
is_abstract: bool
is_classmethod: bool
is_overload: bool
is_method: bool

def __init__(self, name: str, ctx: "context.Context") -> None:
super().__init__(name, ctx)
Expand Down Expand Up @@ -82,7 +87,7 @@ def match_args(
args: function.Args,
alias_map: "datatypes.UnionFind | None" = None,
match_all_views: bool = False,
) -> "list[matcher.GoodMatch]":
) -> "list[list[tuple[_pytd_function.PyTDSignature, dict[str, cfg.Variable], matcher.GoodMatch]]]":
"""Check whether the given arguments can match the function signature."""
for a in args.posargs:
if not a.bindings:
Expand All @@ -99,7 +104,7 @@ def _match_args_sequentially(
args: function.Args,
alias_map: "datatypes.UnionFind | None",
match_all_views: bool,
) -> "list[matcher.GoodMatch]":
) -> "list[list[tuple[_pytd_function.PyTDSignature, dict[str, cfg.Variable], matcher.GoodMatch]]]":
raise NotImplementedError(self.__class__.__name__)

def __repr__(self) -> str:
Expand Down Expand Up @@ -256,13 +261,16 @@ def property_get(
return self


class BoundFunction(_base.BaseValue):
_SomeFunction = TypeVar("_SomeFunction", bound=Function)


class BoundFunction(Generic[_SomeFunction], _base.BaseValue):
"""An function type which has had an argument bound into it."""

underlying: _SomeFunction

def __init__(
self,
callself: "cfg.Variable",
underlying: "_interpreter_function.InterpreterFunction",
self, callself: "cfg.Variable", underlying: _SomeFunction
) -> None:
super().__init__(underlying.name, underlying.ctx)
self.cls = _classes.FunctionPyTDClass(self, self.ctx)
Expand Down Expand Up @@ -305,7 +313,7 @@ def argcount(self, node: "cfg.CFGNode") -> int:

@property
def signature(self) -> function.Signature:
return self.underlying.signature.drop_first_parameter()
return self.underlying.signature.drop_first_parameter() # pytype: disable=attribute-error

@property
def callself(self) -> "cfg.Variable":
Expand Down Expand Up @@ -335,7 +343,7 @@ def call(
and self.underlying.has_self_annot
)
if should_set_self_annot:
context = self.underlying.set_self_annot(self._self_annot)
context = self.underlying.set_self_annot(self._self_annot) # pytype: disable=attribute-error
else:
context = contextlib.nullcontext()
with context:
Expand All @@ -356,7 +364,7 @@ def call(
return node, ret

def get_positional_names(self) -> Sequence[str]:
return self.underlying.get_positional_names()
return self.underlying.get_positional_names() # pytype: disable=attribute-error

def has_varargs(self) -> bool:
return self.underlying.has_varargs()
Expand Down Expand Up @@ -415,7 +423,9 @@ def get_special_attribute(
return super().get_special_attribute(node, name, valself)


class BoundInterpreterFunction(BoundFunction):
class BoundInterpreterFunction(
BoundFunction["_interpreter_function.InterpreterFunction"]
):
"""The method flavor of InterpreterFunction."""

@contextlib.contextmanager
Expand Down Expand Up @@ -463,7 +473,7 @@ class ClassMethod(_base.BaseValue):
def __init__(
self,
name: str,
method: "_interpreter_function.InterpreterFunction",
method: Function,
callself: "cfg.Variable",
ctx: "context.Context",
) -> None:
Expand Down Expand Up @@ -602,7 +612,7 @@ def match_and_map_args(
node: "cfg.CFGNode",
args: function.Args,
alias_map: "datatypes.UnionFind | None" = None,
) -> "tuple[list[matcher.GoodMatch], dict[str, cfg.Variable]]":
) -> "tuple[list[list[tuple[_pytd_function.PyTDSignature, dict[str, cfg.Variable], matcher.GoodMatch]]], dict[str, cfg.Variable]]":
"""Calls match_args() and _map_args()."""
return self.match_args(node, args, alias_map), self._map_args(node, args)

Expand Down Expand Up @@ -718,7 +728,7 @@ def _match_args_sequentially(
args: function.Args,
alias_map: "datatypes.UnionFind | None",
match_all_views: bool,
) -> "list[matcher.GoodMatch]":
) -> "list[list[tuple[_pytd_function.PyTDSignature, dict[str, cfg.Variable], matcher.GoodMatch]]]":
args_to_match = []
self._check_paramspec_args(args)
for name, arg, formal in self.signature.iter_args(args):
Expand Down
7 changes: 5 additions & 2 deletions pytype/abstract/_pytd_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ def _call_with_signatures(
func: cfg.Binding,
args: function.Args,
view: datatypes.AccessTrackingDict,
signatures: list[pytd.Signature],
signatures: "list[tuple[PyTDSignature, dict[str, cfg.Variable], matcher.GoodMatch]]",
) -> tuple[cfg.CFGNode, cfg.Variable, list[function.Mutation]]:
"""Perform a function call that involves multiple signatures."""
ret_type = self._combine_multiple_returns(signatures)
Expand Down Expand Up @@ -489,7 +489,10 @@ def _call_with_signatures(
)
return node, result, mutations

def _combine_multiple_returns(self, signatures):
def _combine_multiple_returns(
self,
signatures: "list[tuple[PyTDSignature, dict[str, cfg.Variable], matcher.GoodMatch]]",
):
"""Combines multiple return types.
Args:
Expand Down
12 changes: 5 additions & 7 deletions pytype/abstract/_special_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ def build_class(
continue
if base.is_enum:
enum_base = abstract_utils.get_atomic_value(
ctx.vm.loaded_overlays["enum"].members[
"Enum"
] # pytype: disable=attribute-error
ctx.vm.loaded_overlays["enum"].members["Enum"] # pytype: disable=attribute-error
)
return enum_base.make_class(node, props)
elif base.full_name == "typing.NamedTuple":
Expand Down Expand Up @@ -101,8 +99,8 @@ def matches_class(self, c: "_classes.PyTDClass") -> bool:
return c.name in self.CLASSES

def matches_base(self, c: "_classes.PyTDClass") -> bool:
return any( # pytype: disable=attribute-error
isinstance(b, pytd.ClassType) and self.matches_class(b) for b in c.bases
return any(
isinstance(b, pytd.ClassType) and self.matches_class(b) for b in c.bases # pytype: disable=attribute-error
)

def matches_mro(self, c: "_classes.PyTDClass") -> bool:
Expand Down Expand Up @@ -132,8 +130,8 @@ def matches_class(self, c: "_classes.PyTDClass") -> bool:
return c.name in self.CLASSES

def matches_base(self, c: "_classes.PyTDClass") -> bool:
return any( # pytype: disable=attribute-error
isinstance(b, pytd.ClassType) and self.matches_class(b) for b in c.bases
return any(
isinstance(b, pytd.ClassType) and self.matches_class(b) for b in c.bases # pytype: disable=attribute-error
)

def matches_mro(self, c: "_classes.PyTDClass") -> bool:
Expand Down
6 changes: 4 additions & 2 deletions pytype/abstract/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,14 +563,16 @@ def __repr__(self) -> str:
def instantiate(
self,
node: "cfg.CFGNode",
container: _instance_base.Instance | None = None,
container: (
_instance_base.SimpleValue | abstract_utils.DummyContainer | None
) = None,
) -> "cfg.Variable":
var = self.ctx.program.NewVariable()
if container and (
not isinstance(container, _instance_base.SimpleValue)
or self.full_name in container.all_template_names
):
instance = self._INSTANCE_CLASS(self, container, self.ctx) # pylint: disable=not-callable
instance = self._INSTANCE_CLASS(self, container, self.ctx) # pylint: disable=not-callable # pytype: disable=wrong-arg-types
return instance.to_variable(node)
else:
for c in self.constraints:
Expand Down
4 changes: 3 additions & 1 deletion pytype/imports/pickle_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ def DecodeAst(data: bytes) -> serialize_ast.SerializableAst:
def LoadAst(
filename: Path, compress: bool = False, open_function=open
) -> serialize_ast.SerializableAst:
return _Load(AstDecoder, filename, compress, open_function)
return _Load(
AstDecoder, filename, compress, open_function
) # pytype: disable=bad-return-type


def DecodeBuiltins(data: bytes) -> serialize_ast.ModuleBundle:
Expand Down

0 comments on commit cb04097

Please sign in to comment.