mirror of
https://github.com/davidhalter/typeshed.git
synced 2026-05-06 21:43:59 +08:00
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
This commit is contained in:
+1
-1
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user