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

name.py used as module instead of name module directory #7711

Open
mortoray opened this issue Oct 14, 2019 · 6 comments
Open

name.py used as module instead of name module directory #7711

mortoray opened this issue Oct 14, 2019 · 6 comments
Labels
bug mypy got something wrong priority-2-low

Comments

@mortoray
Copy link

I have a module named mdl with the below __init__.py (The contents don't actually matter).

from .document import load_document

Then I have a file called mdl.py:

import mdl
doc = mdl.load_document( "happy" )

mypy produces the error:

mdl.py:2: error: Module has no attribute "load_document"

If I rename the file to anything_else.py this error doesn't happen.

It appears as though mypy is resolving the mdl import to the file mdl.py instead of the mdl module directory. Since the Python interpreter resolve the names as I intended, I'd assume mypy would as well.

@ilevkivskyi
Copy link
Member

Sorry, but I am a bit lost. Could you please show the fill directory tree, content of the files, and the exact command you use to run mypy?

In any case, the fact that this work at runtime is not a justification, since x: int = 'no' also works at runtime. Sometimes, imports may resolve at runtime the way you want them because of execution order. Mypy always resolves imports statically, and can't support some patterns in principle.

@mortoray
Copy link
Author

mortoray commented Oct 14, 2019

The directory structure (simplified from a larger project).

mdl.py
mdl/__init__.py
mdl/document.py

Inside mdl.py the import mdl seems to resolve to mdl.py and not the mdl module directory (the runtime resolves to the module directory).

@mortoray
Copy link
Author

mortoray commented Oct 14, 2019

I invoke:

python -m mypy mdl.py

Or with *.py for multiple files.

mypy 0.730

@ilevkivskyi
Copy link
Member

OK, I see. IIUC you want to have both mdl.py and mdl/__init__.py in the same mypy build, but this is impossible and this is highly unlikely to change. Mypy keeps a 1-to-1 module to file mapping for every build. So you can't have two files mdl.py and mdl/__init__.py in the same build because they both correspond to module mdl.

The bug here is that mypy is silent about the duplicate module situation (unlike if you directly pass mypy mdl.py mdl/__init__.py), this is however low priority.

@ilevkivskyi ilevkivskyi added bug mypy got something wrong priority-2-low labels Oct 14, 2019
@mortoray
Copy link
Author

Is this a limitation of Python itself? I can understand why mypy behaves this way, but wondering if whether it's also an undefined situation in the Python runtime and I'm getting lucky that it works.

@ilevkivskyi
Copy link
Member

I can understand why mypy behaves this way, but wondering if whether it's also an undefined situation in the Python runtime and I'm getting lucky that it works.

This is mypy-specific, but in general I personally consider this a dangerous style since it can cause surprises during refactoring (similar to why I think absolute imports are better than relative ones).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong priority-2-low
Projects
None yet
Development

No branches or pull requests

2 participants