Skip to content

Commit 3ced11a

Browse files
authored
[stubgen] Include simple decorators in stub files (#18489)
Stubgen historically only included a selected number of decorators in the generated stubs. I couldn't find the actual reason for it, however it's likely fair to assume that decorator typing only started being possible with PEP 612 thus most had been untyped previously. As it's fairly simple to annotate decorators with `ParamSpec` now, it's probably fair to include them in the stub file now.
1 parent ebafbce commit 3ced11a

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

mypy/stubgen.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,9 @@ def process_decorator(self, o: Decorator) -> None:
755755
elif fullname in DATACLASS_TRANSFORM_NAMES:
756756
p = AliasPrinter(self)
757757
self._decorators.append(f"@{decorator.accept(p)}")
758+
elif isinstance(decorator, (NameExpr, MemberExpr)):
759+
p = AliasPrinter(self)
760+
self._decorators.append(f"@{decorator.accept(p)}")
758761

759762
def get_fullname(self, expr: Expression) -> str:
760763
"""Return the expression's full name."""

test-data/unit/stubgen.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,24 @@ class A: ...
338338
class B(A): ...
339339

340340
[case testDecoratedFunction]
341+
import x
342+
341343
@decorator
342344
def foo(x): ...
345+
346+
@x.decorator
347+
def bar(x): ...
348+
349+
@decorator(x=1, y={"a": 1})
350+
def foo_bar(x): ...
343351
[out]
352+
import x
353+
354+
@decorator
344355
def foo(x) -> None: ...
356+
@x.decorator
357+
def bar(x) -> None: ...
358+
def foo_bar(x) -> None: ...
345359

346360
[case testMultipleAssignment]
347361
x, y = 1, 2

0 commit comments

Comments
 (0)