mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 02:38:25 +00:00
![]() This primarily comes up with annotated `self` parameters in constructors: ```py class C[T]: def __init__(self: C[int]): ... ``` Here, we want infer a specialization of `{T = int}` for a call that hits this overload. Normally when inferring a specialization of a function call, typevars appear in the parameter annotations, and not in the argument types. In this case, this is reversed: we need to verify that the `self` argument (`C[T]`, as we have not yet completed specialization inference) is assignable to the parameter type `C[int]`. To do this, we simply look for a typevar/type in both directions when performing inference, and apply the inferred specialization to argument types as well as parameter types before verifying assignability. As a wrinkle, this exposed that we were not checking subtyping/assignability for function literals correctly. Our function literal representation includes an optional specialization that should be applied to the signature. Before, function literals were considered subtypes of (assignable to) each other only if they were identical Salsa objects. Two function literals with different specializations should still be considered subtypes of (assignable to) each other if those specializations result in the same function signature (typically because the function doesn't use the typevars in the specialization). Closes https://github.com/astral-sh/ty/issues/370 Closes https://github.com/astral-sh/ty/issues/100 Closes https://github.com/astral-sh/ty/issues/258 --------- Co-authored-by: Carl Meyer <carl@astral.sh> |
||
---|---|---|
.. | ||
legacy | ||
pep695 | ||
builtins.md | ||
scoping.md |