Prohibit some illegal uses of Literal#6034
Conversation
This pull request checks to make sure we report errors if the user tries using Literal types in invalid places. In particular, this PR... 1. Adds tests to make sure we cannot subclass Literals (mypy already directly supported this) 2. Checks to make sure we cannot use Literals inside `isinstance` and `issubclass` checks I also wanted to add a check preventing people from attempting to instantiate a Literal (e.g. disallow `Literal[3]()` or `Literal()`), but that might require a little more work and a few changes to `typing_extensions`. (We currently don't raise an error when people try doing things like `Final()`, `Final[int]()` or `Protocol()` either).
Hm, maybe this depend on version, but I see this: Anyway, I think the fact that the error is emitted at runtime means it is not super-important to also flag this statically. |
Oh, I meant we don't raise an error at type-check-time. (Well, we sort of do for
Yeah -- I agree. I thought about making more of an effort to add support for this, but came to the same conclusion you did and just submitted this PR as-is. |
This pull request checks to make sure we report errors if the user tries using Literal types in invalid places. In particular, this PR...
isinstanceandissubclasschecksI also wanted to add a check preventing people from attempting to instantiate a Literal (e.g. disallow
Literal[3]()orLiteral()), but that might require a little more work and a few changes totyping_extensions. (We currently don't raise an error when people try doing things likeFinal(),Final[int]()orProtocol()either).