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
Incorrect treatment of Final class variables when auto_attribs=True #784
Comments
I sounds like one line patch if Final is really equivalent to ClassVar. @euresti ? (I’m so sorry) |
Ish. It is, only if there's a default value assigned. |
Hmmm that complicates things of course. |
Hmm. I think that PEP and attrs are in a little bit of a fight:
However to atts, the assignment is a default value not an actual assignment. So:
is a bit ambiguous. Attrs can't know if you mean the A.x is an instance variable that is Final or a class variable. Also, looks like Python's dataclasses treat Final in the same way. |
Ok looks like I said exactly what you said in the bug report. (Learn to read David) |
Use If we require that then we have to ask ourselves -- is that a breaking change? Part of me says yes because I bet you people out there are relying on that behavior and are unaware of that clause in PEP 591. (To be fair, the current behavior is logical.) At the same time, it's technically incorrect behavior and people are relying on a bug.
Oh dear... |
Okay so after some thinking, considering that Python's native dataclasses have the same behavior as |
Hey, I couldn't find your ticket on bugs.pythonl.org, could you reference it here? It appears that in python 3.10 the behaviour of dataclass is indeed still like this, yet it disagrees with the documentation of Final. I did just run into this issue, and I would like to express my support to clarify that behaviour. |
It depends on which you consider the source of truth: the behavior of python's dataclasses or what's explicitly spelled out in PEP 591. To me it looks like the latter is the formal specification, and I really don't care if following the specification in attrs would contradict the behavior in dataclasses. As a workaround though, I managed to exclude Final class variables from being considered instance variables by dispensing with
Obviously not the smartest solution, but it works. |
@burnpanck Sorry, I completely forgot about this! |
@burnpanck Looks like someone filed a ticket with Python a few months after I filed this ticket. The thread died but it seems like everyone's opposed to changing Python's behavior, and instead prefer modifying the PEP. |
Hey @dargueta, thanks for following up on this. In fact, that ticket has been migrated to GitHub, and it seems there is some recent movement there which may bring |
attrs
appears to treat an initialized final class variable as an instance variable. I'd expect that this would follow PEP 591, specifically this sentence in the "Semantics and Examples" section:However, that doesn't appear to be the case. Suppose we have the following code, where
x
is intended to be an "immutable" class variable andy
is an instance variable:The code crashes with an error:
This indicates that
x
isn't being properly treated as a class variable and skipped over. Indeed, if we reverse the order of the declarations, it "works" in the sense that it doesn't crash, but it generates a class with incorrect behavior.As you can see,
x
is unexpectedly an instance variable. If we're conforming to PEP 591, the correct behavior here would be to build a class equivalent to the following:Environment:
attrs==20.3.0
The text was updated successfully, but these errors were encountered: