-
-
Notifications
You must be signed in to change notification settings - Fork 308
Add the __annotations__ attribute to the ClassDef object model.
#2431
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
Add the __annotations__ attribute to the ClassDef object model.
#2431
Conversation
Pylint now does not emit a ``no-member`` error when accessing
``__annotations`` in the following cases:
```
class Test:
print(__annotations__)
```
```
from typing import TypedDict
OtherTypedDict = TypedDict('OtherTypedDict', {'a': int, 'b': str})
print(OtherTypedDict.__annotations__)
```
Closes pylint-dev/pylint#7126
Codecov ReportAll modified and coverable lines are covered by tests β
Additional details and impacted files@@ Coverage Diff @@
## main #2431 +/- ##
==========================================
- Coverage 92.78% 92.72% -0.06%
==========================================
Files 94 94
Lines 11098 10996 -102
==========================================
- Hits 10297 10196 -101
+ Misses 801 800 -1
Flags with carried forward coverage won't be shown. Click here to find out more.
|
DanielNoord
left a comment
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.
I'm fine with the change, but should this behind a version flag?
See https://docs.python.org/3/howto/annotations.html. It seems it was only added to all objects in 3.10+?
I see what you mean @DanielNoord great point. On Python 3.8, for example, we can demonstrate your point (this corresponds to case 2 in the description above): >>> class Fruit:
... pass
...
>>> Fruit().__annotations__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Fruit' object has no attribute '__annotations__'Now, let's look at case 1 on Python 3.8: >>> class Fruit:
... print(__annotations__)
...
{}
>>> Without the fix, Pylint does emit the error here. At this moment I don't know if it's better to make a distinction or keep it as-is but I'm open to ideas. |
|
I think a distinction would be better as that better represents the actual versions right? |
|
I would need to investigate a bit further @DanielNoord but my thinking is that we need the attribute anyway to satisfy both cases; so is the distinction necessary? Possibly but I need to see how that could work |
DanielNoord
left a comment
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.
Thinking about this some more, we shouldn't optimize for old versions. Most people will be running their linter with any of the newer versions anyway. Let's merge! (with one last nit :))
Co-authored-by: DaniΓ«l van Noord <[email protected]>
At a glance, I expect that's because on 3.9 and below, the base class |
jacobtylerwalls
left a comment
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.
Thanks for this!
|
|
||
|
|
||
| def test_class_annotations_typed_dict() -> None: | ||
| """Test that we can access class annotations on various TypedDicts""" |
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.
This is not important at the present moment, but elsewhere I've been encouraged to avoid boilerplate like "test that" and "we", and I think it's a helpful edit ;)
Type of Changes
Description
Add the
__annotations__attribute to theClassDefobject model.Pylint now does not emit a
no-membererror when accessing__annotations__in the following cases:case 1.
case 2.
case 1 is similar to the behaviour of some of the other special attributes:
It turns out that this fix also fixed case 2.
However, it's still unclear why case 2 was only a false positive for Python 3.9 and older π¬
Closes pylint-dev/pylint#7126