Comments (4)
Yes, but that's not what Final
means. Final
is closer to const
, i.e. you can't reassign the name to something else, but it doesn't prevent mutations.
x: Final[dict[str, int]] = {}
x["five"] = 5 # this is allowed
x.clear() # this is also allowed
x = {} # but this is forbidden
But tracking mutations statically in Python is tricky if not impossible (especially in pyi files), hence the additional need for telling the type checker which methods will cause an object to be mutated. So this is a massive change and will require a PEP, it's not as simple as allowing Final
in function signatures.
from typing.
There's also this existing discussion on the Python Discourse: https://discuss.python.org/t/extend-the-typing-final-type-qualifier-to-support-function-arguments/41916
I personally don't think Final
is a good equivalent for the absence of Rust's mut
. They actually mean quite different things and an Immutable
modifier would be difficult to support from a type checker's perspective unless you treat all method calls as mutations by default and then also add an @idempotent
decorator to mark methods that are fine to call on Immutable
references to objects.
Also you are using str
in your example which is an immutable type in Python, so your example never modifies changeable
or unchangeable
regardless, it will always create new str
instances, even for +=
.
from typing.
from typing.
from typing.
Related Issues (20)
- hello
- Spec: Add PEP 696 (Type parameter defaults) HOT 2
- Spec: Add PEP 705 (Read-only TypedDict items) HOT 3
- Extra key of TypedDict HOT 2
- Reason given for disallowing non-concrete subtype assignment is unsound HOT 10
- Out-of-date description of how to indicate positional-only parameters
- Typing spec should be clearer that type checkers are not expected to support PEP 3141 HOT 22
- Incorporate PEP 483 into the spec HOT 5
- Conformance tests: Add mechanism to allow errors on one of multiple lines HOT 2
- Conformance tests: Add mechanism to ignore errors
- Conformance tests: Align automated and manual scoring HOT 1
- Introduce a `Language` type to provide consistent language information of strings. HOT 5
- Spec: Version and platform checking underspecified HOT 6
- Support list comprehension for `TypeVarTuple` HOT 2
- Clarify the float/int/complex special case HOT 5
- Clarify the implications of subclassing Any HOT 1
- Allow multiple `TypeVarTuple` in Generic Classes when they are wrapped. HOT 5
- pytype conformance tests crash if no `python3.11` executable is in `PATH` HOT 5
- Conformance test: make dataclass_hash.py not rely on `typing.Hashable`? HOT 1
- Proposal: Add coerced type narrowing similar to 'cast' HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typing.