Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stubgen] Include simple decorators in stub files #18489

Merged
merged 1 commit into from
Jan 25, 2025
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
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): ...
Comment on lines +349 to +350
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Decorator CallExpr are left for a future PR. For type checker to be able to infer the correct type, all arguments need to be passed along as well.

[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
Loading