You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Non-aliased imported symbol not considered private #2277
* Imported symbols are considered private by default. If they use the “import A as A” (a redundant module alias), “from X import A as A” (a redundant symbol alias), or “from . import A” forms, symbol “A” is not private unless the name begins with an underscore. If a file `__init__.py` uses form “from .A import X”, symbol “A” is treated likewise. If a wildcard import (of the form “from X import *”) is used, all symbols referenced by the wildcard are not private.
Modules and variables imported into the stub are not considered exported from the stub unless the import uses the import ... as ... form or the equivalent from ... import ... as ... form. (UPDATE: To clarify, the intention here is that only names imported using the form X as X will be exported, i.e. the name before and after as must be the same.)
reportPrivateUsage (or another rule) should say that lib.TWO is private.
Thanks for the bug report. You're correct that there was a missing check. I've implemented a fix. We're going to need to take care in rolling out this bug fix because it's likely to generate new errors within some code bases.
Jake Bailey (@jakebailey), here's the PR: #2279. I plan to wait until after this week's pylance release before merging this.
We've decided to add a new diagnostic rule. It's called "reportPrivateImportUsage". It will be on by default with basic type checking rules, but it can be disabled to retain the existing behavior. This will give you the ability to work around cases where the maintainers of a "py.typed" library haven't properly added redundant alias forms to indicate which imported symbols should be re-exported.
Reacted by Thibault Derousseaux, Fabien Celier and Brieuc Loussouarn
Is this a correct interpretation of PEP 484? It seems to be in the section for notes on stub files, and it's not clear to me why this should apply to libraries typed inline as well. Can you provide more information on your reasoning? At least some maintainers appear to believe it's a bug and the discussion therein wasn't clarified despite crosslinking here Textualize/rich#1523
This applies to type stubs and py.typed libraries. For details, refer to this documentation for details. This behavior hasn't been formally specified yet in a PEP, but it is generally agreed upon by the typing community. Without this, there's no way for the author of a py.typed library to specify which symbols comprise the public interface of the library. Unlike most other modern languages, Python lacks an export keyword or similar construct.
Reacted by Gregory Beauregard, Thibault Derousseaux and Oliver Mannion
Thanks, this clarifies things a lot. I do sympathize with the linked maintainer, though. The symbols there are solely imported as part of an intro/demo program in __main__.py. Is it worth blocklisting __main__.py files from this check? For what it's worth mypy --strict doesn't throw any errors with that particular library so mypy may be doing something similar.
Activity
erictraut commentedon Sep 8, 2021
Thanks for the bug report. You're correct that there was a missing check. I've implemented a fix. We're going to need to take care in rolling out this bug fix because it's likely to generate new errors within some code bases.
Jake Bailey (@jakebailey), here's the PR: #2279. I plan to wait until after this week's pylance release before merging this.
erictraut commentedon Sep 12, 2021
We've decided to add a new diagnostic rule. It's called "reportPrivateImportUsage". It will be on by default with basic type checking rules, but it can be disabled to retain the existing behavior. This will give you the ability to work around cases where the maintainers of a "py.typed" library haven't properly added redundant alias forms to indicate which imported symbols should be re-exported.
erictraut commentedon Sep 14, 2021
This is included in pyright 1.1.168, which I just published. It will also be included in the next release of pylance.
GBeauregard commentedon Oct 7, 2021
Is this a correct interpretation of PEP 484? It seems to be in the section for notes on stub files, and it's not clear to me why this should apply to libraries typed inline as well. Can you provide more information on your reasoning? At least some maintainers appear to believe it's a bug and the discussion therein wasn't clarified despite crosslinking here Textualize/rich#1523
erictraut commentedon Oct 7, 2021
This applies to type stubs and py.typed libraries. For details, refer to this documentation for details. This behavior hasn't been formally specified yet in a PEP, but it is generally agreed upon by the typing community. Without this, there's no way for the author of a py.typed library to specify which symbols comprise the public interface of the library. Unlike most other modern languages, Python lacks an
exportkeyword or similar construct.GBeauregard commentedon Oct 7, 2021
Thanks, this clarifies things a lot. I do sympathize with the linked maintainer, though. The symbols there are solely imported as part of an intro/demo program in
__main__.py. Is it worth blocklisting__main__.pyfiles from this check? For what it's worthmypy --strictdoesn't throw any errors with that particular library so mypy may be doing something similar.Explicitly define public interface via __all__
Fix "not exported from module" error in Pyright (#51)
Alias exports to mark them as exported
Changes for pyright #55