Skip to content

How can I specify that a value can't change, but can be overridden? #1486

Answered by erictraut
beauxq asked this question in Q&A
Discussion options

You must be logged in to vote

As you point out, Final implies both "immutable" and "can't be overridden". In your use case, you want to indicate that the field is read-only but still overridable by subclasses. There has been some discussion about adding ReadOnly to the type system, but it hasn't gone very far.

This wasn't an issue prior to pyright enforcing invariance for mutable fields. Mypy users will also encounter the problem when this feature is implemented in mypy.

You may need to resort to a # type: ignore to work around this current shortcoming in the type system.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by beauxq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants