Code Monkey home page Code Monkey logo

Comments (4)

Daverball avatar Daverball commented on August 21, 2024 2

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.

Daverball avatar Daverball commented on August 21, 2024

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.

cojmeister avatar cojmeister commented on August 21, 2024

from typing.

cojmeister avatar cojmeister commented on August 21, 2024

from typing.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.