Skip to content

stubtest: be safer with inspect.signature #10884

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 27, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 27 additions & 21 deletions mypy/stubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,23 +646,16 @@ def verify_funcitem(
yield Error(object_path, "is not present at runtime", stub, runtime)
return

if (
not isinstance(runtime, (types.FunctionType, types.BuiltinFunctionType))
and not isinstance(runtime, (types.MethodType, types.BuiltinMethodType))
and not inspect.ismethoddescriptor(runtime)
):
if not is_probably_a_function(runtime):
yield Error(object_path, "is not a function", stub, runtime)
if not callable(runtime):
return

for message in _verify_static_class_methods(stub, runtime, object_path):
yield Error(object_path, "is inconsistent, " + message, stub, runtime)

try:
signature = inspect.signature(runtime)
except (ValueError, RuntimeError):
# inspect.signature throws sometimes
# catch RuntimeError because of https://bugs.python.org/issue39504
signature = safe_inspect_signature(runtime)
if not signature:
return

stub_sig = Signature.from_funcitem(stub)
Expand Down Expand Up @@ -730,21 +723,16 @@ def verify_overloadedfuncdef(
# We get here in cases of overloads from property.setter
return

if (
not isinstance(runtime, (types.FunctionType, types.BuiltinFunctionType))
and not isinstance(runtime, (types.MethodType, types.BuiltinMethodType))
and not inspect.ismethoddescriptor(runtime)
):
if not is_probably_a_function(runtime):
yield Error(object_path, "is not a function", stub, runtime)
if not callable(runtime):
return

for message in _verify_static_class_methods(stub, runtime, object_path):
yield Error(object_path, "is inconsistent, " + message, stub, runtime)

try:
signature = inspect.signature(runtime)
except ValueError:
signature = safe_inspect_signature(runtime)
if not signature:
return

stub_sig = Signature.from_overloadedfuncdef(stub)
Expand Down Expand Up @@ -881,6 +869,24 @@ def is_dunder(name: str, exclude_special: bool = False) -> bool:
return name.startswith("__") and name.endswith("__")


def is_probably_a_function(runtime: Any) -> bool:
return (
isinstance(runtime, (types.FunctionType, types.BuiltinFunctionType))
or isinstance(runtime, (types.MethodType, types.BuiltinMethodType))
or (inspect.ismethoddescriptor(runtime) and callable(runtime))
)


def safe_inspect_signature(runtime: Any) -> Optional[inspect.Signature]:
try:
return inspect.signature(runtime)
except (ValueError, RuntimeError, TypeError):
# inspect.signature throws sometimes
# catch RuntimeError because of https://bugs.python.org/issue39504
# catch TypeError because of https://github.com/python/typeshed/pull/5762
return None


def is_subtype_helper(left: mypy.types.Type, right: mypy.types.Type) -> bool:
"""Checks whether ``left`` is a subtype of ``right``."""
left = mypy.types.get_proper_type(left)
Expand Down Expand Up @@ -923,8 +929,8 @@ def anytype() -> mypy.types.AnyType:
type_info = builtins.names["function"].node
assert isinstance(type_info, nodes.TypeInfo)
fallback = mypy.types.Instance(type_info, [anytype()])
try:
signature = inspect.signature(runtime)
signature = safe_inspect_signature(runtime)
if signature:
arg_types = []
arg_kinds = []
arg_names = []
Expand All @@ -946,7 +952,7 @@ def anytype() -> mypy.types.AnyType:
arg_kinds.append(nodes.ARG_STAR2)
else:
raise AssertionError
except ValueError:
else:
arg_types = [anytype(), anytype()]
arg_kinds = [nodes.ARG_STAR, nodes.ARG_STAR2]
arg_names = [None, None]
Expand Down