-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Allow both Final[ClassVar[T]] and ClassVar[Final[T]] in dataclass context #8676
Comments
… a dataclass. Previously, the `Final` qualifier needed to be the outermost. This addresses #8676.
Yeah, I don't see anything in the typing spec that indicates This will be included in the next release. |
I just realized now that while it makes sense from a typing point of view to allow them to combine in both directions, since both It's up to you if you still consider that this is worth including in the next release. IMO this change in pyright is the right one and a bug report should be opened against dataclasses to consider What is your opinion on this? |
Ah, then I will revert the change. Pyright should match the runtime behavior. |
Would it be at least possible to give a better error message that suggests what is wrong and what to do about it (i.e. reverse the order of the two) so people don't go hunting on google for answers? I know I wasted a lot of time looking for this because it's not intuitive that they need to be in a specific order, nor is this mandated by the typing spec. Also do you still think it's worth opening a bug report with dataclasses and coming back to this fix when they address it (if they do)? |
I found some discussion about this and someone already mentioned this there, so they seem to be aware of it: So I won't open an issue for the time being, see what that discussion concludes. |
Is your feature request related to a problem? Please describe.
I added some
size: Final[ClassVar[int]] = 256
within a dataclass and pyright gave an error thatClassVar is not allowed in this context
, which was unclear why it failed, considering that this was recently allowed by the typing spec and was implemented in pyright not long ago.After some googling and reading some discussions on the subject I found that people have mentioned both forms
Final[ClassVar[T]]
andClassVar[Final[T]]
during the discussions about dataclasses and class variables with final values, so I tried the other form and it worked.Describe the solution you’d like
I would like to suggest that both forms be accepted. The runtime accepts both and the order in which they are written is just a matter of personal preference. I myself find
Final[ClassVar[T]]
to be more readable, that's why I wrote it like that in the first place. Accepting both would definitely reduce the friction and confusion of why it doesn't work for people that writeFinal[ClassVar[T]]
because it sound more readable to them.The text was updated successfully, but these errors were encountered: