Python Glossary Русский

Python 3.5+typingterm

typing.SupportsInt

Supports* protocols

A runtime-checkable protocol for objects supporting int() (has __int__).

Syntax

isinstance(obj, typing.SupportsInt)

isinstance(x, SupportsInt) only checks that __int__ exists, not that int(x) will actually succeed. The classic trap: str has no __int__, so isinstance('123', SupportsInt) is False even though int('123') works fine — strings are parsed through a separate path inside the int constructor.

Examples

from typing import SupportsInt
print(isinstance(5, SupportsInt))   # → True
print(isinstance(2.9, SupportsInt))  # → True
print(isinstance('5', SupportsInt))  # → False
from typing import SupportsInt
class Rome: __int__ = lambda self: 4
print(isinstance(Rome(), SupportsInt))  # → True
print(int(Rome()))  # → 4

See also

Open in the full glossarydocs.python.orgReport an inaccuracy