Skip to content

Ensure TypeVarIds are unique #11657

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

Closed
wants to merge 3 commits into from
Closed
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
21 changes: 21 additions & 0 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,17 @@ def analyze_class(self, defn: ClassDef) -> None:
self.mark_incomplete(defn.name, defn)
return

# update the typevar ids such that they will not conflict with any base classes
# (yuck, there has to be a better way to do this.)
if any(isinstance(base[0], Instance) for base in base_types):
offset = max(
self.find_maximum_class_id(base[0].type)
for base in base_types if isinstance(base[0], Instance)
)
# mutating the type vars to be what we want (and hoping nothing previously saved them)
for tvar in tvar_defs:
tvar.id.raw_id += offset

is_typeddict, info = self.typed_dict_analyzer.analyze_typeddict_classdef(defn)
if is_typeddict:
for decorator in defn.decorators:
Expand Down Expand Up @@ -1183,6 +1194,16 @@ def analyze_class(self, defn: ClassDef) -> None:
self.analyze_class_decorator(defn, decorator)
self.analyze_class_body_common(defn)

# should this be memoized in TypeInfo?
def find_maximum_class_id(self, info: TypeInfo) -> int:
if info.bases:
return len(info.type_vars) + max(
self.find_maximum_class_id(cls.type)
for cls in info.bases
)
else:
return len(info.type_vars)

def is_core_builtin_class(self, defn: ClassDef) -> bool:
return self.cur_mod_id == 'builtins' and defn.name in CORE_BUILTIN_CLASSES

Expand Down
31 changes: 26 additions & 5 deletions test-data/unit/check-generics.test
Original file line number Diff line number Diff line change
Expand Up @@ -1933,9 +1933,9 @@ class C(Generic[T]):
class D(C[Tuple[T, S]]): ...
class E(D[S, str]): ...

reveal_type(D.make_one) # N: Revealed type is "def [T, S] (x: Tuple[T`1, S`2]) -> __main__.C[Tuple[T`1, S`2]]"
reveal_type(D.make_one) # N: Revealed type is "def [T, S] (x: Tuple[T`2, S`3]) -> __main__.C[Tuple[T`2, S`3]]"
reveal_type(D[int, str].make_one) # N: Revealed type is "def (x: Tuple[builtins.int*, builtins.str*]) -> __main__.C[Tuple[builtins.int*, builtins.str*]]"
reveal_type(E.make_one) # N: Revealed type is "def [S] (x: Tuple[S`1, builtins.str*]) -> __main__.C[Tuple[S`1, builtins.str*]]"
reveal_type(E.make_one) # N: Revealed type is "def [S] (x: Tuple[S`4, builtins.str*]) -> __main__.C[Tuple[S`4, builtins.str*]]"
reveal_type(E[int].make_one) # N: Revealed type is "def (x: Tuple[builtins.int*, builtins.str*]) -> __main__.C[Tuple[builtins.int*, builtins.str*]]"
[builtins fixtures/classmethod.pyi]

Expand Down Expand Up @@ -2111,11 +2111,11 @@ class A(Generic[T]):

class B(A[T], Generic[T, S]):
def meth(self) -> None:
reveal_type(A[T].foo) # N: Revealed type is "def () -> Tuple[T`1, __main__.A[T`1]]"
reveal_type(A[T].foo) # N: Revealed type is "def () -> Tuple[T`2, __main__.A[T`2]]"
@classmethod
def other(cls) -> None:
reveal_type(cls.foo) # N: Revealed type is "def () -> Tuple[T`1, __main__.B[T`1, S`2]]"
reveal_type(B.foo) # N: Revealed type is "def [T, S] () -> Tuple[T`1, __main__.B[T`1, S`2]]"
reveal_type(cls.foo) # N: Revealed type is "def () -> Tuple[T`2, __main__.B[T`2, S`3]]"
reveal_type(B.foo) # N: Revealed type is "def [T, S] () -> Tuple[T`2, __main__.B[T`2, S`3]]"
[builtins fixtures/classmethod.pyi]

[case testGenericClassAlternativeConstructorPrecise]
Expand Down Expand Up @@ -2504,3 +2504,24 @@ b: I[I[Any]]
reveal_type([a, b]) # N: Revealed type is "builtins.list[__main__.I*[__main__.I[Any]]]"
reveal_type([b, a]) # N: Revealed type is "builtins.list[__main__.I*[__main__.I[Any]]]"
[builtins fixtures/list.pyi]

[case testOverlappingTypeVarIds]
from typing import TypeVar, Generic

class A: ...
class B: ...

T = TypeVar("T", bound=A)
V = TypeVar("V", bound=B)
S = TypeVar("S")

class Whatever(Generic[T]):
def something(self: S) -> S:
return self

# the "V" here had the same id as "T" and so mypy used to think it could expand one into another.
# this test is here to make sure that doesn't happen!
class WhateverPartTwo(Whatever[A], Generic[V]):
def something(self: S) -> S:
return self

6 changes: 3 additions & 3 deletions test-data/unit/semanal-types.test
Original file line number Diff line number Diff line change
Expand Up @@ -662,9 +662,9 @@ MypyFile:1(
ClassDef:4(
c
TypeVars(
t`1)
t`2)
BaseType(
__main__.d[t`1])
__main__.d[t`2])
PassStmt:4()))

[case testTupleType]
Expand Down Expand Up @@ -907,7 +907,7 @@ MypyFile:1(
ClassDef:5(
A
TypeVars(
t`1)
t`2)
BaseType(
__main__.B[Any])
PassStmt:5()))
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/typexport-basic.test
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ class B(A[C, T], Generic[T]):
CallExpr(9) : None
MemberExpr(9) : def (a: C)
NameExpr(9) : C
NameExpr(9) : B[T`1]
NameExpr(9) : B[T`3]

[case testExternalReferenceWithGenericInheritance]
from typing import TypeVar, Generic
Expand Down