Skip to content

Commit 9152182

Browse files
authored
New semantic analyser: Fix special attrs decorators (#7083)
Fixes #6953 I first thought about actually not modifying them in `attrs` plugin, but this is non-trivial, plus there are few places apart from the plugin where we modify decorators. So for now I just restore them after each iteration (in the same way we do this for overloads and other things).
1 parent bbd732f commit 9152182

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

mypy/newsemanal/semanal.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,8 @@ def check_function_signature(self, fdef: FuncItem) -> None:
900900

901901
def visit_decorator(self, dec: Decorator) -> None:
902902
self.statement = dec
903+
# TODO: better don't modify them at all.
904+
dec.decorators = dec.original_decorators.copy()
903905
dec.func.is_conditional = self.block_depth[-1] > 0
904906
if not dec.is_overload:
905907
self.add_symbol(dec.name(), dec, dec)

test-data/unit/check-attr.test

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,3 +1049,35 @@ class B: # E: Function is missing a type annotation for one or more arguments
10491049

10501050
reveal_type(B) # N: Revealed type is 'def (x: Any) -> __main__.B'
10511051
[builtins fixtures/list.pyi]
1052+
1053+
[case testAttrsDefaultDecoratorDeferred]
1054+
defer: Yes
1055+
1056+
import attr
1057+
@attr.s
1058+
class C(object):
1059+
x: int = attr.ib(default=1)
1060+
y: int = attr.ib()
1061+
@y.default
1062+
def inc(self):
1063+
return self.x + 1
1064+
1065+
class Yes: ...
1066+
[builtins fixtures/list.pyi]
1067+
1068+
[case testAttrsValidatorDecoratorDeferred]
1069+
defer: Yes
1070+
1071+
import attr
1072+
@attr.s
1073+
class C(object):
1074+
x = attr.ib()
1075+
@x.validator
1076+
def check(self, attribute, value):
1077+
if value > 42:
1078+
raise ValueError("x must be smaller or equal to 42")
1079+
C(42)
1080+
C(43)
1081+
1082+
class Yes: ...
1083+
[builtins fixtures/exception.pyi]

0 commit comments

Comments
 (0)