This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vanburgerberg
Recipients vanburgerberg
Date 2021-10-30.15:33:26
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1635608006.78.0.966278676499.issue45679@roundup.psfhosted.org>
In-reply-to
Content
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.
History
Date User Action Args
2021-10-30 15:33:26vanburgerbergsetrecipients: + vanburgerberg
2021-10-30 15:33:26vanburgerbergsetmessageid: <1635608006.78.0.966278676499.issue45679@roundup.psfhosted.org>
2021-10-30 15:33:26vanburgerberglinkissue45679 messages
2021-10-30 15:33:26vanburgerbergcreate