-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Check property decorators stricter #19313
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: master
Are you sure you want to change the base?
Check property decorators stricter #19313
Conversation
return False | ||
if not isinstance(deco.expr, NameExpr) or deco.expr.name != property_name: | ||
return False | ||
if deco.name not in {"setter", "deleter"}: |
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.
if deco.name not in {"setter", "deleter"}: | |
if deco.name not in {"getter", "setter", "deleter"}: |
@x.getter
is valid at runtime (albeit not common). We probably don't want to emit an "Only supported top decorators are ..." error for it.
I guess we don't have any tests for it 🤷
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.
Hm. This definitely needs a test, but I think rejecting @prop.getter
is a reasonable thing to do: it should override the existing getter, supporting it properly won't be trivial, so explicitly saying "we don't support redefined getters" is not that bad (given that such usage should be really rare, 0 primer hits)
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.
By following this github search, I identified two common patterns with @prop.getter
(excluding partial subclass overrides as they don't trigger this code and aren't supported anyway):
-
Mistake with
getter
instead ofsetter
(followed by signaturedef (self, value)
) -
Obscure way of defining properties:
class A: @property def foo(self): pass @foo.getter def foo(self): ... # Some logic here @foo.setter def foo(self, new_foo): ...
Honestly this makes my eyes bleed. I don't know how popular this is as regex code search is limited to 5 pages, and the file count estimate is never close to reality, they don't actually regex-match whole github. I propose to accept the risk and wait - if we get a couple of tickets about this in the next release, we can whitelist
getter
as well.
As a secondary reference, pyright
handles this in the most confusing manner possible, we don't want to replicate that behaviour. It silently accepts any @prop.getter
even with incompatible return type and ignores it completely - see my ticket microsoft/pyright#10633
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
Fixes #19312. Fixes #18327.
Only accept
@current_prop_name.{setter,deleter}
as property-related decorators.