Skip to content

Commit d687e71

Browse files
author
hauntsaninja
committed
Add __path__ to package __init__
Resolves python#1422
1 parent 37777b3 commit d687e71

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
@@ -2810,3 +2810,13 @@ CustomDict = TypedDict(
28102810
)
28112811
[typing fixtures/typing-full.pyi]
28122812
[builtins fixtures/tuple.pyi]
2813+
2814+
[case testPackagePath]
2815+
import p
2816+
reveal_type(p.__path__) # N: Revealed type is 'builtins.list[builtins.str]'
2817+
p.m.__path__ # E: "object" has no attribute "__path__"
2818+
2819+
[file p/__init__.py]
2820+
from . import m as m
2821+
[file p/m.py]
2822+
[builtins fixtures/list.pyi]

0 commit comments

Comments
 (0)