-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Attrs/tweak magic attribute (#13522)
This is an attempt to fix the attrs magic attribute handling. The attrs magic attribute functionality was added by me a few months ago, but inexpertly. Since we're starting to see usage in the wild, I'm attempting to fix it. Context: every attrs class has a magic attribute, a final classvar available under `Class.__attrs_attrs__`. This is an instance of an ad-hoc tuple subclass, and it contains an instance of `attrs.Attribute[T]` for each class attribute. The reason it's a tuple subclass is that is has properties for each attribute, so instead of `Class.__attrs_attrs__[0]` we can use `Class.__attrs_attrs__.a`. Users aren't really supposed to use this attribute directly, but through `attrs.fields()` (a subject of a future PR), but having Mypy know about it is useful for f.e. protocols (and the newest release of attrs does contain a protocol using the magic attribute). When I implemented the magic attribute here initially, I set it as `no_serialize=True`. This is incorrect for some use cases. My first issue: is my approach OK? Like I mentioned, the type of each magic attribute is an instance of an ad-hoc tuple subclass. I need to stash this subclass somewhere so cached runs work properly. Right now I'm trying to serialize it under `Class.__Class_AttrsAttributes__`; maybe there's a better way? I needed to incorporate the fully qualified class name in the attribute name so Mypy doesn't complain about incompatible fields when subclassing. My second issue is some incremental, fine-grained tests are failing now and I cannot figure out how to fix them, so it'd be great to get some advice here too. Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
- Loading branch information
Showing
5 changed files
with
86 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters