mirror of https://github.com/astral-sh/ruff
Fixes https://github.com/astral-sh/ty/issues/1368 ## Summary Add support for patterns like this, where a type alias to a literal type (or union of literal types) is used to subscript `typing.Literal`: ```py type MyAlias = Literal[1] def _(x: Literal[MyAlias]): ... ``` This shows up in the ecosystem report for PEP 613 type alias support. One interesting case is an alias to `bool` or an enum type. `bool` is an equivalent type to `Literal[True, False]`, which is a union of literal types. Similarly an enum type `E` is also equivalent to a union of its member literal types. Since (for explicit type aliases) we infer the RHS directly as a type expression, this makes it difficult for us to distinguish between `bool` and `Literal[True, False]`, so we allow either one to (or an alias to either one) to appear inside `Literal`, where other type checkers allow only the latter. I think for implicit type aliases it may be simpler to support only types derived from actually subscripting `typing.Literal`, though, so I didn't make a TODO-comment commitment here. ## Test Plan Added mdtests, including TODO-filled tests for PEP 613 and implicit type aliases. ### Conformance suite All changes here are positive -- we now emit errors on lines that should be errors. This is a side effect of the new implementation, not the primary purpose of this PR, but it's still a positive change. ### Ecosystem Eliminates one ecosystem false positive, where a PEP 695 type alias for a union of literal types is used to subscript `typing.Literal`. |
||
|---|---|---|
| .. | ||
| resources | ||
| src | ||
| tests | ||
| Cargo.toml | ||
| build.rs | ||
| mdtest.py | ||
| mdtest.py.lock | ||