Files
Carl MeyerandDouglas Creager 2c98e83118 [ty] Treat caller-fixed type variables as concrete during inference (#30423)
Consider this example of a generic call within another generic function
(the last line):

```python
from typing import Any

def keep[L: list[Any]](items: L) -> L:
    return items

def forward[T](items: list[T]) -> list[T]:
    return keep(items)
```

The type variables for the call we are currently inferring (`L`) are
"inferable", and we can pick any solution for them that fits the
constraints: the goal of constraint solving is to pick a valid solution
for inferable type variables.

Type variables from the outer scope (`T`) are "fixed" or "non-inferable"
-- they could take on any type within their bounds/constraints, and our
solution has to work for all their possible specializations; we aren't
allowed to pick a solution for them.

Today on main, our solver largely internally treats non-inferable type
variables as inferable. This can cause us to wrongly report "solutions"
that are only valid for some specializations of the non-inferable
typevars. It can also cause us to wrongly substitute occurrences of the
fixed typevar with some other type.

For example, in the above snippet we get the constraint `list[T] <: L`
from the call argument, and `L <: list[T]` from the return-type context
provided by the return annotation on `forward`. This should be enough to
straightforwardly solve the call to `list[T]`, which is the right
solution. But on main we get confused by the additional constraint `L <:
list[Any]` (from the upper bound of `L`): we combine that with the above
constraints to infer `T = Any`, and then we substitute that into
`list[T]` in order to get the solution `list[Any]`. We should not be
substituting for non-inferable typevars like this.

This change uses the set of inferable type variables when collecting and
solving constraints. It preserves `T` and infers `list[T]` for this
call.

Closes astral-sh/ty#4696.
Closes astral-sh/ty#5019.
Closes astral-sh/ty#5074.
Partially addresses astral-sh/ty#5063 (improves Bokeh perf by ~70%)

---------

Co-authored-by: Douglas Creager <dcreager@dcreager.net>
2026-11-23 12:16:40 -08:00

21 KiB

Calling builtins

bool with incorrect arguments

class NotBool:
    __bool__ = None

# error: [too-many-positional-arguments] "Too many positional arguments to class `bool`: expected 1, got 2"
bool(1, 2)

# TODO: We should emit an `unsupported-bool-conversion` error here because the argument doesn't implement `__bool__` correctly.
bool(NotBool())

Calls to str()

Valid calls

str()
str("")
str(b"")
str(1)
str(object=1)

str(b"M\xc3\xbcsli", "utf-8")
str(b"M\xc3\xbcsli", "utf-8", "replace")

str(b"M\x00\xfc\x00s\x00l\x00i\x00", encoding="utf-16")
str(b"M\x00\xfc\x00s\x00l\x00i\x00", encoding="utf-16", errors="ignore")

str(bytearray.fromhex("4d c3 bc 73 6c 69"), "utf-8")
str(bytearray(), "utf-8")
str(memoryview(b"hello world"), "utf-8")

str(encoding="utf-8", object=b"M\xc3\xbcsli")
str(b"", errors="replace")

range as an ordinary range value

reveal_type(list(range(3)))  # revealed: list[int]
reveal_type([range(0)])  # revealed: list[range]

class Uop:
    replicated = range(0)

def _(uop: Uop) -> None:
    uop.replicated = range(1, 3)
    reveal_type(uop.replicated)  # revealed: range

Invalid calls

# These are valid at runtime, but the typeshed signature for `str.__new__` requires `object`
# when `encoding` or `errors` are provided.
# error: [no-matching-overload]
str(encoding="utf-8")

# error: [no-matching-overload]
str(errors="replace")

# error: [invalid-argument-type]
# error: [invalid-argument-type]
str(1, 2)

# error: [no-matching-overload]
str(o=1)

# First argument is not a bytes-like object:
# error: [invalid-argument-type]
str("Müsli", "utf-8")

# Second argument is not a valid encoding:
# error: [invalid-argument-type]
str(b"M\xc3\xbcsli", b"utf-8")

Calls to isinstance

We infer Literal[True] for a limited set of cases where we can be sure that the answer is correct, but fall back to bool otherwise.

from enum import Enum
from types import FunctionType
from typing import TypeVar

class Answer(Enum):
    NO = 0
    YES = 1

reveal_type(isinstance(True, bool))  # revealed: Literal[True]
reveal_type(isinstance(True, int))  # revealed: Literal[True]
reveal_type(isinstance(True, object))  # revealed: Literal[True]
reveal_type(isinstance("", str))  # revealed: Literal[True]
reveal_type(isinstance(1, int))  # revealed: Literal[True]
reveal_type(isinstance(b"", bytes))  # revealed: Literal[True]
reveal_type(isinstance(Answer.NO, Answer))  # revealed: Literal[True]

reveal_type(isinstance((1, 2), tuple))  # revealed: Literal[True]

def f(): ...

reveal_type(isinstance(f, FunctionType))  # revealed: Literal[True]

reveal_type(isinstance("", int))  # revealed: bool

class A: ...
class SubclassOfA(A): ...
class OtherSubclassOfA(A): ...
class B: ...

reveal_type(isinstance(A, type))  # revealed: Literal[True]

a = A()

reveal_type(isinstance(a, A))  # revealed: Literal[True]
reveal_type(isinstance(a, object))  # revealed: Literal[True]
reveal_type(isinstance(a, SubclassOfA))  # revealed: bool
reveal_type(isinstance(a, B))  # revealed: bool

s = SubclassOfA()
reveal_type(isinstance(s, SubclassOfA))  # revealed: Literal[True]
reveal_type(isinstance(s, A))  # revealed: Literal[True]

def _(x: A | B, y: list[int]):
    reveal_type(isinstance(y, list))  # revealed: Literal[True]
    reveal_type(isinstance(x, A))  # revealed: bool

    if isinstance(x, A):
        pass
    else:
        reveal_type(x)  # revealed: B & ~A
        reveal_type(isinstance(x, B))  # revealed: Literal[True]

T = TypeVar("T")
T_bound_A = TypeVar("T_bound_A", bound=A)
T_constrained = TypeVar("T_constrained", SubclassOfA, OtherSubclassOfA)

def _(
    x: T,
    x_bound_a: T_bound_A,
    x_constrained_sub_a: T_constrained,
):
    reveal_type(isinstance(x, object))  # revealed: Literal[True]
    reveal_type(isinstance(x, A))  # revealed: bool

    reveal_type(isinstance(x_bound_a, object))  # revealed: Literal[True]
    reveal_type(isinstance(x_bound_a, A))  # revealed: Literal[True]
    reveal_type(isinstance(x_bound_a, SubclassOfA))  # revealed: bool
    reveal_type(isinstance(x_bound_a, B))  # revealed: bool

    reveal_type(isinstance(x_constrained_sub_a, object))  # revealed: Literal[True]
    reveal_type(isinstance(x_constrained_sub_a, A))  # revealed: Literal[True]
    reveal_type(isinstance(x_constrained_sub_a, SubclassOfA))  # revealed: bool
    reveal_type(isinstance(x_constrained_sub_a, OtherSubclassOfA))  # revealed: bool
    reveal_type(isinstance(x_constrained_sub_a, B))  # revealed: bool

An isinstance check against Callable is always true when the checked value is known to be callable. This applies to both typing.Callable and collections.abc.Callable, including after a preceding isinstance check narrows a union.

from collections.abc import Callable
from typing import Callable as TypingCallable

def reveal_callable_result(x: Callable[[int], int], y: Callable[[int], int] | dict[str, int]):
    reveal_type(isinstance(x, Callable))  # revealed: Literal[True]
    reveal_type(isinstance(x, TypingCallable))  # revealed: Literal[True]

    if isinstance(y, dict):
        return

    reveal_type(isinstance(y, Callable))  # revealed: Literal[True]
    reveal_type(isinstance(y, TypingCallable))  # revealed: Literal[True]

An isinstance check against a tuple is always true when each possible type of the checked value is accepted by at least one class in the tuple. This avoids a false implicit-return error when the check is the only path that returns a value. The same applies when the tuple is assigned to a local variable.

def reveal_tuple_result(x: A | B):
    reveal_type(isinstance(x, (A, B)))  # revealed: Literal[True]

def accepts_a(x: A) -> bool:
    if isinstance(x, (B, A)):
        return True

def accepts_a_or_b(x: A | B) -> bool:
    if isinstance(x, (A, B)):
        return True

def accepts_object(x: object) -> bool:
    if isinstance(x, (object,)):
        return True

def accepts_stored_tuple(x: A | B) -> bool:
    targets = (A, B)
    if isinstance(x, targets):
        return True

The tuple must contain a fixed set of known classes. A partial tuple cannot cover a union, a variadic tuple can be empty, and a value annotated as type[A] can refer to a subclass of A. Nested tuples, unions used as tuple elements, and aliases such as typing.List are also left as bool.

from typing import List

def reveal_unsupported_tuple_results(x: A | B, items: list[int]):
    reveal_type(isinstance(x, (A, (B, bytes))))  # revealed: bool
    reveal_type(isinstance(x, (A | B,)))  # revealed: bool
    reveal_type(isinstance(items, (List,)))  # revealed: bool

def partial_tuple(x: A | B) -> bool:  # error: [invalid-implicit-return]
    if isinstance(x, (A, bytes)):
        return True

def variadic_tuple(x: A, targets: tuple[type[A], ...]) -> bool:  # error: [invalid-implicit-return]
    if isinstance(x, targets):
        return True

def subclass_target(x: A, target: type[A]) -> bool:  # error: [invalid-implicit-return]
    if isinstance(x, (target,)):
        return True

The single-class path already leaves runtime-checkable protocol checks as bool. Tuple members use the same inference, so a class that only appears structurally compatible does not make the tuple check certain.

from typing import Protocol, runtime_checkable

@runtime_checkable
class RuntimeProtocol(Protocol):
    value: int

class StructuralImplementation:
    value: int

reveal_type(isinstance(StructuralImplementation(), RuntimeProtocol))  # revealed: bool
reveal_type(isinstance(StructuralImplementation(), (RuntimeProtocol,)))  # revealed: bool

Single-class isinstance inference does not account for an overridden __instancecheck__. Tuple members again use the same inference. Python returns False for these checks, while ty infers Literal[True].

class RejectingMeta(type):
    def __instancecheck__(self, instance: object, /) -> bool:
        return False

class RejectingBase(metaclass=RejectingMeta): ...
class RejectingChild(RejectingBase): ...

reveal_type(isinstance(RejectingChild(), RejectingBase))  # revealed: Literal[True]
reveal_type(isinstance(RejectingChild(), (RejectingBase,)))  # revealed: Literal[True]

The same limitation applies to type: list[int] is accepted where type is expected, but isinstance(list[int], type) is false at runtime. Single-class and tuple checks both infer Literal[True] for a bare type and a type variable bound to type.

T_bound_type = TypeVar("T_bound_type", bound=type)

def bare_type(x: type):
    reveal_type(isinstance(x, type))  # revealed: Literal[True]
    reveal_type(isinstance(x, (type,)))  # revealed: Literal[True]

bare_type(list[int])

def type_variable_bound_to_type(x: T_bound_type):
    reveal_type(isinstance(x, type))  # revealed: Literal[True]
    reveal_type(isinstance(x, (type,)))  # revealed: Literal[True]

type_variable_bound_to_type(list[int])

Certain special forms in the typing module are not instances of type, so are strictly-speaking disallowed as the second argument to isinstance() according to typeshed's annotations. However, at runtime they work fine as the second argument, and we implement that special case in ty:

import typing as t

# no errors emitted for any of these:
isinstance("", t.Dict)
isinstance("", t.List)
isinstance("", t.Set)
isinstance("", t.FrozenSet)
isinstance("", t.Tuple)
isinstance("", t.ChainMap)
isinstance("", t.Counter)
isinstance("", t.Deque)
isinstance("", t.OrderedDict)
isinstance("", t.Callable)
isinstance("", t.Type)
isinstance("", t.Callable | t.Deque)

# `Any` is valid in `issubclass()` calls but not `isinstance()` calls
issubclass(list, t.Any)
issubclass(list, t.Any | t.Dict)

# The same works in tuples
isinstance("", (int, t.Dict))
isinstance("", (int, t.Callable))
issubclass(list, (int, t.Any))

But for other special forms that are not permitted as the second argument, we still emit an error:

isinstance("", t.TypeGuard)  # error: [invalid-argument-type]
isinstance("", t.ClassVar)  # error: [invalid-argument-type]
isinstance("", t.Final)  # error: [invalid-argument-type]
isinstance("", t.Any)  # error: [invalid-argument-type]

# The same applies when `Any` is nested inside a tuple
isinstance("", (int, t.Any))  # error: [invalid-argument-type]

Calls to isinstance with tuple-covered aliases and type variables

[environment]
python-version = "3.12"
from typing import TypeVar
from typing_extensions import TypeAliasType, Union

class A: ...
class B: ...

type AliasA = A
type AliasB = B
type AliasAB = AliasA | AliasB

T_constrained_a_b = TypeVar("T_constrained_a_b", A, B)
T_bound_a_b = TypeVar("T_bound_a_b", bound=A | B)
T_bound_alias_a_b = TypeVar("T_bound_alias_a_b", bound=AliasAB)

def accepts_alias(x: AliasAB) -> bool:
    reveal_type(isinstance(x, (A, B)))  # revealed: Literal[True]
    if isinstance(x, (A, B)):
        return True

def accepts_constrained_typevar(x: T_constrained_a_b) -> bool:
    reveal_type(isinstance(x, (A, B)))  # revealed: Literal[True]
    if isinstance(x, (A, B)):
        return True

def accepts_union_bound_typevar(x: T_bound_a_b) -> bool:
    reveal_type(isinstance(x, (A, B)))  # revealed: Literal[True]
    if isinstance(x, (A, B)):
        return True

def accepts_alias_bound_typevar(x: T_bound_alias_a_b) -> bool:
    reveal_type(isinstance(x, (A, B)))  # revealed: Literal[True]
    if isinstance(x, (A, B)):
        return True

def accepts_truthy_constrained_typevar(x: T_constrained_a_b) -> bool:
    if not x:
        return False

    reveal_type(x)  # revealed: T_constrained_a_b@accepts_truthy_constrained_typevar & ~AlwaysFalsy
    reveal_type(isinstance(x, (A, B)))  # revealed: Literal[True]
    if isinstance(x, (A, B)):
        return True

# Invalid alias cycles still recover the non-recursive members for narrowing.
RecursiveA = TypeAliasType("RecursiveA", Union[A, "RecursiveB"])  # error: [cyclic-type-alias-definition]
RecursiveB = TypeAliasType("RecursiveB", Union[B, "RecursiveA"])  # error: [cyclic-type-alias-definition]
RecursivePartialA = TypeAliasType("RecursivePartialA", Union[A, "RecursivePartialB"])  # error: [cyclic-type-alias-definition]
RecursivePartialB = TypeAliasType("RecursivePartialB", Union[bytes, "RecursivePartialA"])  # error: [cyclic-type-alias-definition]

def accepts_mutually_recursive_alias(x: RecursiveA) -> bool:
    reveal_type(isinstance(x, (A, B)))  # revealed: Literal[True]
    if isinstance(x, (A, B)):
        return True

def partial_mutually_recursive_alias(x: RecursivePartialA) -> bool:  # error: [invalid-implicit-return]
    reveal_type(isinstance(x, (A, B)))  # revealed: bool
    if isinstance(x, (A, B)):
        return True

Generic builtins should not overfit upper-bound-only callback constraints

These examples are minimized from ecosystem regressions seen while preserving explicit Never and object bounds through the constraint solver. Callback parameter upper bounds should not overfit the result when the iterable argument is otherwise unknown. The callable return type should still be used where possible.

from ty_extensions._internal import Unknown

def _(xs: Unknown):
    # TODO: should be `list[Unknown]`
    reveal_type(sorted(xs, key=len))  # revealed: list[Sized]

    reveal_type(map("{}".format, xs))  # revealed: map[str]

    # TODO: This should reveal `str`. See https://github.com/astral-sh/ty/issues/4536.
    # The lack of gradual constraints allows the `LiteralString` overload to be selected.
    reveal_type("".join(map("{}".format, xs)))  # revealed: LiteralString

Mapping methods accept arbitrary object types

[environment]
python-version = "3.13"
from collections.abc import Mapping

def _(mapping: Mapping[str, int], dictionary: dict[str, int], key: object) -> None:
    mapping.get(key)
    mapping.get(key, 0)
    mapping.get(key, "default")
    dictionary.get(key)
    dictionary.get(key, 0)
    dictionary.get(key, "default")
    dictionary.pop(key)  # error: [invalid-argument-type]
    dictionary.pop(key, 0)
    dictionary.pop(key, None)
    dictionary.keys().isdisjoint(key)
    dictionary.items().isdisjoint(key)

The builtin NotImplemented constant is not callable

def _():
    # snapshot: not-callable
    raise NotImplemented()
error[not-callable]: `NotImplemented` is not callable
 --> src/mdtest_snippet.py:3:11
  |
3 |     raise NotImplemented()
  |           --------------^^
  |           |
  |           Did you mean `NotImplementedError`?
help: Use `NotImplementedError` instead
  |
2 |     # snapshot: not-callable
  -     raise NotImplemented()
3 +     raise NotImplementedError()
4 | def _():
  |
note: This is an unsafe fix and may change runtime behavior
def _():
    # snapshot: not-callable
    raise NotImplemented("this module is not implemented yet!!!")
error[not-callable]: `NotImplemented` is not callable
 --> src/mdtest_snippet.py:6:11
  |
6 |     raise NotImplemented("this module is not implemented yet!!!")
  |           --------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |           |
  |           Did you mean `NotImplementedError`?
help: Use `NotImplementedError` instead
  |
5 |     # snapshot: not-callable
  -     raise NotImplemented("this module is not implemented yet!!!")
6 +     raise NotImplementedError("this module is not implemented yet!!!")
7 | def _(NotImplementedError: object):
  |
note: This is an unsafe fix and may change runtime behavior

When a local binding shadows NotImplementedError, replacing NotImplemented with that name would not necessarily produce an exception, so we omit the fix.

def _(NotImplementedError: object):
    # snapshot: not-callable
    raise NotImplemented()
error[not-callable]: `NotImplemented` is not callable
 --> src/mdtest_snippet.py:9:11
  |
9 |     raise NotImplemented()
  |           --------------^^
  |           |
  |           Did you mean `NotImplementedError`?

map with generic callbacks

from ty_extensions._internal import Unknown
import re

def _(s: Unknown | str):
    escaped = map(re.escape, s)
    reveal_type(escaped)  # revealed: map[str]
    "".join(escaped)

def _(xs: Unknown | list[str]):
    escaped = map(re.escape, xs)
    reveal_type(escaped)  # revealed: map[str]
    tokens: list[Unknown | str] = []
    tokens.extend(escaped)

Failed map calls retain their result type

When the argument count identifies a single map overload, an incompatible callback still produces its usual error. The mapped values retain the callback's return type and do not produce an additional error when called.

class Function:
    def __init__(self, value: str) -> None: ...
    def __call__(self) -> None: ...

# error: [invalid-argument-type]
for function in map(Function, [object()]):
    function()

Constructing a dictionary from gradual-length tuples

Each tuple supplied to dict must contain two elements. A gradual tuple can materialize to that length, and so its element type supplies both the key and value types.

from typing import Any

def _(values: list[tuple[Any, ...]]):
    reveal_type(dict(values))  # revealed: dict[Any, Any]

Failed dict calls do not expose internal type variables

Several dict overloads accept one positional argument. When none matches, an arbitrarily selected overload must not make an otherwise compatible return type fail.

[analysis]
strict-generic-narrowing = true
from collections.abc import Mapping

def copy(value: object) -> dict[str, str]:
    if isinstance(value, Mapping):
        # TODO: This should error. See https://github.com/astral-sh/ty/issues/4645.
        return dict(value)

    return {}

Failed dict calls preserve narrowed mapping types

An invalid dict call must not invalidate an assignment inside a branch where the original value has already been narrowed to a mapping.

[analysis]
strict-generic-narrowing = true
from collections.abc import Mapping

def clean(value: dict[str, int] | str | None) -> None:
    if isinstance(value, Mapping):
        # TODO: This should error. See https://github.com/astral-sh/ty/issues/4645.
        value = dict(value)
        for key, item in value.items():
            value[key] = item

dict keyword arguments with a shadowed typing module

[environment]
python-version = "3.10"

An empty first-party typing module hides the definitions that make dict generic. Calls with one or more named keyword arguments still check their values and recover with Unknown, just like dictionary literals.

typing.py:

main.py:

reveal_type(dict(a=1))  # revealed: Unknown
reveal_type(dict(a=1, b=2))  # revealed: Unknown
reveal_type({"a": 1})  # revealed: Unknown

# error: [unresolved-reference]
dict(a=1, b=missing)

Empty dict calls with a non-generic stub

An empty call to a non-generic dictionary class is rejected if its constructor requires an argument.

[environment]
typeshed = "/typeshed"

/typeshed/stdlib/builtins.pyi:

class object: ...
class int: ...

class dict:
    def __init__(self, value: int) -> None: ...
dict(1)

dict()  # error: [missing-argument] "No argument provided for required parameter `value`"

dict keyword arguments that violate a type variable bound

A custom typeshed can constrain dictionary values. A value that violates the bound is rejected, and later keyword values are still checked.

[environment]
python-version = "3.12"
typeshed = "/typeshed"

/typeshed/stdlib/builtins.pyi:

class object: ...
class str: ...
class int: ...

class dict[K, V: int]:
    def __init__(self, **kwargs: V) -> None: ...
dict(a=1)

# error: [invalid-argument-type] "does not satisfy upper bound `int`"
# error: [unresolved-reference]
dict(a="oops", b=missing)

Failed inner OrderedDict calls do not invalidate outer constructors

Constructing an OrderedDict from a list containing both strings and floats is already rejected. That failure must not cause a second error when the resulting value is passed to another OrderedDict constructor.

from collections import OrderedDict

items = [OrderedDict([["key", 1.0]])]  # error: [no-matching-overload]
OrderedDict(zip(["name"], items))

Contextual inference for next

The iterator determines the result type of next. Returning that int from a method annotated with str is an error.

[rules]
unsound-return = "error"
class Counter:
    def __next__(self) -> int:
        return 1

    def text(self) -> str:
        return next(self)  # error: [invalid-explicit-return]

    def read(self) -> int | None:
        return reveal_type(next(self))  # revealed: int

Unreachable nested calls in a union context

A failed call in an unreachable branch does not prevent narrowing the return context of list.

def takes_no_parameters() -> None: ...

# revealed: list[int | str] | list[int | None]
result: list[int | str] | list[int | None] = reveal_type(list((1, 2, 3) if True else takes_no_parameters(1)))