Skip to content

Commit 6bcf3f2

Browse files
committed
wip
1 parent ed9b899 commit 6bcf3f2

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed

mypy/semanal.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1826,8 +1826,8 @@ def apply_class_plugin_hooks(self, defn: ClassDef) -> None:
18261826

18271827
def get_fullname_for_hook(self, expr: Expression) -> str | None:
18281828
if isinstance(expr, CallExpr):
1829-
return self.get_fullname_for_hook(expr.callee)
1830-
elif isinstance(expr, IndexExpr):
1829+
expr = expr.callee
1830+
if isinstance(expr, IndexExpr):
18311831
return self.get_fullname_for_hook(expr.base)
18321832
elif isinstance(expr, RefExpr):
18331833
if expr.fullname:

test-data/unit/check-custom-plugin.test

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,3 +1029,43 @@ reveal_type(1) # N: Revealed type is "Literal[1]?"
10291029
[file mypy.ini]
10301030
\[mypy]
10311031
plugins=<ROOT>/test-data/unit/plugins/custom_errorcode.py
1032+
1033+
[case testClassDecoratorHook2]
1034+
# flags: --config-file tmp/mypy.ini
1035+
from typing import Any, TypeVar
1036+
T = TypeVar('T')
1037+
1038+
def my_decorator(t: Any) -> Any:
1039+
raise
1040+
1041+
@my_decorator
1042+
class C:
1043+
pass
1044+
1045+
obj = C()
1046+
reveal_type(obj.magic) # N: Revealed type is "builtins.str"
1047+
[file mypy.ini]
1048+
\[mypy]
1049+
plugins=<ROOT>/test-data/unit/plugins/class_decorator_hook_2.py
1050+
1051+
[typing fixtures/typing-medium.pyi]
1052+
1053+
[case testClassDecoratorHook2_incorrect]
1054+
# flags: --config-file tmp/mypy.ini
1055+
from typing import Any, TypeVar
1056+
T = TypeVar('T')
1057+
1058+
def my_decorator(t: Any) -> Any:
1059+
raise
1060+
1061+
@my_decorator()() # E: Missing positional argument "t" in call to "my_decorator"
1062+
class C:
1063+
pass
1064+
1065+
obj = C()
1066+
_ = obj.magic # E: "C" has no attribute "magic"
1067+
[file mypy.ini]
1068+
\[mypy]
1069+
plugins=<ROOT>/test-data/unit/plugins/class_decorator_hook_2.py
1070+
1071+
[typing fixtures/typing-medium.pyi]

test-data/unit/check-dataclasses.test

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2515,3 +2515,14 @@ a: MyDataclass
25152515
b = [a, a] # trigger joining the types
25162516

25172517
[builtins fixtures/dataclasses.pyi]
2518+
2519+
[case testDataclassesInvalidDecorator]
2520+
from dataclasses import dataclass
2521+
2522+
@dataclass()() # E: Too few arguments
2523+
class C: # E: Cannot instantiate type "Type[Never]"
2524+
a: str
2525+
2526+
reveal_type(C) # N: Revealed type is "def () -> __main__.C"
2527+
2528+
[builtins fixtures/dataclasses.pyi]
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from __future__ import annotations
2+
3+
from typing import Callable
4+
5+
from mypy.plugin import ClassDefContext, Plugin
6+
from mypy.plugins.common import add_attribute_to_class
7+
8+
9+
class MyPlugin(Plugin):
10+
def get_class_decorator_hook_2(self, fullname: str) -> Callable[[ClassDefContext], bool] | None:
11+
if fullname == "__main__.my_decorator":
12+
return transform_hook
13+
return None
14+
15+
16+
def transform_hook(ctx: ClassDefContext) -> bool:
17+
add_attribute_to_class(ctx.api, ctx.cls, 'magic', ctx.api.named_type('builtins.str'))
18+
return True
19+
20+
21+
def plugin(version: str) -> type[MyPlugin]:
22+
return MyPlugin

0 commit comments

Comments
 (0)