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

Cannot import attribute from module of the same name within __init__.py #1776

Closed
Diggsey opened this issue Jun 30, 2016 · 4 comments
Closed
Labels
bug mypy got something wrong feature priority-2-low

Comments

@Diggsey
Copy link

Diggsey commented Jun 30, 2016

Example:

# __init__.py
from .app import app

mypy will complain about app being defined twice.

@gvanrossum gvanrossum added the bug mypy got something wrong label Jun 30, 2016
@gvanrossum
Copy link
Member

Yeah, I can confirm this, with app.py containing

class App:
    pass
app = App()

I'm not sure how easy this is to fix in mypy; I can suggest as a workaround to rename one of the two things named app.

NOTE: Even though this works in Python, it is definitely "skating on thin ice": normally when there's a package P with a submodule M, P's namespace contains a name M referencing the module M. What you're doing here is overwriting that M with another thing also named M and imported from P.M (in your case M == app).

@Diggsey
Copy link
Author

Diggsey commented Jun 30, 2016

If it was purely my own project, I'd avoid it as bad practice. However, this technique seems to be used widely throughout many python packages, including sqlalchemy.

@gvanrossum gvanrossum added this to the Undetermined priority milestone Jul 7, 2016
@gvanrossum
Copy link
Member

Implementing this will be somewhat tricky. :-(

@gvanrossum gvanrossum removed this from the Undetermined priority milestone Mar 29, 2017
@ilevkivskyi
Copy link
Member

It looks like this is now fixed. I can't reproduce the error.

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

No branches or pull requests

3 participants