Skip to content

Commit

Permalink
Test import from typing_extensions
Browse files Browse the repository at this point in the history
Also, mark the import from typing as Python 3.12 only. And change the
message a bit.
  • Loading branch information
tmke8 committed May 8, 2023
1 parent 8ea05d6 commit 10cd4aa
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
2 changes: 1 addition & 1 deletion mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -1496,7 +1496,7 @@ def cant_assign_to_classvar(self, name: str, context: Context) -> None:
def no_overridable_method(self, name: str, context: Context) -> None:
self.fail(
f'Method "{name}" is marked as an override, '
"but no base method with this name was found",
"but no base method was found with this name",
context,
)

Expand Down
32 changes: 25 additions & 7 deletions test-data/unit/check-functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -2726,12 +2726,13 @@ g: Callable[[Union[Sequence[TI], Sequence[TS]]], None]
f = g

[case explicitOverride]
# flags: --python-version 3.12
from typing import override

class A:
def f(self, x: int) -> str: pass
@override
def g(self, x: int) -> str: pass # E: Method "g" is marked as an override, but no base method with this name was found
def g(self, x: int) -> str: pass # E: Method "g" is marked as an override, but no base method was found with this name

class B(A):
@override
Expand All @@ -2755,6 +2756,7 @@ class F(E):
[builtins fixtures/tuple.pyi]

[case explicitOverrideStaticmethod]
# flags: --python-version 3.12
from typing import override

class A:
Expand All @@ -2767,15 +2769,15 @@ class B(A):
def f(x: int) -> str: pass
@override
@staticmethod
def g(x: int) -> str: pass # E: Method "g" is marked as an override, but no base method with this name was found
def g(x: int) -> str: pass # E: Method "g" is marked as an override, but no base method was found with this name

class C(A): # inverted order of decorators
@override
@staticmethod
def f(x: int) -> str: pass
@override
@staticmethod
def g(x: int) -> str: pass # E: Method "g" is marked as an override, but no base method with this name was found
def g(x: int) -> str: pass # E: Method "g" is marked as an override, but no base method was found with this name

class D(A):
@staticmethod
Expand All @@ -2787,6 +2789,7 @@ class D(A):
[builtins fixtures/callable.pyi]

[case explicitOverrideClassmethod]
# flags: --python-version 3.12
from typing import override

class A:
Expand All @@ -2799,15 +2802,15 @@ class B(A):
def f(cls, x: int) -> str: pass
@override
@classmethod
def g(cls, x: int) -> str: pass # E: Method "g" is marked as an override, but no base method with this name was found
def g(cls, x: int) -> str: pass # E: Method "g" is marked as an override, but no base method was found with this name

class C(A): # inverted order of decorators
@override
@classmethod
def f(cls, x: int) -> str: pass
@override
@classmethod
def g(cls, x: int) -> str: pass # E: Method "g" is marked as an override, but no base method with this name was found
def g(cls, x: int) -> str: pass # E: Method "g" is marked as an override, but no base method was found with this name

class D(A):
@classmethod
Expand All @@ -2819,6 +2822,7 @@ class D(A):
[builtins fixtures/callable.pyi]

[case explicitOverrideProperty]
# flags: --python-version 3.12
from typing import override

class A:
Expand All @@ -2831,15 +2835,15 @@ class B(A):
def f(self) -> str: pass
@override
@property
def g(self) -> str: pass # E: Method "g" is marked as an override, but no base method with this name was found
def g(self) -> str: pass # E: Method "g" is marked as an override, but no base method was found with this name

class C(A): # inverted order of decorators
@override
@property
def f(self) -> str: pass
@override
@property
def g(self) -> str: pass # E: Method "g" is marked as an override, but no base method with this name was found
def g(self) -> str: pass # E: Method "g" is marked as an override, but no base method was found with this name

class D(A):
@property
Expand All @@ -2849,6 +2853,7 @@ class D(A):
[typing fixtures/typing-full.pyi]

[case invalidExplicitOverride]
# flags: --python-version 3.12
from typing import override

@override # E: "override" used with a non-method
Expand All @@ -2864,6 +2869,7 @@ def g() -> None:
[builtins fixtures/tuple.pyi]

[case explicitOverrideSpecialMethods]
# flags: --python-version 3.12
from typing import override

class A:
Expand All @@ -2878,3 +2884,15 @@ class C:
def __init__(self, a: int) -> None: pass
[typing fixtures/typing-full.pyi]
[builtins fixtures/tuple.pyi]

[case explicitOverrideFromExtensions]
from typing_extensions import override

class A:
def f(self, x: int) -> str: pass

class B(A):
@override
def f2(self, x: int) -> str: pass # E: Method "f2" is marked as an override, but no base method was found with this name
[typing fixtures/typing-full.pyi]
[builtins fixtures/tuple.pyi]
2 changes: 2 additions & 0 deletions test-data/unit/lib-stub/typing_extensions.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,6 @@ def dataclass_transform(
**kwargs: Any,
) -> Callable[[T], T]: ...

def override(__arg: _T) -> _T: ...

_FutureFeatureFixture = 0

0 comments on commit 10cd4aa

Please sign in to comment.