You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Implement Const type to indicate non-rebindable variables/attributes #1214
As a Python developer, I would like a Const type so that I can indicate that a variable or attribute may not be rebound to another value. This would be especially useful for module and class constants, but could also be helpful for instance attributes that really shouldn't be modified. Not only does it help prevent accidentally overwriting constants, it would hopefully reduce the need to use @property on non-critical attributes. Here is a contrived example:
kind = MANAGER # type: Const reqiures the Const type to rebind kind without an error just as a const variable in C requires a cast during reassignment to quiet compiler warnings/errors.
I'm not sure if it makes sense to allow Const to take parameters as the type can be inferred in most cases. One might want other variables, such as those passed into functions, to be const, in which case it makes sense to allow Const to take parameters. Here is an example:
Early in the history of mypy I was actually planning to have something like this, but I guess I later forgot about it. This would likely require a PEP 484 (and potentially typing) change.
A clever programmer would be able to get around this through methods that mutate, or by a shared reference, but the guard rails are still nice.
Alternatively, it could only be allowed for immutable non-container primitives such as int or str, or maybe even extended to tuples/namedtuples of immutable primitives as well
We are now considering something like this, perhaps spelled as Final. An interesting use case would be with literal types (another potential new feature) -- it would allow mypy to infer things like if X: never being taken if there is X: Final = False.
Activity
JukkaL commentedon Feb 19, 2016
Early in the history of mypy I was actually planning to have something like this, but I guess I later forgot about it. This would likely require a PEP 484 (and potentially
typing) change.davidroeca commentedon Apr 29, 2017
Const in Python could have similar guarantees to something like JavaScript ES6's const, to prevent reassignment https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const
A clever programmer would be able to get around this through methods that mutate, or by a shared reference, but the guard rails are still nice.
Alternatively, it could only be allowed for immutable non-container primitives such as int or str, or maybe even extended to tuples/namedtuples of immutable primitives as well
JukkaL commentedon May 18, 2018
We are now considering something like this, perhaps spelled as
Final. An interesting use case would be with literal types (another potential new feature) -- it would allow mypy to infer things likeif X:never being taken if there isX: Final = False.Final names and attributes (#5522)