Skip to content

feat: int subclasses get int's arithmetic, and a class asks __get__ - #277

Merged
jonruttan merged 1 commit into
mainfrom
feat/int-subclass-get
Nov 29, 2026
Merged

jonruttan merged 1 commit into
mainfrom
feat/int-subclass-get

Conversation

@jonruttan

Copy link
Copy Markdown
Owner

The second runtime PR enum needs, after #275. IntEnum and IntFlag are classes over int, and enum's member lookups read descriptors from the class.

Int subclasses

  • int now has the methods a subclass inherits:
    • the arithmetic, bitwise and unary operators, with their reflected forms;
    • the comparisons;
    • __int__, __index__, __float__, __bool__ and __hash__.
  • Each works on the int the instance carries and answers a plain int, as CPython's does.
  • An operand that isn't a number gets NotImplemented, so the other side's reflected method is tried, and the error names the subclass ('C' and 'str').
  • A plain number on the left runs its own op first unless the right operand's class derives from its type, which is CPython's order. 1 + C(2) asks C.__radd__ first; 1.5 + C(2) is float's add.
  • int also gains real, imag, numerator, denominator, conjugate(), as_integer_ratio() and is_integer(). Plain ints had none of them, and now answer them too.

Other protocols this needed

  • __index__ is now read where an int index is: sequence subscripts, repeat counts ('ab' * C(2)), %o/%x/%X, bin/hex/oct and chr.
  • round() asks __round__ first, and otherwise rounds the number an object carries. With neither it raises CPython's "type X doesn't define round method".
  • Format specs: a non-empty spec on an object that carries a number formats the number, as int.__format__ does ('{:03d}'.format(C(6))).
  • divmod dispatches __divmod__ and __rdivmod__.

__get__ from a class

  • A descriptor the program wrote is now asked __get__(None, cls) when it's read from a class, as it already was from an instance and through super. C.__dict__['d'] and vars(C) still give the raw descriptor.

Testing

  • New spec C3-int-subclass with five cases, outputs from CPython 3.14.
  • Suite: 1308 tests, 0 failed.
  • Conformance: 644 tests with 3 failures (array_int_repr, class_str, fun_code_colines), all from the known set.
  • if-ladder check is clean.

Not done yet: float and str subclasses don't get the matching rows. Enum doesn't need them.

🤖 Generated with Claude Code

A class over int now gets int's operators. int gains the methods a
subclass inherits: the arithmetic, bitwise and unary operators with
their reflected forms, comparisons, __int__, __index__, __float__,
__bool__ and __hash__, plus real, imag, numerator, denominator,
conjugate, as_integer_ratio and is_integer, which plain ints answer
too. Each works on the int the instance carries and answers a plain
int. An operand that is no number gets NotImplemented, so the error
names the subclass. A plain number on the left goes first unless the
right's class derives from its type, as in CPython.

__index__ is now read where an int index is: sequence subscripts,
repeat counts, %o/%x/%X, bin/hex/oct and chr. round() asks
__round__, and a format spec on an object that carries a number
formats the number.

A descriptor the program wrote is now asked __get__(None, cls) when
it is read from a class, as it already was from an instance and
through super.

C3-int-subclass covers it, with outputs from CPython 3.14.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jonruttan
jonruttan merged commit cb0838d into main Nov 29, 2026
12 checks passed
@jonruttan jonruttan mentioned this pull request Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant