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 6, 2024
1 parent 6433bce commit 3659140
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 102 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: str
) -> "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
10 changes: 6 additions & 4 deletions pytype/abstract/abstract_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -930,15 +930,17 @@ def check_against_mro(
return None if ambiguous else False


def maybe_unwrap_decorated_function(func: "_classes.FunctionPyTDClass"):
def maybe_unwrap_decorated_function(func: "_function_base.Function"):
# Some decorators, like special_builtins.PropertyInstance, have a
# 'func' pointer to the decorated function. Note that we check for .data to
# make sure 'func' is a Variable.
try:
func.func.data
# TODO: b/350643999 - The type here passed in is indeed correct but
# the intent of this functions is quite unclear. Figure out and fix it.
func.func.data # pytype: disable=attribute-error
except AttributeError:
return None
return func.func
return func.func # pytype: disable=attribute-error


def unwrap_final(val: "_base.BaseValue") -> "_base.BaseValue":
Expand Down Expand Up @@ -1020,7 +1022,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 3659140

Please sign in to comment.