-
Notifications
You must be signed in to change notification settings - Fork 295
fix “Decorator @X can only be used on methods” is too strict #2610 #2891
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -246,23 +246,68 @@ testcase!( | |||||||
| test_invalid_top_level_function_decorators, | ||||||||
| r#" | ||||||||
| from typing import * | ||||||||
| from abc import abstractstaticmethod, abstractmethod # E: `abstractstaticmethod` is deprecated | ||||||||
| from abc import abstractmethod | ||||||||
| from enum import member, nonmember | ||||||||
|
|
||||||||
| @member # E: can only be used on methods | ||||||||
| @nonmember # E: can only be used on methods | ||||||||
| @abstractmethod # E: can only be used on methods | ||||||||
| @staticmethod # E: can only be used on methods | ||||||||
| @classmethod # E: can only be used on methods | ||||||||
| @abstractstaticmethod # E: can only be used on methods | ||||||||
| @property # E: can only be used on methods | ||||||||
| @final # E: can only be used on methods | ||||||||
| @override # E: can only be used on methods | ||||||||
| def f(x: int) -> int: | ||||||||
| return x | ||||||||
| "#, | ||||||||
| ); | ||||||||
|
|
||||||||
| testcase!( | ||||||||
| test_top_level_descriptor_decorators, | ||||||||
| r#" | ||||||||
| from typing import assert_type | ||||||||
|
|
||||||||
| def make_property(): | ||||||||
| @property | ||||||||
| def x(self: "Point") -> int: | ||||||||
| return self._x | ||||||||
|
|
||||||||
| return x | ||||||||
|
|
||||||||
|
|
||||||||
| class Point: | ||||||||
| def __init__(self, x: int): | ||||||||
| self._x = x | ||||||||
|
|
||||||||
| x = make_property() | ||||||||
|
|
||||||||
|
|
||||||||
| assert_type(Point(1).x, int) | ||||||||
|
|
||||||||
| @staticmethod | ||||||||
| def utility(x: int) -> int: | ||||||||
| return x * 2 | ||||||||
|
|
||||||||
|
|
||||||||
| class Helper: | ||||||||
| compute = utility | ||||||||
|
|
||||||||
|
|
||||||||
| assert_type(Helper.compute(5), int) | ||||||||
|
||||||||
| assert_type(Helper.compute(5), int) | |
| assert_type(Helper.compute(5), int) | |
| assert_type(Helper().compute(5), int) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR description mentions top-level
@cached_propertyis now allowed, but the added regression test covers@property,@staticmethod, and@classmethodonly. Adding a top-level@cached_propertyfactory/assignment case here would prevent regressions for that specific decorator.