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.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 asfrozen=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
, ordataclasses
).Note that currently
mypy
has a bug (python/mypy#5608) whereFinal
annotations 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 theFinal
PEP that indicates it should be an error to not have a value when defining aFinal
annotation 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: ignore
on 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.