Skip to content

mypy incorrectly resolves names inside decorated methods #8161

@beckjake

Description

@beckjake
  • Are you reporting a bug, or opening a feature request?

A bug

  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.
from typing import Type

class A:
    pass

class B:
    @staticmethod
    def A() -> Type[A]:
        return A

assert A is B.A()
  • What is the actual behavior/output?
$ mypy mypy_fail.py
mypy_fail.py:8: error: Function "mypy_fail.B.A" is not valid as a type
mypy_fail.py:8: note: Perhaps you need "Callable[...]" or a callback protocol?
Found 1 error in 1 file (checked 1 source file)
  • What is the behavior/output you expect?

I expect this to be accepted - it works fine in python

Probably of interest: regular methods work like I expect. classmethod and property complain the same way, so I assume this has to do with decorators. For functions, I can't reproduce syntax like this that's also valid python (I get UnboundLocalErrors), so that's as far as I got with debugging.

  • What are the versions of mypy and Python you are using?
    Do you see the same issue after installing mypy from Git master?

mypy 0.750 and python 3.6/3.8. I also get it on git master.

  • What are the mypy flags you are using? (For example --strict-optional)

No flags required

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions