-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Missing mechanism to enforce/check parameters interior mutability/inmutability #6947
Comments
Found related (but not entirely equivalent) issues: |
This has been discussed in various contexts previously, starting from the early days of Python static type checking, but it has never moved forward, perhaps because nobody has been able to come up with a workable proposal. Marking functions as pure is difficult, since for it to work as I'd expect, library functions without side effects would also have to be annotated as pure, which would be a big undertaking. For example, a pure method should probably be able to call If you still think that this is worth pursuing, my recommendation is to write a concrete proposal about what it would look like, with a focus on explaning how this would work with real-world code. Otherwise it may be hard to get people interested. The typing repository or the typing-sig@ mailing list are better places to discuss new type system features, since maintainers of other type checking tools also follow them, and we generally try to standardize major new mypy type system features. |
This isn't quite strictly true, but it's mostly accurate to say that |
Well, but it also affects the possibility of reading the object's parameters, since we don't pass the |
Totally true, I was thinking that, given that the typing is something optional, we could create some kind of "good-faith" annotation/comment to mark the calls to not annotated methods, that the type checker should trust (I'm not considering the possibility of "adversarial programmers" that mess with the typing system on purpose). That way it should be easier to incorporate these changes gradually, without having to undertake a big effort in one single release, but of course the burden would be on the caller side, at least for some time. Maybe something like: def foo(x: Immutable[C]) -> str:
# We attach a local annotation to a function/callable, so the type checker won't complain
# for calling a function that does not provide the guarantees that `foo`'s signature is
# promising.
#
# annotate(bar): Callable[[Immutable[C]], str]
return bar(x)
def moo(x: Immutable[C]) -> str:
# This should generate a warning, because the annotation is redundant
# annotate(foo): Callable[[Immutable[C]], str]
return foo(x)
def func(x: Immutable[C]) -> int:
# This should generate an error, because the annotation is not compatible
# annotate(foo): Callable[[Immutable[C]], int]
return foo(x) Although could be that this mechanism is general enough for other use cases, not sure though. |
Regarding how to start a discussion about this in the typing repository, I have three points now, and not sure if I should comment all together or open different issues for each of them:
|
Disclaimer: Not sure if this "feature request" should belong to Mypy or to Python itself in the form of RFC/PEP document(s). In any case, I would very like to be involved in this kind of stuff, so I would be very grateful about any kind of guidance on the topic.
First of all, this could be split into two different related issues, but for now I prefer to keep both together:
self
reference as a parameter.There are various possible approaches, but I think they could be summarized to two: trying to mark the whole function as "pure", or marking each parameter as "safe from being mutated" individually.
Why this could be interesting? Well, it could give extra guarantees to the consumers/users of some functions and/or methods, and also could make easier to spot implementation problems.
Note: Even if Mypy/Pyre/Pycharm are not able to do anything with the annotations, I believe starting to think on how they should look is a first important step.
The text was updated successfully, but these errors were encountered: