@@ -1928,21 +1928,23 @@ from typing_extensions import TypeVar, Self, Protocol
1928
1928
from ty_extensions import is_equivalent_to, static_assert, is_assignable_to, is_subtype_of
1929
1929
1930
1930
class NewStyleClassScoped[T](Protocol):
1931
- def method (self , input : T) -> None : ...
1931
+ def method (self : Self , input : T) -> None : ...
1932
1932
1933
1933
S = TypeVar(" S" )
1934
1934
1935
1935
class LegacyClassScoped (Protocol[S]):
1936
- def method (self , input : S) -> None : ...
1936
+ def method (self : Self , input : S) -> None : ...
1937
1937
1938
- static_assert(is_equivalent_to(NewStyleClassScoped, LegacyClassScoped))
1939
- static_assert(is_equivalent_to(NewStyleClassScoped[int ], LegacyClassScoped[int ]))
1938
+ # TODO : these should pass
1939
+ static_assert(is_equivalent_to(NewStyleClassScoped, LegacyClassScoped)) # error: [static-assert-error]
1940
+ static_assert(is_equivalent_to(NewStyleClassScoped[int ], LegacyClassScoped[int ])) # error: [static-assert-error]
1940
1941
1941
1942
class NominalGeneric[T]:
1942
1943
def method (self , input : T) -> None : ...
1943
1944
1944
1945
def _[T](x: T) -> T:
1945
- static_assert(is_equivalent_to(NewStyleClassScoped[T], LegacyClassScoped[T]))
1946
+ # TODO : should pass
1947
+ static_assert(is_equivalent_to(NewStyleClassScoped[T], LegacyClassScoped[T])) # error: [static-assert-error]
1946
1948
static_assert(is_subtype_of(NominalGeneric[T], NewStyleClassScoped[T]))
1947
1949
static_assert(is_subtype_of(NominalGeneric[T], LegacyClassScoped[T]))
1948
1950
return x
@@ -2016,17 +2018,27 @@ class NominalReturningSelfNotGeneric:
2016
2018
# TODO : should pass
2017
2019
static_assert(is_equivalent_to(LegacyFunctionScoped, NewStyleFunctionScoped)) # error: [static-assert-error]
2018
2020
2019
- static_assert(is_subtype_of(NominalNewStyle, NewStyleFunctionScoped))
2020
- static_assert(is_subtype_of(NominalNewStyle, LegacyFunctionScoped))
2021
+ static_assert(is_assignable_to(NominalNewStyle, NewStyleFunctionScoped))
2022
+ static_assert(is_assignable_to(NominalNewStyle, LegacyFunctionScoped))
2023
+ # TODO : should pass
2024
+ static_assert(is_subtype_of(NominalNewStyle, NewStyleFunctionScoped)) # error: [static-assert-error]
2025
+ # TODO : should pass
2026
+ static_assert(is_subtype_of(NominalNewStyle, LegacyFunctionScoped)) # error: [static-assert-error]
2021
2027
static_assert(not is_assignable_to(NominalNewStyle, UsesSelf))
2022
2028
2023
- static_assert(is_subtype_of(NominalLegacy, NewStyleFunctionScoped))
2024
- static_assert(is_subtype_of(NominalLegacy, LegacyFunctionScoped))
2029
+ static_assert(is_assignable_to(NominalLegacy, NewStyleFunctionScoped))
2030
+ static_assert(is_assignable_to(NominalLegacy, LegacyFunctionScoped))
2031
+ # TODO : should pass
2032
+ static_assert(is_subtype_of(NominalLegacy, NewStyleFunctionScoped)) # error: [static-assert-error]
2033
+ # TODO : should pass
2034
+ static_assert(is_subtype_of(NominalLegacy, LegacyFunctionScoped)) # error: [static-assert-error]
2025
2035
static_assert(not is_assignable_to(NominalLegacy, UsesSelf))
2026
2036
2027
2037
static_assert(not is_assignable_to(NominalWithSelf, NewStyleFunctionScoped))
2028
2038
static_assert(not is_assignable_to(NominalWithSelf, LegacyFunctionScoped))
2029
- static_assert(is_subtype_of(NominalWithSelf, UsesSelf))
2039
+ static_assert(is_assignable_to(NominalWithSelf, UsesSelf))
2040
+ # TODO : should pass
2041
+ static_assert(is_subtype_of(NominalWithSelf, UsesSelf)) # error: [static-assert-error]
2030
2042
2031
2043
# TODO : these should pass
2032
2044
static_assert(not is_assignable_to(NominalNotGeneric, NewStyleFunctionScoped)) # error: [static-assert-error]
@@ -2035,8 +2047,23 @@ static_assert(not is_assignable_to(NominalNotGeneric, UsesSelf))
2035
2047
2036
2048
static_assert(not is_assignable_to(NominalReturningSelfNotGeneric, NewStyleFunctionScoped))
2037
2049
static_assert(not is_assignable_to(NominalReturningSelfNotGeneric, LegacyFunctionScoped))
2050
+
2038
2051
# TODO : should pass
2039
2052
static_assert(not is_assignable_to(NominalReturningSelfNotGeneric, UsesSelf)) # error: [static-assert-error]
2053
+
2054
+ # These test cases are taken from the typing conformance suite:
2055
+ class ShapeProtocolImplicitSelf (Protocol ):
2056
+ def set_scale (self , scale : float ) -> Self: ...
2057
+
2058
+ class ShapeProtocolExplicitSelf (Protocol ):
2059
+ def set_scale (self : Self, scale : float ) -> Self: ...
2060
+
2061
+ class BadReturnType :
2062
+ def set_scale (self , scale : float ) -> int :
2063
+ return 42
2064
+
2065
+ static_assert(not is_assignable_to(BadReturnType, ShapeProtocolImplicitSelf))
2066
+ static_assert(not is_assignable_to(BadReturnType, ShapeProtocolExplicitSelf))
2040
2067
```
2041
2068
2042
2069
## Subtyping of protocols with ` @classmethod ` or ` @staticmethod ` members
0 commit comments