Skip to content

get_class_decorator_hook_2: fix fullname #16063

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1826,8 +1826,8 @@ def apply_class_plugin_hooks(self, defn: ClassDef) -> None:

def get_fullname_for_hook(self, expr: Expression) -> str | None:
if isinstance(expr, CallExpr):
return self.get_fullname_for_hook(expr.callee)
elif isinstance(expr, IndexExpr):
expr = expr.callee
if isinstance(expr, IndexExpr):
Comment on lines 1828 to +1830
Copy link
Contributor Author

@ikonst ikonst Sep 7, 2023

Choose a reason for hiding this comment

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

The culprit - see testClassDecoratorHook2_incorrect for repro.

Basically, if we have

@my_decorator()()
 ^^^^^^^^^^^^^^^^

then I feel we ought to use the whole callable, and not recurse in, but maybe I'm missing some use case?

return self.get_fullname_for_hook(expr.base)
elif isinstance(expr, RefExpr):
if expr.fullname:
Expand Down
47 changes: 47 additions & 0 deletions test-data/unit/check-custom-plugin.test
Original file line number Diff line number Diff line change
Expand Up @@ -1029,3 +1029,50 @@ reveal_type(1) # N: Revealed type is "Literal[1]?"
[file mypy.ini]
\[mypy]
plugins=<ROOT>/test-data/unit/plugins/custom_errorcode.py

[case testClassDecoratorHook2]
# flags: --config-file tmp/mypy.ini
from typing import Any, TypeVar
T = TypeVar('T')

def my_decorator(t: Any) -> Any:
raise

@my_decorator
class C:
pass

obj = C()
reveal_type(obj.magic) # N: Revealed type is "builtins.str"
[file mypy.ini]
\[mypy]
plugins=<ROOT>/test-data/unit/plugins/class_decorator_hook_2.py

[typing fixtures/typing-medium.pyi]

[case testClassDecoratorHook2_incorrect]
# flags: --config-file tmp/mypy.ini
from typing import Any, TypeVar
T = TypeVar('T')

def my_decorator(t: Any) -> Any:
raise

@my_decorator()()
Copy link
Member

Choose a reason for hiding this comment

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

What will happen for @my_decorator(1)()?

class C:
pass

obj = C()
_ = obj.magic
[out]
main:8: error: invalid syntax

[out version>=3.9]
main:8: error: Missing positional argument "t" in call to "my_decorator"
main:13: error: "C" has no attribute "magic"

[file mypy.ini]
\[mypy]
plugins=<ROOT>/test-data/unit/plugins/class_decorator_hook_2.py

[typing fixtures/typing-medium.pyi]
19 changes: 19 additions & 0 deletions test-data/unit/check-dataclasses.test
Original file line number Diff line number Diff line change
Expand Up @@ -2515,3 +2515,22 @@ a: MyDataclass
b = [a, a] # trigger joining the types

[builtins fixtures/dataclasses.pyi]

[case testDataclassesInvalidDecorator]
from dataclasses import dataclass

@dataclass()()
class C:
a: str

reveal_type(C)

[out]
main:3: error: invalid syntax

[out version>=3.9]
main:3: error: Too few arguments
main:4: error: Cannot instantiate type "Type[Never]"
main:7: note: Revealed type is "def () -> __main__.C"

[builtins fixtures/dataclasses.pyi]
22 changes: 22 additions & 0 deletions test-data/unit/plugins/class_decorator_hook_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from __future__ import annotations

from typing import Callable

from mypy.plugin import ClassDefContext, Plugin
from mypy.plugins.common import add_attribute_to_class


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


def transform_hook(ctx: ClassDefContext) -> bool:
add_attribute_to_class(ctx.api, ctx.cls, 'magic', ctx.api.named_type('builtins.str'))
return True


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