-
-
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
Make stubgen re-export correctly imported names when module has no __all__
#3981
Comments
Following up the discussion that came from #3169 (comment) and suggests (a), from checking a couple of simple examples (even the trivial examples in stubgen tests), I see that generated stubs will end up with no required imports. Most of the imports are normally not intended for reexport except on some high-level package modules (which are typically My inclination towards (b) is that it's easier to add a few reexports when required. Of course, any scenario can be fixed by adding an adequate |
I also favor option (b). The rule about |
Only (b) makes sense here.
…On Fri, Sep 22, 2017 at 8:16 PM, Jelle Zijlstra ***@***.***> wrote:
I also favor option (b). The rule about import as in PEP 484 is about
stub files, not implementation files, so I don't think it's relevant here.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#3981 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ACwrMglMSLIlLPnDk26tGCbfxH4YRCD0ks5slHgrgaJpZM4PhDso>
.
--
--Guido van Rossum (python.org/~guido)
|
@JelleZijlstra # lib/utils.py
class PublicClass:
...
# lib/__init__.py
from .utils import PublicClass
# main.py
from lib import PublicClass It works perfectly and passes mypy. But after running |
Sure, lots of stubgen-generated code needs manual fixes later. I just think (b) is usually closer to being correct than (a) is. Perhaps we can make an additional exception for relative imports in files named |
The point is that (a) just makes stubs a bit noisier (but they are not intended to be read often anyway), while (b) causes actual false positives, and what is worse, these false positives will be discovered not by a library maintainer or whoever produces stubs, but by users of mypy/typeshed. |
I wonder if there needs to be a special case for `__init__.py` when it
imports things from submodules of the same package.
I really don't want stubs containing `import os`, `import sys`, `import re`
etc. (though I do see it would be easy to clean up).
|
I was thinking about two options: |
I'm not sure what the right solution for this would be, but it'd be really helpful for the situation described below to:
# first_service/__init__.py
from services.first_service.some_stuff import foo
from services.first_service.some_stuff import other_cool_function In our Not sure if anyone has any other work arounds or suggestions. Thanks! |
Here's an approach that I'm currently working on (these are the most important rules):
I'm also thinking of having an option to disable (2). In particular, |
The motivation for the above rules is that I'd like stubgen to generate usable stubs with minimal manual work. When unsure about whether something should be part of a public API, we'd include it in the public API. Now we'd have extra manual work to trim unnecessary exported names from the stubs, instead of adding missing names. Arguably removing names is less tedious than adding names, and also extra names are less of a problem than missing names, I'd say. |
OK, this makes sense. |
Thanks for the response. That makes sense to me and sounds better than my proposal. 😄 |
What is wrong with making |
stubgen has the |
PEP-484 describes in which situations an imported name (which comes from an
import name
orfrom module import name
) in a stub file are considered part of the exported interface. When generating a stub, stubgen must decide which of the imported names are intended to be reexported (ehich then must be added to the stub asimport name as name
) and which aren't.Currently stubgen handles properly most situations where
__all__
has been defined, but does not attempt to force reexports otherwise (some reexports may happen anyway if a name imported with an alias is required in an annotation).The main problem with the "no
__all__
is set" is that we do not know author intent and we have to guess. There was some discussion within #3169 which was unresolved so I'm opening this issue as a followupThe 2 extreme alternatives when no
__all__
are:a) Assume every name coming from an import in the source module is intended to be exported, and add the aliases in the stubs.
b) Assume no name coming from an import in the source module is intended to be exported, and just keep the imports needed for annotations (the current status)
Other more complicated heuristics may be possible in the spectrum from a to b.
The text was updated successfully, but these errors were encountered: