8000 Always freeze type variables that were previously freshen (#8075) · python/mypy@ce8951e · GitHub
[go: up one dir, main page]

Skip to content

Commit ce8951e

Browse files
authored
Always freeze type variables that were previously freshen (#8075)
Fixes #8072 The fix is straightforward, just copy the logic from the working case (see last test) to two other code paths. Note that only two first cases I added crash on master, but I also add the last one for completeness because I didn't find a similar existing test case.
1 parent e05bd1c commit ce8951e

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

mypy/checkmember.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,9 +569,10 @@ def analyze_var(name: str,
569569
dispatched_type = meet.meet_types(mx.original_type, itype)
570570
signature = freshen_function_type_vars(functype)
571571
signature = check_self_arg(signature, dispatched_type, var.is_classmethod,
572-
mx.context, name, mx.msg)
572+
mx.context, name, mx.msg)
573573
signature = bind_self(signature, mx.self_type, var.is_classmethod)
574574
expanded_signature = get_proper_type(expand_type_by_instance(signature, itype))
575+
freeze_type_vars(expanded_signature)
575576
if var.is_property:
576577
# A property cannot have an overloaded type => the cast is fine.
577578
assert isinstance(expanded_signature, CallableType)
@@ -854,6 +855,7 @@ class B(A[str]): pass
854855
t = bind_self(t, original_type, is_classmethod=True)
855856
assert isuper is not None
856857
t = cast(CallableType, expand_type_by_instance(t, isuper))
858+
freeze_type_vars(t)
857859
return t.copy_modified(variables=tvars + t.variables)
858860
elif isinstance(t, Overloaded):
859861
return Overloaded([cast(CallableType, add_class_tvars(item, isuper,

test-data/unit/check-serialize.test

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,3 +1336,61 @@ import b
13361336
def foo() -> None:
13371337
class Foo:
13381338
class Bar: pass
1339+
1340+
[case testSerializeGenericClassMethod]
1341+
import a
1342+
[file a.py]
1343+
import b
1344+
from typing import TypeVar
1345+
1346+
T = TypeVar('T')
1347+
1348+
class C:
1349+
@classmethod
1350+
def f(cls, x: T) -> T: ...
1351+
1352+
x = C.f
1353+
[file b.py]
1354+
x = 1
1355+
[file b.py.2]
1356+
x = 'yes'
1357+
[builtins fixtures/classmethod.pyi]
1358+
1359+
[case testSerializeGenericAbstractMethod]
1360+
import a
1361+
[file a.py]
1362+
import b
1363+
from typing import TypeVar
1364+
from abc import abstractmethod
1365+
1366+
T = TypeVar('T')
1367+
1368+
class C:
1369+
@abstractmethod
1370+
def f(self, x: T) -> T: ...
1371+
1372+
c: C
1373+
x = c.f
1374+
[file b.py]
1375+
x = 1
1376+
[file b.py.2]
1377+
x = 'yes'
1378+
1379+
[case testSerializeGenericNormalMethod]
1380+
import a
1381+
[file a.py]
1382+
import b
1383+
from typing import TypeVar
1384+
from abc import abstractmethod
1385+
1386+
T = TypeVar('T')
1387+
1388+
class C:
1389+
def f(self, x: T) -> T: ...
1390+
1391+
c: C
1392+
x = c.f
1393+
[file b.py]
1394+
x = 1
1395+
[file b.py.2]
1396+
x = 'yes'

0 commit comments

Comments
 (0)
0