Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass variable as final #1557

Open
cojmeister opened this issue Jan 2, 2024 · 4 comments
Open

Pass variable as final #1557

cojmeister opened this issue Jan 2, 2024 · 4 comments
Labels
topic: feature Discussions about new features for Python's type annotations

Comments

@cojmeister
Copy link

Proposal:
The idea is to be able to pass variables as constants, mimicking c++ or rust behaviour such as. And have this be solely as part of the typings ecosystem. It's extremely helpful when reviewing code, or starting to use new codebases.

Issue opened in cpython repo: #113654

fn my_function(mut changeable: &String, unchangeable: &String) {}

In python I suggest the following:

def my_function(changeable: str, unchangeable: Final[str])-> None:
    arguments_that_changes += "world!"
    return arguments_that_changes + argument_that_stays_constant
@cojmeister cojmeister added the topic: feature Discussions about new features for Python's type annotations label Jan 2, 2024
@Daverball
Copy link
Contributor

Daverball commented Jan 2, 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 +=.

@cojmeister
Copy link
Author

cojmeister commented Jan 2, 2024 via email

@Daverball
Copy link
Contributor

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.

@cojmeister
Copy link
Author

cojmeister commented Jan 2, 2024 via email

@python python deleted a comment from karimahmadi99 Feb 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: feature Discussions about new features for Python's type annotations
Projects
None yet
Development

No branches or pull requests

2 participants