Skip to content
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
2 changes: 2 additions & 0 deletions mypy/newsemanal/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,8 @@ def check_function_signature(self, fdef: FuncItem) -> None:

def visit_decorator(self, dec: Decorator) -> None:
self.statement = dec
# TODO: better don't modify them at all.
dec.decorators = dec.original_decorators.copy()
dec.func.is_conditional = self.block_depth[-1] > 0
if not dec.is_overload:
self.add_symbol(dec.name(), dec, dec)
Expand Down
32 changes: 32 additions & 0 deletions test-data/unit/check-attr.test
Original file line number Diff line number Diff line change
Expand Up @@ -1049,3 +1049,35 @@ class B: # E: Function is missing a type annotation for one or more arguments

reveal_type(B) # N: Revealed type is 'def (x: Any) -> __main__.B'
[builtins fixtures/list.pyi]

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you check that there are tests that run multiple semantic passes with various kinds of decorators? There is a small risk that this might break something.

Copy link
Member Author

Choose a reason for hiding this comment

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

I run all test cases where top-levels processed twice (second time gives an extra iteration). I have found no failures related to this change. I however added another attrs specific decorator test case just in case.

[case testAttrsDefaultDecoratorDeferred]
defer: Yes

import attr
@attr.s
class C(object):
x: int = attr.ib(default=1)
y: int = attr.ib()
@y.default
def inc(self):
return self.x + 1

class Yes: ...
[builtins fixtures/list.pyi]

[case testAttrsValidatorDecoratorDeferred]
defer: Yes

import attr
@attr.s
class C(object):
x = attr.ib()
@x.validator
def check(self, attribute, value):
if value > 42:
raise ValueError("x must be smaller or equal to 42")
C(42)
C(43)

class Yes: ...
[builtins fixtures/exception.pyi]