Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9e1f4df

Browse files
authored
Use TypeVar refresh uniformly for class object access (#15945)
Fixes#15934I think this is a right thing to do, it may even fix some other rareaccidental `TypeVar` clashes not involving self-types. This causes a bitof churn in tests, but not too much.
1 parent351371d commit9e1f4df

File tree

6 files changed

+52
-31
lines changed

6 files changed

+52
-31
lines changed

‎mypy/checkmember.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,12 +1198,12 @@ class B(A[str]): pass
11981198
# (i.e. appear in the return type of the class object on which the method was accessed).
11991199
ifisinstance(t,CallableType):
12001200
tvars=original_varsiforiginal_varsisnotNoneelse []
1201+
t=freshen_all_functions_type_vars(t)
12011202
ifis_classmethod:
1202-
t=freshen_all_functions_type_vars(t)
12031203
t=bind_self(t,original_type,is_classmethod=True)
12041204
assertisuperisnotNone
12051205
t=expand_type_by_instance(t,isuper)
1206-
freeze_all_type_vars(t)
1206+
freeze_all_type_vars(t)
12071207
returnt.copy_modified(variables=list(tvars)+list(t.variables))
12081208
elifisinstance(t,Overloaded):
12091209
returnOverloaded(

‎test-data/unit/check-classes.test‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1162,7 +1162,7 @@ def test() -> None:
11621162
reveal_type(x) # N: Revealed type is "T`-1"
11631163
reveal_type(x.returns_int()) # N: Revealed type is "builtins.int"
11641164
return foo
1165-
reveal_type(Foo.bar) # N: Revealed type is "def [T <: __main__.Foo@5] (self: __main__.Foo@5, foo: T`-1) -> T`-1"
1165+
reveal_type(Foo.bar) # N: Revealed type is "def [T <: __main__.Foo@5] (self: __main__.Foo@5, foo: T`1) -> T`1"
11661166

11671167
[case testGenericClassWithInvalidTypevarUseWithinFunction]
11681168
from typing import TypeVar

‎test-data/unit/check-incremental.test‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3035,10 +3035,10 @@ main:15: error: Unsupported left operand type for >= ("NoCmp")
30353035
[case testAttrsIncrementalDunder]
30363036
from a import A
30373037
reveal_type(A) # N: Revealed type is "def (a: builtins.int) -> a.A"
3038-
reveal_type(A.__lt__) # N: Revealed type is "def [_AT] (self: _AT`-1, other: _AT`-1) -> builtins.bool"
3039-
reveal_type(A.__le__) # N: Revealed type is "def [_AT] (self: _AT`-1, other: _AT`-1) -> builtins.bool"
3040-
reveal_type(A.__gt__) # N: Revealed type is "def [_AT] (self: _AT`-1, other: _AT`-1) -> builtins.bool"
3041-
reveal_type(A.__ge__) # N: Revealed type is "def [_AT] (self: _AT`-1, other: _AT`-1) -> builtins.bool"
3038+
reveal_type(A.__lt__) # N: Revealed type is "def [_AT] (self: _AT`3, other: _AT`3) -> builtins.bool"
3039+
reveal_type(A.__le__) # N: Revealed type is "def [_AT] (self: _AT`4, other: _AT`4) -> builtins.bool"
3040+
reveal_type(A.__gt__) # N: Revealed type is "def [_AT] (self: _AT`5, other: _AT`5) -> builtins.bool"
3041+
reveal_type(A.__ge__) # N: Revealed type is "def [_AT] (self: _AT`6, other: _AT`6) -> builtins.bool"
30423042

30433043
A(1) < A(2)
30443044
A(1) <= A(2)
@@ -3072,10 +3072,10 @@ class A:
30723072
[stale]
30733073
[out2]
30743074
main:2: note: Revealed type is "def (a: builtins.int) -> a.A"
3075-
main:3: note: Revealed type is "def [_AT] (self: _AT`-1, other: _AT`-1) -> builtins.bool"
3076-
main:4: note: Revealed type is "def [_AT] (self: _AT`-1, other: _AT`-1) -> builtins.bool"
3077-
main:5: note: Revealed type is "def [_AT] (self: _AT`-1, other: _AT`-1) -> builtins.bool"
3078-
main:6: note: Revealed type is "def [_AT] (self: _AT`-1, other: _AT`-1) -> builtins.bool"
3075+
main:3: note: Revealed type is "def [_AT] (self: _AT`1, other: _AT`1) -> builtins.bool"
3076+
main:4: note: Revealed type is "def [_AT] (self: _AT`2, other: _AT`2) -> builtins.bool"
3077+
main:5: note: Revealed type is "def [_AT] (self: _AT`3, other: _AT`3) -> builtins.bool"
3078+
main:6: note: Revealed type is "def [_AT] (self: _AT`4, other: _AT`4) -> builtins.bool"
30793079
main:15: error: Unsupported operand types for < ("A" and "int")
30803080
main:16: error: Unsupported operand types for <= ("A" and "int")
30813081
main:17: error: Unsupported operand types for > ("A" and "int")
@@ -3963,10 +3963,10 @@ class A:
39633963
tmp/b.py:3: note: Revealed type is "def (a: builtins.int) -> a.A"
39643964
tmp/b.py:4: note: Revealed type is "def (builtins.object, builtins.object) -> builtins.bool"
39653965
tmp/b.py:5: note: Revealed type is "def (builtins.object, builtins.object) -> builtins.bool"
3966-
tmp/b.py:6: note: Revealed type is "def [_DT] (self: _DT`-1, other: _DT`-1) -> builtins.bool"
3967-
tmp/b.py:7: note: Revealed type is "def [_DT] (self: _DT`-1, other: _DT`-1) -> builtins.bool"
3968-
tmp/b.py:8: note: Revealed type is "def [_DT] (self: _DT`-1, other: _DT`-1) -> builtins.bool"
3969-
tmp/b.py:9: note: Revealed type is "def [_DT] (self: _DT`-1, other: _DT`-1) -> builtins.bool"
3966+
tmp/b.py:6: note: Revealed type is "def [_DT] (self: _DT`1, other: _DT`1) -> builtins.bool"
3967+
tmp/b.py:7: note: Revealed type is "def [_DT] (self: _DT`2, other: _DT`2) -> builtins.bool"
3968+
tmp/b.py:8: note: Revealed type is "def [_DT] (self: _DT`3, other: _DT`3) -> builtins.bool"
3969+
tmp/b.py:9: note: Revealed type is "def [_DT] (self: _DT`4, other: _DT`4) -> builtins.bool"
39703970
tmp/b.py:18: error: Unsupported operand types for < ("A" and "int")
39713971
tmp/b.py:19: error: Unsupported operand types for <= ("A" and "int")
39723972
tmp/b.py:20: error: Unsupported operand types for > ("A" and "int")
@@ -6325,7 +6325,7 @@ reveal_type(D.meth)
63256325
reveal_type(D().meth)
63266326
[out]
63276327
[out2]
6328-
tmp/m.py:4: note: Revealed type is "def [Self <: lib.C] (self: Self`0, other: Self`0) -> Self`0"
6328+
tmp/m.py:4: note: Revealed type is "def [Self <: lib.C] (self: Self`1, other: Self`1) -> Self`1"
63296329
tmp/m.py:5: note: Revealed type is "def (other: m.D) -> m.D"
63306330

63316331
[case testIncrementalNestedGenericCallableCrash]

‎test-data/unit/check-parameter-specification.test‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -901,8 +901,8 @@ class A:
901901
def func(self, action: Callable[_P, _R], *args: _P.args, **kwargs: _P.kwargs) -> _R:
902902
...
903903

904-
reveal_type(A.func) # N: Revealed type is "def [_P, _R] (self: __main__.A, action: def (*_P.args, **_P.kwargs) -> _R`-2, *_P.args, **_P.kwargs) -> _R`-2"
905-
reveal_type(A().func) # N: Revealed type is "def [_P, _R] (action: def (*_P.args, **_P.kwargs) -> _R`5, *_P.args, **_P.kwargs) -> _R`5"
904+
reveal_type(A.func) # N: Revealed type is "def [_P, _R] (self: __main__.A, action: def (*_P.args, **_P.kwargs) -> _R`3, *_P.args, **_P.kwargs) -> _R`3"
905+
reveal_type(A().func) # N: Revealed type is "def [_P, _R] (action: def (*_P.args, **_P.kwargs) -> _R`7, *_P.args, **_P.kwargs) -> _R`7"
906906

907907
def f(x: int) -> int:
908908
...
@@ -934,8 +934,8 @@ class A:
934934
def func(self, action: Job[_P, None]) -> Job[_P, None]:
935935
...
936936

937-
reveal_type(A.func) # N: Revealed type is "def [_P] (self: __main__.A, action: __main__.Job[_P`-1, None]) -> __main__.Job[_P`-1, None]"
938-
reveal_type(A().func) # N: Revealed type is "def [_P] (action: __main__.Job[_P`3, None]) -> __main__.Job[_P`3, None]"
937+
reveal_type(A.func) # N: Revealed type is "def [_P] (self: __main__.A, action: __main__.Job[_P`2, None]) -> __main__.Job[_P`2, None]"
938+
reveal_type(A().func) # N: Revealed type is "def [_P] (action: __main__.Job[_P`4, None]) -> __main__.Job[_P`4, None]"
939939
reveal_type(A().func(Job(lambda x: x))) # N: Revealed type is "__main__.Job[[x: Any], None]"
940940

941941
def f(x: int, y: int) -> None: ...

‎test-data/unit/check-plugin-attrs.test‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,10 @@ from attr import attrib, attrs
185185
class A:
186186
a: int
187187
reveal_type(A) # N: Revealed type is "def (a: builtins.int) -> __main__.A"
188-
reveal_type(A.__lt__) # N: Revealed type is "def [_AT] (self: _AT`-1, other: _AT`-1) -> builtins.bool"
189-
reveal_type(A.__le__) # N: Revealed type is "def [_AT] (self: _AT`-1, other: _AT`-1) -> builtins.bool"
190-
reveal_type(A.__gt__) # N: Revealed type is "def [_AT] (self: _AT`-1, other: _AT`-1) -> builtins.bool"
191-
reveal_type(A.__ge__) # N: Revealed type is "def [_AT] (self: _AT`-1, other: _AT`-1) -> builtins.bool"
188+
reveal_type(A.__lt__) # N: Revealed type is "def [_AT] (self: _AT`3, other: _AT`3) -> builtins.bool"
189+
reveal_type(A.__le__) # N: Revealed type is "def [_AT] (self: _AT`4, other: _AT`4) -> builtins.bool"
190+
reveal_type(A.__gt__) # N: Revealed type is "def [_AT] (self: _AT`5, other: _AT`5) -> builtins.bool"
191+
reveal_type(A.__ge__) # N: Revealed type is "def [_AT] (self: _AT`6, other: _AT`6) -> builtins.bool"
192192

193193
A(1) < A(2)
194194
A(1) <= A(2)
@@ -989,10 +989,10 @@ class C(A, B): pass
989989
@attr.s
990990
class D(A): pass
991991

992-
reveal_type(A.__lt__) # N: Revealed type is "def [_AT] (self: _AT`-1, other: _AT`-1) -> builtins.bool"
993-
reveal_type(B.__lt__) # N: Revealed type is "def [_AT] (self: _AT`-1, other: _AT`-1) -> builtins.bool"
994-
reveal_type(C.__lt__) # N: Revealed type is "def [_AT] (self: _AT`-1, other: _AT`-1) -> builtins.bool"
995-
reveal_type(D.__lt__) # N: Revealed type is "def [_AT] (self: _AT`-1, other: _AT`-1) -> builtins.bool"
992+
reveal_type(A.__lt__) # N: Revealed type is "def [_AT] (self: _AT`5, other: _AT`5) -> builtins.bool"
993+
reveal_type(B.__lt__) # N: Revealed type is "def [_AT] (self: _AT`6, other: _AT`6) -> builtins.bool"
994+
reveal_type(C.__lt__) # N: Revealed type is "def [_AT] (self: _AT`7, other: _AT`7) -> builtins.bool"
995+
reveal_type(D.__lt__) # N: Revealed type is "def [_AT] (self: _AT`8, other: _AT`8) -> builtins.bool"
996996

997997
A() < A()
998998
B() < B()

‎test-data/unit/check-selftype.test‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,7 +1484,7 @@ class C:
14841484
return self
14851485
class D(C): ...
14861486

1487-
reveal_type(C.meth) # N: Revealed type is "def [Self <: __main__.C] (self: Self`0) -> builtins.list[Self`0]"
1487+
reveal_type(C.meth) # N: Revealed type is "def [Self <: __main__.C] (self: Self`1) -> builtins.list[Self`1]"
14881488
C.attr # E: Access to generic instance variables via class is ambiguous
14891489
reveal_type(D().meth()) # N: Revealed type is "builtins.list[__main__.D]"
14901490
reveal_type(D().attr) # N: Revealed type is "builtins.list[__main__.D]"
@@ -1793,7 +1793,7 @@ class C:
17931793
def bar(self) -> Self: ...
17941794
def foo(self, x: S) -> Tuple[Self, S]: ...
17951795

1796-
reveal_type(C.foo) # N: Revealed type is "def [Self <: __main__.C, S] (self: Self`0, x: S`-1) -> Tuple[Self`0, S`-1]"
1796+
reveal_type(C.foo) # N: Revealed type is "def [Self <: __main__.C, S] (self: Self`1, x: S`2) -> Tuple[Self`1, S`2]"
17971797
reveal_type(C().foo(42)) # N: Revealed type is "Tuple[__main__.C, builtins.int]"
17981798
[builtins fixtures/tuple.pyi]
17991799

@@ -1903,7 +1903,7 @@ class C:
19031903

19041904
class D(C): ...
19051905

1906-
reveal_type(D.f) # N: Revealed type is "def [T] (T`-1) -> T`-1"
1906+
reveal_type(D.f) # N: Revealed type is "def [T] (T`1) -> T`1"
19071907
reveal_type(D().f) # N: Revealed type is "def () -> __main__.D"
19081908

19091909
[case testTypingSelfOnSuperTypeVarValues]
@@ -2015,3 +2015,24 @@ class Add(Async):
20152015
reveal_type(Add.async_func()) # N: Revealed type is "def (x: builtins.int, y: builtins.int) -> builtins.int"
20162016
reveal_type(Add().async_func()) # N: Revealed type is "def (x: builtins.int, y: builtins.int) -> builtins.int"
20172017
[builtins fixtures/classmethod.pyi]
2018+
2019+
[case testSelfTypeMethodOnClassObject]
2020+
from typing import Self
2021+
2022+
class Object: # Needed to mimic object in typeshed
2023+
ref: Self
2024+
2025+
class Foo:
2026+
def foo(self) -> Self:
2027+
return self
2028+
2029+
class Ben(Object):
2030+
MY_MAP = {
2031+
"foo": Foo.foo,
2032+
}
2033+
@classmethod
2034+
def doit(cls) -> Foo:
2035+
reveal_type(cls.MY_MAP) # N: Revealed type is "builtins.dict[builtins.str, def [Self <: __main__.Foo] (self: Self`4) -> Self`4]"
2036+
foo_method = cls.MY_MAP["foo"]
2037+
return foo_method(Foo())
2038+
[builtins fixtures/isinstancelist.pyi]

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp