Skip to content

Commit

Permalink
Add type annotations to abstract/function.py.
Browse files Browse the repository at this point in the history
Also fix all other type annotations that were wrong, or add suppressions to where pytype is wrong or cannot figure out the types due to limitations.

PiperOrigin-RevId: 690586637
  • Loading branch information
h-joo authored and copybara-github committed Nov 4, 2024
1 parent 502da7d commit 5a7f76c
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 99 deletions.
2 changes: 1 addition & 1 deletion pytype/abstract/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def get_instance_type_parameter(
return self.ctx.new_unsolvable(node)

def get_formal_type_parameter(
self, t # TODO: b/350643999 - Figure out the type of 't'.
self, t: "BaseValue"
) -> "BaseValue":
"""Get the class's type for the type parameter.
Expand Down
2 changes: 1 addition & 1 deletion pytype/abstract/_function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ def _check_paramspec_args(self, args: function.Args) -> None:
for name, _, formal in self.signature.iter_args(args):
if not _isinstance(formal, "ParameterizedClass"):
continue
params = formal.get_formal_type_parameters()
params = formal.get_formal_type_parameters() # pytype: disable=attribute-error
if name == self.signature.varargs_name:
for param in params.values():
if _isinstance(param, "ParamSpecArgs"):
Expand Down
12 changes: 6 additions & 6 deletions pytype/abstract/_interpreter_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def __init__(
defaults,
kw_defaults,
closure,
annotations: "dict[str, _base.BaseValue]",
annotations: dict[str, pytd.Type],
overloads,
ctx: "context.Context",
) -> None:
Expand Down Expand Up @@ -328,7 +328,7 @@ def _check_signature(self) -> None:
)

def _build_signature(
self, name: str, annotations: "dict[str, _base.BaseValue]"
self, name: str, annotations: dict[str, pytd.Type]
) -> function.Signature:
"""Build a function.Signature object representing this function."""
vararg_name = None
Expand Down Expand Up @@ -564,7 +564,7 @@ def _paramspec_signature(
self,
callable_type: _classes.ParameterizedClass,
substs: "list[matcher.GoodMatch]",
) -> str | None:
) -> function.Signature | None:
# Unpack the paramspec substitution we have created in the matcher.
rhs = callable_type.formal_type_parameters[0]
if _isinstance(rhs, "Concatenate"):
Expand Down Expand Up @@ -593,15 +593,15 @@ def _handle_paramspec(
if not sig.has_return_annotation:
return
retval = sig.annotations["return"]
if not (_isinstance(retval, "CallableClass") and retval.has_paramspec()):
if not (_isinstance(retval, "CallableClass") and retval.has_paramspec()): # pytype: disable=attribute-error
return
ret_sig = self._paramspec_signature(retval, substs)
if ret_sig:
ret_annot = self.ctx.pytd_convert.signature_to_callable(ret_sig)
annotations["return"] = ret_annot
for name, _, annot in sig.iter_args(callargs):
if _isinstance(annot, "CallableClass") and annot.has_paramspec():
param_sig = self._paramspec_signature(annot, substs)
if _isinstance(annot, "CallableClass") and annot.has_paramspec(): # pytype: disable=attribute-error
param_sig = self._paramspec_signature(annot, substs) # pytype: disable=wrong-arg-types
if param_sig:
param_annot = self.ctx.pytd_convert.signature_to_callable(param_sig)
annotations[name] = param_annot
Expand Down
2 changes: 1 addition & 1 deletion pytype/abstract/abstract_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,7 @@ def get_generic_type(

def with_empty_substitutions(
subst: datatypes.AliasingDict[str, cfg.Variable],
pytd_type: pytd.Signature,
pytd_type: "_base.BaseValue",
node: cfg.CFGNode,
ctx: "context.Context",
) -> datatypes.AliasingDict[str, cfg.Variable]:
Expand Down
Loading

0 comments on commit 5a7f76c

Please sign in to comment.