8000 Add __path__ to package __init__ by hauntsaninja · Pull Request #9454 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Add __path__ to package __init__ #9454

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

Merged
merged 8 commits into from
Aug 4, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add __path__ to package __init__
Resolves #1422
  • Loading branch information
hauntsaninja committed Oct 6, 2020
commit e632feaecc11bc8f1e1f12c8842a2b55458e1ae6
11 changes: 7 additions & 4 deletions mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,13 @@ def get_column(self) -> int:
inverse_node_kinds = {_kind: _name for _name, _kind in node_kinds.items()} # type: Final


implicit_module_attrs = {'__name__': '__builtins__.str',
'__doc__': None, # depends on Python version, see semanal.py
'__file__': '__builtins__.str',
'__package__': '__builtins__.str'} # type: Final
implicit_module_attrs = {
'__name__': '__builtins__.str',
'__doc__': None, # depends on Python version, see semanal.py
'__path__': None, # depends on if the module is a package
'__file__': '__builtins__.str',
'__package__': '__builtins__.str'
} # type: Final


# These aliases exist because built-in class objects are not subscriptable.
Expand Down
6 changes: 6 additions & 0 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,12 @@ def add_implicit_module_attrs(self, file_node: MypyFile) -> None:
else:
typ = UnionType([UnboundType('__builtins__.str'),
UnboundType('__builtins__.unicode')])
elif name == '__path__':
if not file_node.is_package_init_file():
continue
# Need to construct the type ourselves, to avoid issues with __builtins__.list
# not being subscriptable or typing.List not getting bound
typ = self.named_type("__builtins__.list", [self.str_type()])
else:
assert t is not None, 'type should be specified for {}'.format(name)
typ = UnboundType(t)
Expand Down
10 changes: 10 additions & 0 deletions test-data/unit/check-modules.test
Original file line number Diff line number Diff line change
Expand Up @@ -2825,3 +2825,13 @@ from mystery import a, b as b, c as d
[out]
tmp/stub.pyi:1: error: Cannot find implementation or library stub for module named 'mystery'
tmp/stub.pyi:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports

[case testPackagePath]
import p
reveal_type(p.__path__) # N: Revealed type is 'builtins.list[builtins.str]'
p.m.__path__ # E: "object" has no attribute "__path__"

[file p/__init__.py]
from . import m as m
[file p/m.py]
[builtins fixtures/list.pyi]
0