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
Add an option for mypy, maybe something like --infer-immutable-when-possible, that would cause mypy to infer immutable version of types (like Sequence, Mapping, FrozenSet) instead of mutable (List, Dict, Set)
With this feature turned on the below code:
x= [1, 2, 3]
x.append(4)
mypy would give error: "Sequence[int]" has no attribute "append" as it would infer x to be Sequence[int] instead of List[int]
Pitch
Many developers support the idea of immutability by default for a number of reasons, and adding this option would be a nice way for them to opt-in to some version of this with mypy. Currently if I want my variable x = [1, 2, 3] to be Sequence[int] instead of List[int], I need to explicitly add a Sequence[int] type annotation, which is easy to forget. If I had this feature option turned on, I would instead need to explicitly opt-in to mutability with x: List[int] = [1, 2, 3]
I'd love to find out if this feature is something that would be 1. feasible and 2. welcome in the project. If so, I could take a shot with implementing it!
The text was updated successfully, but these errors were encountered:
Sequence does not support add for concatenation, so such inference would prevent more than just mutation. In this case, will the type checker know that the inference is not possible? Also how does the inference option relate to variance? e.g. I imagine callable return types should not change from List to Sequence.
There haven't been any other requests for this feature. I think it wouldn't be usable in most Python programs, and could interact surprisingly with narrowing. I think #5756 would be the good, explicit way to do this.
Feature
Add an option for mypy, maybe something like
--infer-immutable-when-possible
, that would cause mypy to infer immutable version of types (likeSequence
,Mapping
,FrozenSet
) instead of mutable (List
,Dict
,Set
)With this feature turned on the below code:
mypy would give
error: "Sequence[int]" has no attribute "append"
as it would inferx
to beSequence[int]
instead ofList[int]
Pitch
Many developers support the idea of immutability by default for a number of reasons, and adding this option would be a nice way for them to opt-in to some version of this with mypy. Currently if I want my variable
x = [1, 2, 3]
to beSequence[int]
instead ofList[int]
, I need to explicitly add aSequence[int]
type annotation, which is easy to forget. If I had this feature option turned on, I would instead need to explicitly opt-in to mutability withx: List[int] = [1, 2, 3]
I'd love to find out if this feature is something that would be 1. feasible and 2. welcome in the project. If so, I could take a shot with implementing it!
The text was updated successfully, but these errors were encountered: