Skip to content

Commit

Permalink
Do not report plugin-generated methods with explicit-override (#17433)
Browse files Browse the repository at this point in the history
Closes typeddjango/django-stubs#2226
Closes #17417
Closes #17370
Closes #17224
This is an alternative to #17418

Thanks a lot to @sterliakov, I took a dataclasses test case from #17370

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
sobolevn and pre-commit-ci[bot] committed Jun 24, 2024
1 parent 18945af commit 620e281
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
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

0 comments on commit 620e281

Please sign in to comment.