Support typing.Final annotations - #354
Merged
Merged
Conversation
This adds support for `typing.Final` annotations. `Final` can be used to wrap an existing field annotation, marking it as a field that can't be modified once it's initialized. This has the same semantics as `frozen=True`, but only for a single field, and not enforced at runtime. The `Frozen` annotation may be used to wrap field annotations on any object-like type we support (`msgspec.Struct`, `attrs`, or `dataclasses`).
Member
Author
|
A quick benchmarks trying to measure just the type annotation parsing overhead. For most usage patterns the type annotation parsing happens only once and is cached for subsequent usage, so I wouldn't expect this perf increase to matter to most users. Still, speeding this up can decrease startup times. Main Branch In [1]: import msgspec
In [2]: typ = dict[str, list[int]]
In [3]: msg = b'{"x":[1]}'
In [4]: %timeit msgspec.json.decode(msg, type=typ)
1.78 µs ± 18.2 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)This PR In [1]: import msgspec
In [2]: typ = dict[str, list[int]]
In [3]: msg = b'{"x":[1]}'
In [4]: %timeit msgspec.json.decode(msg, type=typ)
653 ns ± 5.72 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)In both cases the call overhead without parsing type annotations is: In [5]: %timeit msgspec.json.decode(msg)
128 ns ± 0.343 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)Given this, this is roughly a 3x speedup (1650 ns down to 525 ns) in type annotation parsing. |
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.
This adds support for
typing.Finalannotations.Finalcan be used to wrap an existing field annotation, marking it as a field that can't be modified once it's initialized. This has the same semantics asfrozen=True, but only for a single field, and not enforced at runtime.The
Frozenannotation may be used to wrap field annotations on any object-like type we support (msgspec.Struct,attrs, ordataclasses).Note that currently
mypyhas a bug (python/mypy#5608) whereFinalannotations in dataclass-like things require a default value (the bug seems to be early on in mypy's type processing pipeline). This is to enforce some vague wording in theFinalPEP that indicates it should be an error to not have a value when defining aFinalannotation on a class where that value isn't set in the__init__. Since dataclass-like-types don't have a visible__init__, this error is triggered. However! If you add# type: ignoreon that line then the rest works fine, including all the normal dataclass type annotation validation.Adding this since it was asked for in cattrs, and it's nice to have compatibility across these kinds of libraries.
I'm also happy to see that the refactor needed to our type parsing routine also leads to a measurable speedup when processing type annotations into our own internal format. Nice when a new feature also leads to faster code.