Skip to content

Commit 15f98a8

Browse files
authored
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
1 parent 24c78b9 commit 15f98a8

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

stdlib/builtins.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1295,7 +1295,7 @@ def ascii(obj: object, /) -> str: ...
12951295
def bin(number: int | SupportsIndex, /) -> str: ...
12961296
def breakpoint(*args: Any, **kws: Any) -> None: ...
12971297
def callable(obj: object, /) -> TypeIs[Callable[..., object]]: ...
1298-
def chr(i: int, /) -> str: ...
1298+
def chr(i: int | SupportsIndex, /) -> str: ...
12991299

13001300
# We define this here instead of using os.PathLike to avoid import cycle issues.
13011301
# See https://github.com/python/typeshed/pull/991#issuecomment-288160993

0 commit comments

Comments
 (0)