Message405371
When you create `Literal[1]` annotation and then create `Literal[True]` annotation, in the second case you will actually get `Literal[1]` instead. This is happening because `typing` performs caching of the outcome of parameterizing generics and `hash(True)` is equal to `hash(1)`. I think this behavior is incorrect and may lead to unexpected results.
Why is this inaccuracy important?
Consider the following example:
```python
from typing import Literal
SomeUsefulAlias = Literal[1, "abc"]
def func(arg: Literal[True, "abc"]):
if arg is not True:
arg.capitalize()
```
If we look at `func.__annotations__["arg"]`, we will see `Literal[1, 'abc']`. According to the new annotation, we can pass the value `1` to `func`, and this will lead to an attribute error, as you've already understood. Thus, proper runtime type checking cannot be performed. |
|
Date |
User |
Action |
Args |
2021-10-30 15:33:26 | vanburgerberg | set | recipients:
+ vanburgerberg |
2021-10-30 15:33:26 | vanburgerberg | set | messageid: <1635608006.78.0.966278676499.issue45679@roundup.psfhosted.org> |
2021-10-30 15:33:26 | vanburgerberg | link | issue45679 messages |
2021-10-30 15:33:26 | vanburgerberg | create | |
|