-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Should __init__.py imports be treated as exports with --no-implicit-reexport behaviour? #10198
Comments
I personally believe here mypy is in the right. And mypy should fail as it's doing now. However, the project might want to opt-in to an implicit re-export behavior if they don't want to use an explicit export instead. This setting should be done though by the project and not the downstream users though. Perhaps To put it in scope why mypy (rightly) complains here, consider the following example: from typing import List
def generate_list() -> List[int]:
return [1] This module will provide both Historically the |
What is bad about using |
Some people find it odd that you have to:
In practice means making sure your |
I wonder if it would make sense for __all__ = list(locals()) as a way to disable |
Since Pallets ended up reintroducing this conversation, just wanted to chime in that after looking at the issue more we agree with @gaborbernat that mypy is right here, so we'll make bugfix releases with explicit exports in the form Either method is a bit noisy to me, so I think we could still brainstorm how this could be improved to make it easier / more natural to maintain. |
I don't think
|
I'd imagine we should enquire the python core developers about this 🤔 so probably someone should open a topic on discuss.python.org |
It's not limited to defining them twice, it's more than that. Consider I have a file I could live with having explicit exports be mentioned in The use of strings instead of symbols in |
At this point I'm kind of wishing we had an explicit |
I wish star imports would re-export. Consider the following:
While star imports are a bit controversial in general, I think re-exporting names is definitely a common pattern for them, and we use it a lot in typeshed. It would be nice if it would also work outside typeshed. |
Thinking about how it's done in Pallets, if we ever use an import in the module, then it's not intended for export. The only time we want imports to be exported is if they're not used in the module, which happens to only be done in Perhaps mypy could use this as a heuristic. We'd still be using flake8 to detect unused imports (or ignore them in |
#10826 is similar to this, ie. I think @erictraut's comment could be very useful in this context as well: #10826 (comment) But summarizing, there is a proposal for the indentifiable |
The odd `name as name` style is required for mypy to recognise the names, see python/mypy#10198.
I'm uncertain whether
--no-implicit-reexport
has a "correct" behaviour when it comes to__init.py__
files.If I have this
__init__.py
file:My intent is to export
MyName
from the module. Currently this does not work however, and I need to do either:or
Both of which are redundant.
I saw this issue on stub-generation that seems to imply that imports in an init (or otherwise unused imports) should be considered exports.
Shouldn't
--no-implicit-reexport
treat init files as doing exports for the module without the code having to use the redundant...as...
or__all__
form?The text was updated successfully, but these errors were encountered: