Replies: 1 comment 1 reply
-
A change like this would need to be made to the CPython runtime or the Python typing spec. The Python typing forum would be a good place to post. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'd like to enforce immutability in a popular Python GraphQL server library, which uses data class transforms to represent types.
Here's my original issue report and a proposed solution:
frozen
by default? strawberry-graphql/strawberry#3396 (which references changes made to Pyright since 1.1.328As you can see in strawberry-graphql/strawberry#3397 (comment), making all dataclasses frozen introduces a non-trivial performance penalty of 30%. I understand this is because frozen dataclasses are more expensive to initialise: https://rednafi.com/python/statically_enforcing_frozen_dataclasses/
Since immutability is so convenient for static type checkers, I'm wondering if data classes in Python could be extended with a zero-overhead, static-type-checker-only immutability config option?
PS: This is a general Python static typing musing, not really related to Pyright. I'm posting it here because I'd love to hear @erictraut's views on this – but if there is a better place for something like this, please let me know and I'll use it going forward.
PPS: For reference, here is an analogous hack for those using the attrs library https://threeofwands.com/attra-iv-zero-overhead-frozen-attrs-classes/ Something like this would be perfectly fine for codebases that have good static type checking coverage.
Beta Was this translation helpful? Give feedback.
All reactions