-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
[red-knot] implement re-export conventions for imports #14099
Comments
As you say, it's very important that we apply these semantics in stub files. For runtime files, my instinct is to say that we should copy the runtime semantics exactly, but have an additional disabled-by-default error code (let's call it |
I could look into this. |
This makes sense to me. I definitely think that if we see an import of a not-explicitly-re-exported import, we should still infer the correct type that we are able to observe from the runtime semantics, even if we also issue a warning. I think the only difference between what you suggest here and what mypy does (according to my local testing) is that it appears to me the warning is enabled by default in mypy, not disabled by default. I get the warning without using a mypy config file or any additional CLI flags. One additional wrinkle to throw in -- I got clarification at microsoft/pyright#9385 that pyright makes a distinction I hadn't realized here: it only applies these import rules to non-stub modules if they are third-party modules, not first-party ones. The idea is that these conventions are how you clarify the public API of a typed library, but they don't apply within a codebase. I can see the rationale there, and I can also see how the structure of the specification suggests that: the "import conventions" section is within the page about distributing typed libraries. So I think I would weakly favor making the same distinction? |
Mypy's behaviour is:
Random thoughts:
|
Thanks, that's useful! One note on pyright's first vs third party distinction; I think it works in the opposite way from what you're suggesting. Pyright applies strict re-export rules to imports from third party libraries, but not to imports from your own first-party code. The idea is that this allow library authors more control over their exported api surface. |
@charliermarsh are you still planning to work on this? |
Unfortunately dropped the ball here. I’ll re-assign if I pick it back up, but others are welcome to take it over. |
See https://typing.readthedocs.io/en/latest/spec/distributing.html#import-conventions
A normal
from foo import bar
in modulea
should not allow another module to dofrom a import bar
; an explicitfrom foo import bar as bar
should be required to specify a re-export.In the current typing spec, these rules are worded as applicable to all modules. Mypy does apply them in all modules, pyright applies them only in type stubs.
I have a vague memory that these originated as stub-only conventions, but I haven't been able to dig up confirmation.
We need to decide if we will apply these conventions in all modules, or only in stubs, and then implement them accordingly.
In particular this is problematic at the moment because we don't apply these conventions in
builtins.pyi
, meaning we understand all symbols imported intobuiltins.pyi
(e.g. varioustyping
module symbols) as builtin names.The text was updated successfully, but these errors were encountered: