Skip to content

Commit

Permalink
[stubgen] Include simple decorators in stub files (#18489)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cdce8p authored Jan 25, 2025
1 parent ebafbce commit 3ced11a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions mypy/stubgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,9 @@ def process_decorator(self, o: Decorator) -> None:
elif fullname in DATACLASS_TRANSFORM_NAMES:
p = AliasPrinter(self)
self._decorators.append(f"@{decorator.accept(p)}")
elif isinstance(decorator, (NameExpr, MemberExpr)):
p = AliasPrinter(self)
self._decorators.append(f"@{decorator.accept(p)}")

def get_fullname(self, expr: Expression) -> str:
"""Return the expression's full name."""
Expand Down
14 changes: 14 additions & 0 deletions test-data/unit/stubgen.test
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,24 @@ class A: ...
class B(A): ...

[case testDecoratedFunction]
import x

@decorator
def foo(x): ...

@x.decorator
def bar(x): ...

@decorator(x=1, y={"a": 1})
def foo_bar(x): ...
[out]
import x

@decorator
def foo(x) -> None: ...
@x.decorator
def bar(x) -> None: ...
def foo_bar(x) -> None: ...

[case testMultipleAssignment]
x, y = 1, 2
Expand Down

0 comments on commit 3ced11a

Please sign in to comment.