Skip to content
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

Fix module and protocol subtyping, module hasattr #13778

Merged
merged 3 commits into from
Oct 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 8 additions & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -6472,8 +6472,14 @@ def partition_union_by_attr(
return with_attr, without_attr

def has_valid_attribute(self, typ: Type, name: str) -> bool:
if isinstance(get_proper_type(typ), AnyType):
p_typ = get_proper_type(typ)
if isinstance(p_typ, AnyType):
return False
if isinstance(p_typ, Instance) and p_typ.extra_attrs and p_typ.extra_attrs.mod_name:
# Presence of module_symbol_table means this check will skip ModuleType.__getattr__
module_symbol_table = p_typ.type.names
else:
module_symbol_table = None
with self.msg.filter_errors() as watcher:
analyze_member_access(
name,
Expand All @@ -6487,6 +6493,7 @@ def has_valid_attribute(self, typ: Type, name: str) -> bool:
chk=self,
# This is not a real attribute lookup so don't mess with deferring nodes.
no_deferral=True,
module_symbol_table=module_symbol_table,
)
return not watcher.has_new_errors()

Expand Down
1 change: 1 addition & 0 deletions mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,7 @@ def find_member(
and name not in ["__getattr__", "__setattr__", "__getattribute__"]
and not is_operator
and not class_obj
and itype.extra_attrs is None # skip ModuleType.__getattr__
):
for method_name in ("__getattribute__", "__getattr__"):
# Normally, mypy assumes that instances that define __getattr__ have all
Expand Down
3 changes: 3 additions & 0 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1192,6 +1192,9 @@ def __eq__(self, other: object) -> bool:
def copy(self) -> ExtraAttrs:
return ExtraAttrs(self.attrs.copy(), self.immutable.copy(), self.mod_name)

def __repr__(self) -> str:
return f"ExtraAttrs({self.attrs!r}, {self.immutable!r}, {self.mod_name!r})"


class Instance(ProperType):
"""An instance type of form C[T1, ..., Tn].
Expand Down
7 changes: 7 additions & 0 deletions test-data/unit/check-isinstance.test
Original file line number Diff line number Diff line change
Expand Up @@ -2895,6 +2895,13 @@ else:
mod.y # E: Module has no attribute "y"
reveal_type(mod.x) # N: Revealed type is "builtins.int"

if hasattr(mod, "x"):
mod.y # E: Module has no attribute "y"
reveal_type(mod.x) # N: Revealed type is "builtins.int"
else:
mod.y # E: Module has no attribute "y"
reveal_type(mod.x) # N: Revealed type is "builtins.int"

[file mod.py]
x: int
[builtins fixtures/module.pyi]
Expand Down
8 changes: 4 additions & 4 deletions test-data/unit/check-modules.test
Original file line number Diff line number Diff line change
Expand Up @@ -1672,11 +1672,11 @@ mod_any: Any = m
mod_int: int = m # E: Incompatible types in assignment (expression has type Module, variable has type "int")

reveal_type(mod_mod) # N: Revealed type is "types.ModuleType"
mod_mod.a # E: Module has no attribute "a"
reveal_type(mod_mod.a) # N: Revealed type is "Any"
reveal_type(mod_mod2) # N: Revealed type is "types.ModuleType"
mod_mod2.a # E: Module has no attribute "a"
reveal_type(mod_mod2.a) # N: Revealed type is "Any"
reveal_type(mod_mod3) # N: Revealed type is "types.ModuleType"
mod_mod3.a # E: Module has no attribute "a"
reveal_type(mod_mod3.a) # N: Revealed type is "Any"
reveal_type(mod_any) # N: Revealed type is "Any"

[file m.py]
Expand Down Expand Up @@ -1736,7 +1736,7 @@ if bool():
else:
x = n

x.a # E: Module has no attribute "a"
reveal_type(x.nope) # N: Revealed type is "Any"
reveal_type(x.__file__) # N: Revealed type is "builtins.str"

[file m.py]
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/fine-grained-inspect.test
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ class C: ...
[builtins fixtures/module.pyi]
[out]
==
{"<pack.bar>": ["C", "__annotations__", "__doc__", "__file__", "__name__", "__package__", "bar", "x"], "ModuleType": ["__file__"]}
{"<pack.bar>": ["C", "__annotations__", "__doc__", "__file__", "__name__", "__package__", "bar", "x"], "ModuleType": ["__file__", "__getattr__"]}

[case testInspectModuleDef]
# inspect2: --show=definition --include-kind foo.py:2:1
Expand Down
5 changes: 3 additions & 2 deletions test-data/unit/lib-stub/types.pyi
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from typing import TypeVar
from typing import Any, TypeVar
import sys

_T = TypeVar('_T')

def coroutine(func: _T) -> _T: pass

class ModuleType:
__file__ = ... # type: str
__file__: str
def __getattr__(self, name: str) -> Any: pass

if sys.version_info >= (3, 10):
class Union:
Expand Down