From 15f98a8ab8708d41ebf1bdca82996819bd7a8931 Mon Sep 17 00:00:00 2001 From: David Peter Date: Wed, 12 Feb 2025 08:50:42 +0100 Subject: [PATCH] `chr`: Accept `SupportsIndex` argument (#13494) The `chr` function does accept objects that implement the `SupportsIndex` protocol. The [implementation] of the builtin method calls `PyLong_AsLongAndOverflow` on the argument, which in turn calls `__index__()`, if present. The parameter of the `chr` function can therefore be annotated with `int | SupportsIndex`. It seems to me like `SupportsIndex` alone would be enough, since `int` implements `SupportIndex`, but I chose `int | SupportIndex` to make it consistent with the annotations on `hex`, `oct` and `bin`. [implementation]: https://github.com/python/cpython/blob/b05fa90b21dd01bb836285cdd41920320b09e681/Python/bltinmodule.c#L725 [PyLong_AsLongAndOverflow]: https://docs.python.org/3.13/c-api/long.html#c.PyLong_AsLongAndOverflow --- stdlib/builtins.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index b0912b887..0a6dc57b0 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -1295,7 +1295,7 @@ def ascii(obj: object, /) -> str: ... def bin(number: int | SupportsIndex, /) -> str: ... def breakpoint(*args: Any, **kws: Any) -> None: ... def callable(obj: object, /) -> TypeIs[Callable[..., object]]: ... -def chr(i: int, /) -> str: ... +def chr(i: int | SupportsIndex, /) -> str: ... # We define this here instead of using os.PathLike to avoid import cycle issues. # See https://github.com/python/typeshed/pull/991#issuecomment-288160993