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

New semantic analyzer: Name 'MyX' already defined (by an import) #7172

Closed
gregbedwell opened this issue Jul 8, 2019 · 7 comments
Closed

Comments

@gregbedwell
Copy link

gregbedwell commented Jul 8, 2019

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

  • Please insert below the code you are checking with mypy,
    reproducer.zip

I'm somewhat new to mypy so I'm sorry if this is a user error. If nothing else, the error message produced doesn't seem particularly helpful in narrowing down what to do fix it (or what section I might look in in the documentation). This is an error from one of my projects that builds cleanly with mypy --strict as well as pylint, pyflakes, etc. (with a few suppressions where the different tools take different views over things). I've reduced it and suitably anonymized it. There is a kind of circular import taking place where I'm importing the MyX class in MyBase.py and the MyBase class from MyX.py but it's never previously caused any issues.

  • What is the actual behavior/output?
(py3.6) e:\tmp\mypy-issue>python --version
Python 3.6.8

(py3.6) e:\tmp\mypy-issue>mypy --version
mypy 0.711

(py3.6) e:\tmp\mypy-issue>mypy --strict .

(py3.6) e:\tmp\mypy-issue>mypy --strict --new-semantic-analyzer .
outer\inner\MyDerived2.py:1: error: Name 'MyX' already defined (by an import)
outer\inner\MyDerived2.py:8: error: Invalid type "outer.MyX"

If I remove the explicit import of MyX from MyDerived2.py then it obviously fails with:

(py3.6) e:\tmp\mypy-issue>mypy --strict --new-semantic-analyzer .
outer\inner\MyDerived2.py:8: error: Name 'MyX' is not defined
  • What is the behavior/output you expect?
    No error with the new semantic analyzer (same as with the old semantic analyzer).
@Cito
Copy link

Cito commented Jul 13, 2019

This looks a bit similar to my issue #7172.

It seems to happen sometimes when an object is called like its containing module.

@gregbedwell
Copy link
Author

I'm guessing you meant issue #7203 ?

@Cito
Copy link

Cito commented Jul 13, 2019

Sorry, yes, #7203.

@msullivan
Copy link
Collaborator

With 0.720 we instead get a error: Module "outer.inner.MyBase" is not valid as a type

@msullivan
Copy link
Collaborator

A minimized version:

[case testCycleRenames]
# flags: --new-semantic-analyzer
import MyX

[file MyX.py]
from inner import MyBase

class MyX:
    x: MyBase = None

[file inner/__init__.py]
from inner.MyBase import MyBase as MyBase

[file inner/MyBase.py]
from MyX import MyX

class MyBase:
    pass

[file inner/MyDerived1.py]
from inner.MyBase import MyBase

class MyDerived1(MyBase):
    pass

This minimized code I think will actually fail at runtime, though, executing from MyX import MyX
The original code is similarly dodgy, as one of the imports will actually get the submodule instead of the name.

Giving the classes the same name as modules is a worrisome practice, I think?

@ilevkivskyi
Copy link
Member

@msullivan
Taking into account minimized repros for both issues I wonder if this is a duplicate (or other way around) of #7203 (module function with the same name as containing module -> class with the same name as containing module)?

@ilevkivskyi
Copy link
Member

Closing as a duplicate of #7203

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

4 participants