Replies: 2 comments 3 replies
-
I think this would be an inappropriate use of There are cases where |
Beta Was this translation helpful? Give feedback.
-
Your example looks wrong to me.
I don't see why we should special case this to explicitly contradict annotation and allow |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Background
For some background refer to typing-sig discussion and the corresponding discussion on, python/mypy#2008.
pyright allows mutating objects through a variable declared as an immutable superclass and mypy will soon follow suit.
For instance:
The rationale for the change is that assignment at declaration should behave the same as a separate assignment statement, such as:
I agree that assigning a variable on the same line as its declaration should behave like assigning it on the following line.
That said, having a way to declare a constant with a restricted immutable type like
Mapping
orSequence
would still be helpful to improve program correctness. It's also common to use immutable types for function arguments for the same reason.A workaround is to put constants in a separate module as the type checker will use the declared type in this case.
Proposal
What would you think of a proposal that treats initialization with
Final
(specifically) as non-narrowing so that the first example still prevents mutability?My rationale is that:
Final
without a type argument can still be used to infer the assigned type.Beta Was this translation helpful? Give feedback.
All reactions