Skip to content
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
multiple nested case
  • Loading branch information
changhoetyng committed Sep 24, 2024
commit b0d9fe5ea3ce2d27dd5243d1b0468c5538acf182
25 changes: 24 additions & 1 deletion mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -6408,9 +6408,32 @@ def lookup_fully_qualified_or_none(self, fullname: str) -> SymbolTableNode | Non
# TODO: support nested classes (but consider performance impact,
# we might keep the module level only lookup for thing like 'builtins.int').
assert "." in fullname

module, name = fullname.rsplit(".", maxsplit=1)

# The reason for this is that we want to be able to handle cases such as importlib.machinery
if module not in self.modules:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep the original "fast path" which we'll be hitting most of the time. This way your new logic, which is a bit more expensive, it not slowing he typical code path. This may be performance critical. In more concrete terms, add your new logic under if module not in self.modules:, and keep the other code as is in the function.

return None
# Check if there's nested module A.B.C
splitted = fullname.rsplit(".")
module, name = splitted[0], splitted[1:]
# If module still not in modules, return None
if module not in self.modules:
return None
filenode = self.modules[module]
result = filenode.names.get(name[0])

if result is None and self.is_incomplete_namespace(module):
# TODO: More explicit handling of incomplete refs?
self.record_incomplete_ref()

for part in name[1:]:
if result is not None and isinstance(result.node, TypeInfo):
filenode = result.node
result = filenode.names.get(part)
else:
return None
return result

filenode = self.modules[module]
result = filenode.names.get(name)
if result is None and self.is_incomplete_namespace(module):
Expand Down
60 changes: 60 additions & 0 deletions test-data/unit/check-python312.test
Original file line number Diff line number Diff line change
Expand Up @@ -1873,3 +1873,63 @@ d1: Multi[int, str] = Multi[float, str]() # E: Incompatible types in assignment
d2: Multi[float, str] = Multi[int, str]() # E: Incompatible types in assignment (expression has type "Multi[int, str]", variable has type "Multi[float, str]")
d3: Multi[str, int] = Multi[str, float]()
d4: Multi[str, float] = Multi[str, int]() # E: Incompatible types in assignment (expression has type "Multi[str, int]", variable has type "Multi[str, float]")

[case testPEP695MultipleNestedGenericClass1]
# flags: --enable-incomplete-feature=NewGenericSyntax
class A:
class B:
class C:
class D[Q]:
def g(self, x: Q): ...
d: D[str]

x: A.B.C.D[int]
x.g('a') # E: Argument 1 to "g" of "D" has incompatible type "str"; expected "int"
reveal_type(x) # N: Revealed type is "__main__.A.B.C.D[builtins.int]"
reveal_type(A.B.C.d) # N: Revealed type is "__main__.A.B.C.D[builtins.str]"

[case testPEP695MultipleNestedGenericClass2]
# flags: --enable-incomplete-feature=NewGenericSyntax
class A:
class B:
def m(self) -> None:
class C[T]:
def f(self) -> T: ...
x: C[int]
reveal_type(x.f()) # N: Revealed type is "builtins.int"
self.a = C[str]()

reveal_type(A().B().a) # N: Revealed type is "__main__.C@5[builtins.str]"

[case testPEP695MultipleNestedGenericClass3]
# flags: --enable-incomplete-feature=NewGenericSyntax
class A:
class C[T]:
def f(self) -> T: ...
class D[S]:
x: T # E: Name "T" is not defined
def g(self) -> S: ...

a: A.C[int]
reveal_type(a.f()) # N: Revealed type is "builtins.int"
b: A.C.D[str]
reveal_type(b.g()) # N: Revealed type is "builtins.str"

class B:
class E[T]:
class F[T]: # E: "T" already defined as a type parameter
x: T

c: B.E.F[int]

[case testPEP695MultipleNestedGenericClass4]
# flags: --enable-incomplete-feature=NewGenericSyntax
class Z:
class A:
class B[T]:
def __get__(self, instance: Z.A, owner: type[Z.A]) -> T:
return None # E: Incompatible return value type (got "None", expected "T")
f = B[int]()

a = Z.A()
v = a.f
20 changes: 20 additions & 0 deletions test-data/unit/fine-grained-python312.test
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,23 @@ def f(x: int) -> None: pass
[out]
==
main:7: error: Missing positional argument "x" in call to "f"

[case testPEP695MultipleNestedGenericClassMethodUpdated]
from a import f

class A:
class C:
class D[T]:
x: T
def m(self) -> T:
f()
return self.x

[file a.py]
def f() -> None: pass

[file a.py.2]
def f(x: int) -> None: pass
[out]
==
main:8: error: Missing positional argument "x" in call to "f"