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

stubgen: wrong import statements for base module when importing submodule #7006

Closed
patrick-kidger opened this issue Jun 17, 2019 · 2 comments · Fixed by #15624
Closed

stubgen: wrong import statements for base module when importing submodule #7006

patrick-kidger opened this issue Jun 17, 2019 · 2 comments · Fixed by #15624
Labels

Comments

@patrick-kidger
Copy link

There's a bug with the import statements generated in .pyi files.

Consider the folder structure:

somefolder/
    test.py
    foo_mod/
        __init__.py
        bar_mod.py

with file contents:

# test.py
import foo_mod
import foo_mod.bar_mod as bar_mod

class baz(foo_mod.foo, bar_mod.bar):
    pass
# foo_mod/__init__.py
class foo:
    pass
# foo_mod/bar_mod.py
class bar:
    pass

Then running stubgen test.py whilst in somefolder generates an out/test.pyi file with contents:

# out/test.pyi
# Stubs for test (Python 3)
#
# NOTE: This dynamically typed stub was automatically generated by stubgen.

import foo_mod.bar_mod as bar_mod
import foo_mod.bar_mod

class baz(foo_mod.foo, bar_mod.bar): ...

Note how it says foo_mod.bar_mod, rather than just foo_mod as expected.

This issue generalises to subsubpackages as well (and presumably further). That is,

# test.py
import foo_mod
import foo_mod.bar_mod as bar_mod
import foo_mod.bar_mod.quux_mod as quux_mod
...

generates

# out/test.pyi
import foo_mod.bar_mod.quux_mod as bar_mod
import foo_mod.bar_mod.quux_mod
import foo_mod.bar_mod.quux_mod as quux_mod
...

Tested on Windows and Unix, with both mypy version 0.701 and the latest on the GitHub master branch, with Python 3.6.6. (At least, that's the version of Python which is bound to python on the command line, so presumably that's what stubgen is using?)

Possibly related? #6831

@patrick-kidger
Copy link
Author

patrick-kidger commented Jun 17, 2019

It seems that it's whichever import statement is last that is used. e.g.

# test.py
import foo_mod.bar_mod as bar_mod
import foo_mod
...

generates

# out/test.pyi
import foo_mod as bar_mod
import foo_mod
...

@ilevkivskyi
Copy link
Member

I think this is a separate issue from #6831.

@ilevkivskyi ilevkivskyi added bug mypy got something wrong priority-1-normal labels Jun 17, 2019
hamdanal added a commit to hamdanal/mypy that referenced this issue Jul 8, 2023
* Fix handling of nested imports
  Instead of assuming that a name is imported from a top level package,
  look in the imports for this name starting from the parent submodule
  up until the import is found
* Fix "from imports" getting rexported unnecessarily
* Fix import sorting when having import aliases

Fixes python#13661
Fixes python#7006
hauntsaninja pushed a commit that referenced this issue Sep 25, 2023
* Fix handling of nested imports.
Instead of assuming that a name is imported from a top level package,
look in the imports for this name starting from the parent submodule up
until the import is found
* Fix "from imports" getting reexported unnecessarily
* Fix import sorting when having import aliases

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

Successfully merging a pull request may close this issue.

3 participants