[ty] Improve literal promotion heuristics (#21439)

## Summary

Extends literal promotion to apply to any generic method, as opposed to
only generic class constructors. This PR also improves our literal
promotion heuristics to only promote literals in non-covariant position
in the return type, and avoid promotion if the literal is present in
non-covariant position in any argument type.

Resolves https://github.com/astral-sh/ty/issues/1357.
This commit is contained in:
Ibraheem Ahmed 2025-11-14 16:13:56 -05:00 committed by GitHub
parent 3e7e91724c
commit c5d654bce8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 570 additions and 255 deletions

View file

@ -978,10 +978,10 @@ reveal_type(ParentDataclass.__init__)
reveal_type(ChildOfParentDataclass.__init__)
result_int = uses_dataclass(42)
reveal_type(result_int) # revealed: ChildOfParentDataclass[Literal[42]]
reveal_type(result_int) # revealed: ChildOfParentDataclass[int]
result_str = uses_dataclass("hello")
reveal_type(result_str) # revealed: ChildOfParentDataclass[Literal["hello"]]
reveal_type(result_str) # revealed: ChildOfParentDataclass[str]
```
## Descriptor-typed fields