Skip to content

Do not report plugin-generated methods with explicit-override #17433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 8 additions & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1938,8 +1938,15 @@ def check_explicit_override_decorator(
found_method_base_classes: list[TypeInfo] | None,
context: Context | None = None,
) -> None:
plugin_generated = False
if defn.info and (node := defn.info.get(defn.name)) and node.plugin_generated:
# Do not report issues for plugin generated nodes,
# they can't realistically use `@override` for their methods.
plugin_generated = True

if (
found_method_base_classes
not plugin_generated
and found_method_base_classes
and not defn.is_explicit_override
and defn.name not in ("__init__", "__new__")
and not is_private(defn.name)
Expand Down
33 changes: 33 additions & 0 deletions test-data/unit/check-custom-plugin.test
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,39 @@ reveal_type(my_class.stmethod) # N: Revealed type is "Overload(def (arg: builti
\[mypy]
plugins=<ROOT>/test-data/unit/plugins/add_overloaded_method.py

[case testAddMethodPluginExplicitOverride]
# flags: --python-version 3.12 --config-file tmp/mypy.ini
from typing import override, TypeVar

T = TypeVar('T', bound=type)

def inject_foo(t: T) -> T:
# Imitates:
# t.foo_implicit = some_method
return t

class BaseWithoutFoo: pass

@inject_foo
class ChildWithFoo(BaseWithoutFoo): pass
reveal_type(ChildWithFoo.foo_implicit) # N: Revealed type is "def (self: __main__.ChildWithFoo)"

@inject_foo
class SomeWithFoo(ChildWithFoo): pass
reveal_type(SomeWithFoo.foo_implicit) # N: Revealed type is "def (self: __main__.SomeWithFoo)"

class ExplicitOverride(SomeWithFoo):
@override
def foo_implicit(self) -> None: pass

class ImplicitOverride(SomeWithFoo):
def foo_implicit(self) -> None: pass # E: Method "foo_implicit" is not using @override but is overriding a method in class "__main__.SomeWithFoo"
[file mypy.ini]
\[mypy]
plugins=<ROOT>/test-data/unit/plugins/add_method.py
enable_error_code = explicit-override
[typing fixtures/typing-override.pyi]

[case testCustomErrorCodePlugin]
# flags: --config-file tmp/mypy.ini --show-error-codes
def main() -> int:
Expand Down
14 changes: 14 additions & 0 deletions test-data/unit/check-dataclasses.test
Original file line number Diff line number Diff line change
Expand Up @@ -2475,3 +2475,17 @@ class Base:
class Child(Base):
y: int
[builtins fixtures/dataclasses.pyi]


[case testDataclassInheritanceWorksWithExplicitOverridesAndOrdering]
# flags: --enable-error-code explicit-override
from dataclasses import dataclass

@dataclass(order=True)
class Base:
x: int

@dataclass(order=True)
class Child(Base):
y: int
[builtins fixtures/dataclasses.pyi]
23 changes: 23 additions & 0 deletions test-data/unit/plugins/add_method.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from __future__ import annotations

from typing import Callable

from mypy.plugin import ClassDefContext, Plugin
from mypy.plugins.common import add_method
from mypy.types import NoneType


class AddOverrideMethodPlugin(Plugin):
def get_class_decorator_hook_2(self, fullname: str) -> Callable[[ClassDefContext], bool] | None:
if fullname == "__main__.inject_foo":
return add_extra_methods_hook
return None


def add_extra_methods_hook(ctx: ClassDefContext) -> bool:
add_method(ctx, "foo_implicit", [], NoneType())
return True


def plugin(version: str) -> type[AddOverrideMethodPlugin]:
return AddOverrideMethodPlugin
Loading