Skip to content

Non-aliased imported symbol not considered private #2277

@tibdex

Description

@tibdex

Describe the bug

Pyright doesn't flag a non-aliased imported symbol as private.

To Reproduce

# sub.py (lib)
ONE = 1
TWO = 2
# __init__.py (lib)
# ONE is re-exported, TWO is not.
from .sub import ONE as ONE, TWO

THREE = ONE + TWO

Where lib has a py.typed file as described in PEP 561.

# __main__.py (app)
import lib

print(lib.ONE, lib.TWO, lib.THREE)

Expected behavior

According to:

* 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.

and this section of PEP 484:

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.

VS Code extension or command-line

Command line with this config:

[tool.pyright]
typeCheckingMode = "strict"
useLibraryCodeForTypes = true

version: 1.1.166.

Additional context

mypy 0.910 correctly reports the issue:

app/__main__.py:3: error: Module has no attribute "TWO"

Metadata

Metadata

Assignees

No one assigned

    Labels

    addressed in next versionIssue is fixed and will appear in next published versionbugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions