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

Incorrect handling of methods whose name shadows an existing name #4146

Open
elprans opened this issue Oct 20, 2017 · 2 comments
Open

Incorrect handling of methods whose name shadows an existing name #4146

elprans opened this issue Oct 20, 2017 · 2 comments

Comments

@elprans
Copy link
Contributor

elprans commented Oct 20, 2017

Given the following module structure:

top
|-- __init__.py
`-- bar.py

And the following module contents:

  • top/__init__.py:
from . import bar

class Foo:
    def bar(arg: bar.Spam):
        pass
  • top/bar.py:
class Spam:
    pass

mypy -m top fails:

top/__init__.py:4: error: Invalid type "top.Foo.bar"

This is because it prematurely assumes that bar is a function, not a module.
Interestingly, regular functions do not exhibit this behaviour. The following works fine:

from . import bar

def bar(arg: bar.Spam):
    pass
@ezyang
Copy link

ezyang commented Jan 17, 2019

An even simpler repro is:

class foo: ...

class bar:
    foo: foo = ...

@ezyang
Copy link

ezyang commented Jan 17, 2019

A simple workaround is to just give the type a different name and refer to it that way:

class foo: ...
_foo = foo # NB: workaround https://github.com/python/mypy/issues/4146

class bar:
    foo: _foo = ...

henribru added a commit to henribru/pytorch that referenced this issue Oct 24, 2019
I suspect that python/mypy#4146 could
cause an issue with the layout property.
henribru added a commit to henribru/pytorch that referenced this issue Oct 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants