Skip to content

Forward reference to decorated class method has type Any #4485

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

Closed
JukkaL opened this issue Jan 18, 2018 · 0 comments · Fixed by #4486
Closed

Forward reference to decorated class method has type Any #4485

JukkaL opened this issue Jan 18, 2018 · 0 comments · Fixed by #4486

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Jan 18, 2018

In the program below, the type inferred for cls.g in A.f is Any, even though it should be def (x: builtins.str):

from typing import TypeVar, Callable

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

class A:
    @classmethod
    def f(cls) -> None:
        reveal_type(cls.g)  # Any  <-- unexpected

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

    @classmethod
    def h(cls) -> None:
        reveal_type(cls.g)  # def (x: builtins.str)  <-- ok

This works correctly when the decorated method is accessed using an instance type (such as self).

In fine-grained incremental mode the types can be more precise, which can result in additional errors in fine-grained incremental mode.

@JukkaL JukkaL self-assigned this Jan 18, 2018
JukkaL added a commit that referenced this issue Jan 18, 2018
The case wasn't properly handled before when accessing through a type
object -- the type of the forward reference defaulted to `Any`.

The fix doesn't work at module top level since module top levels can't
be deferred, but the reference would generally fail at runtime anyway.

Fixes #4485.
JukkaL added a commit that referenced this issue Feb 19, 2018
The case wasn't properly handled before when accessing through a type
object -- the type of the forward reference defaulted to `Any`.

The fix doesn't work at module top level since module top levels can't
be deferred, but the reference would generally fail at runtime anyway.

Fixes #4485.
yedpodtrzitko pushed a commit to kiwicom/mypy that referenced this issue Mar 15, 2018
The case wasn't properly handled before when accessing through a type
object -- the type of the forward reference defaulted to `Any`.

The fix doesn't work at module top level since module top levels can't
be deferred, but the reference would generally fail at runtime anyway.

Fixes python#4485.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant