-
-
Notifications
You must be signed in to change notification settings - Fork 381
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
Support ClassVar string annotations #361
Comments
Oh god more string comparisons. 🙈 |
This is the last blocker for the next release. Any ideas how to solve this save expanding the comparison to just ClassVar and call it a day? |
I believe these are our only viable options:
(Though any good linter would catch that) |
Can I haz a non-terrible third option? :'( |
The non-gross option is to import typing and use How about this: if user code is using annotations and that annotation is a string, then we import typing and use Let me create a pull request to that effect. |
OK, so I tried the above and it seems it's a nuclear option since it forces all annotations to be evaluated. This is what I wanted to avoid with Making it more intelligent would be possible with I looked what So my pull request is going with Euresti's Option 2. This is going to be enough for 99.9% users. |
Also updated the docstring to reflect why exactly we're doing what we're doing. The previous comment was incorrect (`typing` is already imported in applications using annotations). I added `t.ClassVar` as well which is the only third alternative import I found in use for typing-related classes. Fixes python-attrs#361
I look forward to hear from the other 0.1% :D But yeah, it’s the only realistic option I’m afraid. |
Well, we can do the correct thing and slow things down for 99.9% while making that 0.1% not even notice. Note that our avoidance of evaluation with Admittedly, ClassVar is an ugly edge case. |
I’m not arguing, just lamenting. |
* Support `from typing import ClassVar` Also updated the docstring to reflect why exactly we're doing what we're doing. The previous comment was incorrect (`typing` is already imported in applications using annotations). I added `t.ClassVar` as well which is the only third alternative import I found in use for typing-related classes. Fixes #361 * Tests, docstrings, et al.
The following doesn't work in Python 3.7
Because
ClassVar[int]
will be'ClassVar[int]
inA.__annotations__
and soreturn str(annot).startswith("typing.ClassVar")
returnsFalse
.You can repro without 3.7 using this:
Note: It's weird I'm not giving it a value but if I do then attrs will think it's a default value and the code won't error out.
Oh and the following works fine:
The text was updated successfully, but these errors were encountered: