mirror of
https://github.com/astral-sh/ruff.git
synced 2026-11-21 20:29:20 +08:00
## Summary
PEP 484's numeric tower means we represent a `float` annotation as the
semantic union `int | float*` and `complex` as `int | float* |
complex*`. We previously rendered those internal unions directly, which
exposed an implementation detail in user-facing output:
```python
def f() -> float:
return 1
reveal_type(f()) # Before: int | float; after: float
```
This change recognizes complete numeric-tower unions during display and
renders them using their canonical annotation spelling, `float` or
`complex`.
The implementation does _not_ preserve explicit unions, e.g., if the
user writes out `int | float`, we still collapse that rather than trying
to preserve it, mostly for simplicity (as per
https://github.com/astral-sh/ty/issues/2184#issuecomment-4345219164).
Closes https://github.com/astral-sh/ty/issues/2184.
2.2 KiB
2.2 KiB
numpy
[environment]
python-version = "3.14"
numpy's dtype
numpy functions often accept a dtype parameter. For example, one of np.array's overloads accepts
a dtype parameter of type DTypeLike | None. Here, we build up something that resembles numpy's
internals in order to model the type DTypeLike. Many details have been left out.
mini_numpy.py:
from typing import TypeVar, Generic, Any, Protocol, TypeAlias, runtime_checkable, final
import builtins
_ItemT_co = TypeVar("_ItemT_co", default=Any, covariant=True)
class generic(Generic[_ItemT_co]):
@property
def dtype(self) -> _DTypeT_co:
raise NotImplementedError
_BoolItemT_co = TypeVar("_BoolItemT_co", bound=builtins.bool, default=builtins.bool, covariant=True)
class bool(generic[_BoolItemT_co], Generic[_BoolItemT_co]): ...
@final
class object_(generic): ...
_ScalarT = TypeVar("_ScalarT", bound=generic)
_ScalarT_co = TypeVar("_ScalarT_co", bound=generic, default=Any, covariant=True)
@final
class dtype(Generic[_ScalarT_co]): ...
_DTypeT_co = TypeVar("_DTypeT_co", bound=dtype, default=dtype, covariant=True)
@runtime_checkable
class _SupportsDType(Protocol[_DTypeT_co]):
@property
def dtype(self) -> _DTypeT_co: ...
_DTypeLike: TypeAlias = type[_ScalarT] | dtype[_ScalarT] | _SupportsDType[dtype[_ScalarT]]
DTypeLike: TypeAlias = _DTypeLike[Any] | str | None
Now we can make sure that a function which accepts DTypeLike | None works as expected. A generic
function accepting _DTypeLike[_ScalarT] should also infer the scalar type from a scalar class. The
protocol union element describes instances with a dtype property, not the class object whose class
access exposes that property descriptor:
from typing import TypeVar
import mini_numpy as np
def accepts_dtype(dtype: np.DTypeLike | None) -> None: ...
accepts_dtype(dtype=np.bool)
accepts_dtype(dtype=np.dtype[np.bool])
accepts_dtype(dtype=object)
accepts_dtype(dtype=np.object_)
accepts_dtype(dtype="U")
_ScalarT = TypeVar("_ScalarT", bound=np.generic)
def from_dtype_like(value: np._DTypeLike[_ScalarT]) -> np.dtype[_ScalarT]:
raise NotImplementedError
reveal_type(from_dtype_like(np.bool)) # revealed: dtype[mini_numpy.bool[builtins.bool]]