Skip to content
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

Infer callable signatures for objects with a __getattr__ method #485

Merged
merged 1 commit into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Infer callable signatures for objects with a
`__getattr__` method (#485)
- Do not treat attributes that raise an exception on access
as nonexistent (#481)
- Improve detection of unhashable dict keys and set members (#469)
Expand Down
16 changes: 15 additions & 1 deletion pyanalyze/arg_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from .safe import (
all_of_type,
is_newtype,
safe_equals,
safe_hasattr,
safe_issubclass,
is_typing_name,
Expand Down Expand Up @@ -58,6 +59,7 @@
from collections.abc import Awaitable
import contextlib
from dataclasses import dataclass, replace
import enum
import qcore
import inspect
import sys
Expand All @@ -81,6 +83,8 @@
# types.MethodWrapperType in 3.7+
MethodWrapperType = type(object().__str__)

_ENUM_CALL = enum.Enum.__call__


@used # exposed as an API
@contextlib.contextmanager
Expand Down Expand Up @@ -572,7 +576,7 @@ def _uncached_get_argspec(
if evaluator_sig is not None:
return evaluator_sig

if isinstance(obj, tuple) or hasattr(obj, "__getattr__"):
if isinstance(obj, tuple):
return None # lost cause

# Cythonized methods, e.g. fn.asynq
Expand Down Expand Up @@ -618,6 +622,16 @@ def _uncached_get_argspec(
original_fn, impl, is_asynq, in_overload_resolution
)

# Special case for EnumMeta.__call__. Ideally this should be generalized.
if (
safe_isinstance(obj, type)
and safe_issubclass(obj, enum.Enum)
and safe_equals(obj.__call__, _ENUM_CALL)
):
return self._cached_get_argspec(
_ENUM_CALL, impl, is_asynq, in_overload_resolution
)

allow_call = FunctionsSafeToCall.contains(obj, self.options) or (
safe_isinstance(obj, type) and safe_issubclass(obj, self.safe_bases)
)
Expand Down
29 changes: 29 additions & 0 deletions pyanalyze/test_enum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# static analysis: ignore
from .implementation import assert_is_value
from .value import SubclassValue, TypedValue
from .test_name_check_visitor import TestNameCheckVisitorBase
from .test_node_visitor import assert_passes


class TestEnum(TestNameCheckVisitorBase):
@assert_passes()
def test_functional(self):
from enum import Enum

def capybara():
X = Enum("X", ["a", "b", "c"])
assert_is_value(X, SubclassValue(TypedValue(Enum)))

@assert_passes()
def test_call(self):
from enum import Enum

class X(Enum):
a = 1
b = 2

def capybara():
assert_is_value(X(1), TypedValue(X))
# This should be an error, but the typeshed
# stubs are too lenient.
assert_is_value(X(None), TypedValue(X))