mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 02:38:25 +00:00
![]() This implements the stopgap approach described in https://github.com/astral-sh/ty/issues/336#issuecomment-2880532213 for handling literal types in generic class specializations. With this approach, we will promote any literal to its instance type, but _only_ when inferring a generic class specialization from a constructor call: ```py class C[T]: def __init__(self, x: T) -> None: ... reveal_type(C("string")) # revealed: C[str] ``` If you specialize the class explicitly, we still use whatever type you provide, even if it's a literal: ```py from typing import Literal reveal_type(C[Literal[5]](5)) # revealed: C[Literal[5]] ``` And this doesn't apply at all to generic functions: ```py def f[T](x: T) -> T: return x reveal_type(f(5)) # revealed: Literal[5] ``` --- As part of making this happen, we also generalize the `TypeMapping` machinery. This provides a way to apply a function to type, returning a new type. Complicating matters is that for function literals, we have to apply the mapping lazily, since the function's signature is not created until (and if) someone calls its `signature` method. That means we have to stash away the mappings that we want to apply to the signatures parameter/return annotations once we do create it. This requires some minor `Cow` shenanigans to continue working for partial specializations. |
||
---|---|---|
.. | ||
ruff | ||
ruff_annotate_snippets | ||
ruff_benchmark | ||
ruff_cache | ||
ruff_db | ||
ruff_dev | ||
ruff_diagnostics | ||
ruff_formatter | ||
ruff_graph | ||
ruff_index | ||
ruff_linter | ||
ruff_macros | ||
ruff_notebook | ||
ruff_options_metadata | ||
ruff_python_ast | ||
ruff_python_ast_integration_tests | ||
ruff_python_codegen | ||
ruff_python_formatter | ||
ruff_python_index | ||
ruff_python_literal | ||
ruff_python_parser | ||
ruff_python_resolver | ||
ruff_python_semantic | ||
ruff_python_stdlib | ||
ruff_python_trivia | ||
ruff_python_trivia_integration_tests | ||
ruff_server | ||
ruff_source_file | ||
ruff_text_size | ||
ruff_wasm | ||
ruff_workspace | ||
ty | ||
ty_ide | ||
ty_project | ||
ty_python_semantic | ||
ty_server | ||
ty_test | ||
ty_vendored | ||
ty_wasm |