Support ClassVar on Struct types - #281
Merged
Merged
Conversation
Previously this was unsupported.
Postponed annotation evaluations are a bit of a tricky issue here. We
currently don't parse type annotations at import time (we do so lazily
on the first decode call). This is nice for two reasons:
- It keeps import times quick. Especially for CLI applications where not
all struct types will be decoded in a single invocation, it pays off
to avoid parsing types unless needed.
- It plays better with recursive structures, since we won't ever parse a
type that isn't fully defined.
However, we do need to know which attributes are/aren't fields at Struct
type definition time, which means we'll have to detect if an attribute
is a `ClassVar` *without* eval-ing the annotation string. This is a bit
tricky to get right (enough), without compromising on performance.
Our solution is to only accept `ClassVar` annotations of the following
forms:
- `ClassVar` and `ClassVar[<type>]`
- `typing.ClassVar` and `typing.ClassVar[<type>]`
Importing either `ClassVar` or `typing` under an alias won't be detected
with postponed annotations. I did a quick survey of projects and
couldn't find a use of `ClassVar` that didn't fall into one of these
cases, so I think we're fine here.
Note that this doesn't rely solely on string comparisons, that's just a
fast-filter to discard the 99% of fields that aren't class variables. So
the following annotation is properly detected to not be a class
variable:
```python
from msgspec import Struct
ClassVar = list
class Example(Struct):
x: ClassVar[int] # msgspec can tell that this isn't a ClassVar
```
With this optimization, `ClassVar` detection has a negligible effect on
struct import performance, while still supporting all the common cases.
jcrist
force-pushed
the
struct-classvar
branch
from
January 28, 2023 21:48
664bc48 to
a38ea1f
Compare
Closed
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Previously this was unsupported.
Postponed annotation evaluations are a bit of a tricky issue here. We currently don't parse type annotations at import time (we do so lazily on the first decode call). This is nice for two reasons:
However, we do need to know which attributes are/aren't fields at Struct type definition time, which means we'll have to detect if an attribute is a
ClassVarwithout eval-ing the annotation string. This is a bit tricky to get right (enough), without compromising on performance.Our solution is to only accept
ClassVarannotations of the following forms:ClassVarandClassVar[<type>]typing.ClassVarandtyping.ClassVar[<type>]Importing either
ClassVarortypingunder an alias won't be detected with postponed annotations. I did a quick survey of projects and couldn't find a use ofClassVarthat didn't fall into one of these cases, so I think we're fine here.Note that this doesn't rely solely on string comparisons, that's just a fast-filter to discard the 99% of fields that aren't class variables. So the following annotation is properly detected to not be a class variable:
With this optimization,
ClassVardetection has a negligible effect on struct import performance, while still supporting all the common cases.Fixes #276.