Files
Carl Meyer 9511127d1f [ty] Fix partial application of ParamSpec decorators (#30150)
ty rejected `partial(decorator, option=value)` when the decorator
accepted and returned `Callable[P, str]`. It reported that `P.args` was
incompatible with itself. With this change, binding the option leaves
the decorator free to capture the decorated function’s signature.

The solver tracked `P.args` and `P.kwargs` separately and could overlook
the dependency on `P` when removing the decorator’s local type variables
from a constraint. Record that dependency explicitly. For a specialized
return-type alias, visit the arguments that affect the alias’s value so
the solver can substitute the caller’s parameter list there too.

Fixes astral-sh/ty#4973.

## Test plan

Bisected the reported example to #29543 and checked the fix against the
same example. ty now infers `(name: str) -> str` for the decorated
function. I also compared the semantic mdtests under eight alternate
constraint orderings with the base revision. Each ordering produced the
same failing assertions and diagnostics on both revisions.


## Ecosystem report

At `a3e66405a2b5c4b9062803edbed4ddd45ab2c074`, all seven diagnostic
changes were expected. Five Normal and Uniform property assertions and
two Binomial property assertions now infer the one-dimensional arrays
specified by scipy-stubs, with the correct dtypes. The ecosystem
comparison is clear for review. [Full
report](https://github.com/astral-sh/ruff/pull/30150#issuecomment-6702172316)
2026-11-09 12:33:37 +01:00

3.5 KiB

numpy

[environment]
python-version = "3.13"
python-platform = "linux"

[project]
dependencies = ["numpy==2.4.6"]

Basic usage

import numpy as np

xs = np.array([1, 2, 3])
# TODO: should be `ndarray[tuple[Any, ...], dtype[Any]]`
reveal_type(xs)  # revealed: ndarray[tuple[Any, ...], dtype[Unknown]]

xs = np.array([1.0, 2.0, 3.0], dtype=np.float64)
reveal_type(xs)  # revealed: ndarray[tuple[Any, ...], dtype[float64]]

Explicit dtypes remain distinct when checking an array against a parameter annotation. This is a regression test for https://github.com/astral-sh/ty/issues/3199:

def takes_float16(values: np.ndarray[tuple[int, ...], np.dtype[np.float16]]) -> None: ...

float32_values = np.array([1, 2, 3], dtype=np.float32)
reveal_type(float32_values)  # revealed: ndarray[tuple[Any, ...], dtype[floating[_32Bit]]]

float16_values = np.array([1, 2, 3], dtype=np.float16)
reveal_type(float16_values)  # revealed: ndarray[tuple[Any, ...], dtype[floating[_16Bit]]]

takes_float16(float32_values)  # error: [invalid-argument-type]
takes_float16(float16_values)

An explicit integer dtype is also preserved through array, allowing interp to select its array overload. This is a regression test for https://github.com/astral-sh/ty/issues/1429:

values = np.array([0, 1, 2], dtype=np.int64)
reveal_type(values)  # revealed: ndarray[tuple[Any, ...], dtype[signedinteger[_64Bit]]]

interpolated = np.interp(values, values, values)
reveal_type(interpolated)  # revealed: ndarray[tuple[Any, ...], dtype[float64]]

Iterating over an array union

An array can satisfy map's iterable parameter through its gradual iterator overload, even when another overload yields multidimensional subarrays, not tuples.

from typing import Any
import numpy as np

def prepare(pair: tuple[int | Any, int | Any]) -> None:
    pass

def process(padding):
    if len(padding) == 0:
        padding = np.zeros((0, 2), dtype=np.int64)

    return list(map(prepare, padding))

Phantom property inference

An overloaded helper method can encode a type mapping for a property whose own receiver is a protocol. For a nonempty shape, the property returns an array with the receiver's shape and dtype.

from typing import Generic, Protocol, TypeVar, overload, type_check_only
import numpy as np

type Array[Shape: tuple[int, ...], Scalar: np.generic] = np.ndarray[Shape, np.dtype[Scalar]]

FloatT_co = TypeVar("FloatT_co", bound=np.generic, covariant=True)
ShapeT_co = TypeVar("ShapeT_co", bound=tuple[int, ...], covariant=True)

@type_check_only
class HasPhantomParameter[T](Protocol):
    def phantom_parameter(self) -> T: ...

@type_check_only
class PhantomParameterMixin(Generic[ShapeT_co, FloatT_co]):
    @type_check_only
    @overload
    def phantom_parameter[ScalarT: np.generic](
        self: "PhantomParameterMixin[tuple[()], ScalarT]",
    ) -> ScalarT: ...
    @type_check_only
    @overload
    def phantom_parameter[ShapeT: tuple[int, *tuple[int, ...]], ScalarT: np.generic](
        self: "PhantomParameterMixin[ShapeT, ScalarT]",
    ) -> Array[ShapeT, ScalarT]: ...

class Distribution(Generic[FloatT_co, ShapeT_co]): ...

class Normal(
    Distribution[FloatT_co, ShapeT_co],
    PhantomParameterMixin[ShapeT_co, FloatT_co],
    Generic[ShapeT_co, FloatT_co],
):
    @property
    def value[T](self: HasPhantomParameter[T]) -> T: ...

def make_normal() -> Normal[tuple[int], np.float32]: ...

reveal_type(make_normal().value)  # revealed: ndarray[tuple[int], dtype[floating[_32Bit]]]