Skip to content

Fix type of forward reference to a decorated class method #4486

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 4 commits into from
Feb 19, 2018
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
8 changes: 6 additions & 2 deletions mypy/checkmember.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,12 @@ def analyze_class_attribute_access(itype: Instance,
return builtin_type('types.ModuleType')

if is_decorated:
# TODO: Return type of decorated function. This is quick hack to work around #998.
return AnyType(TypeOfAny.special_form)
assert isinstance(node.node, Decorator)
if node.node.type:
return node.node.type
else:
not_ready_callback(name, context)
return AnyType(TypeOfAny.from_error)
else:
return function_type(cast(FuncBase, node.node), builtin_type('builtins.function'))

Expand Down
25 changes: 25 additions & 0 deletions test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -1786,6 +1786,31 @@ y = 0
tmp/m.py:2: error: "int" not callable
main:3: error: "int" not callable

[case testForwardReferenceToDecoratedClassMethod]
from typing import TypeVar, Callable

T = TypeVar('T')
def dec() -> Callable[[T], T]: pass

A.g # E: Cannot determine type of 'g'

class A:
@classmethod
def f(cls) -> None:
reveal_type(cls.g) # E: Revealed type is 'def (x: builtins.str)'

@classmethod
@dec()
def g(cls, x: str) -> None:
pass

@classmethod
def h(cls) -> None:
reveal_type(cls.g) # E: Revealed type is 'def (x: builtins.str)'

reveal_type(A.g) # E: Revealed type is 'def (x: builtins.str)'
[builtins fixtures/classmethod.pyi]


-- Tests for special cases of unification
-- --------------------------------------
Expand Down