Skip to content

Commit e632fea

Browse files
author
hauntsaninja
committed
Add __path__ to package __init__
Resolves python#1422
1 parent 89de623 commit e632fea

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

mypy/nodes.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,13 @@ def get_column(self) -> int:
9797
inverse_node_kinds = {_kind: _name for _name, _kind in node_kinds.items()} # type: Final
9898

9999

100-
implicit_module_attrs = {'__name__': '__builtins__.str',
101-
'__doc__': None, # depends on Python version, see semanal.py
102-
'__file__': '__builtins__.str',
103-
'__package__': '__builtins__.str'} # type: Final
100+
implicit_module_attrs = {
101+
'__name__': '__builtins__.str',
102+
'__doc__': None, # depends on Python version, see semanal.py
103+
'__path__': None, # depends on if the module is a package
104+
'__file__': '__builtins__.str',
105+
'__package__': '__builtins__.str'
106+
} # type: Final
104107

105108

106109
# These aliases exist because built-in class objects are not subscriptable.

mypy/semanal.py

+6
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,12 @@ def add_implicit_module_attrs(self, file_node: MypyFile) -> None:
400400
else:
401401
typ = UnionType([UnboundType('__builtins__.str'),
402402
UnboundType('__builtins__.unicode')])
403+
elif name == '__path__':
404+
if not file_node.is_package_init_file():
405+
continue
406+
# Need to construct the type ourselves, to avoid issues with __builtins__.list
407+
# not being subscriptable or typing.List not getting bound
408+
typ = self.named_type("__builtins__.list", [self.str_type()])
403409
else:
404410
assert t is not None, 'type should be specified for {}'.format(name)
405411
typ = UnboundType(t)

test-data/unit/check-modules.test

+10
Original file line numberDiff line numberDiff line change
@@ -2825,3 +2825,13 @@ from mystery import a, b as b, c as d
28252825
[out]
28262826
tmp/stub.pyi:1: error: Cannot find implementation or library stub for module named 'mystery'
28272827
tmp/stub.pyi:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
2828+
2829+
[case testPackagePath]
2830+
import p
2831+
reveal_type(p.__path__) # N: Revealed type is 'builtins.list[builtins.str]'
2832+
p.m.__path__ # E: "object" has no attribute "__path__"
2833+
2834+
[file p/__init__.py]
2835+
from . import m as m
2836+
[file p/m.py]
2837+
[builtins fixtures/list.pyi]

0 commit comments

Comments
 (0)