Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Test import from typing_extensions
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
commit 10cd4aa0013da90dacecdb203561a690112ca79d
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
Comment on lines +2916 to +2918
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure whether to allow it on __init__.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't seem very useful, but I also don't see a reason to explicitly disallow it. @stroxler any thoughts?

[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]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't see a test case that uses overloaded methods (using the @overload decorator). Can you test overriding when it involves overloaded methods as well?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the test and also added support for overloads decorated with @override. The rule is now that if any overload item has the decorator, the whole overload is treated as an explicit override.

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