Repository navigation
feat: int subclasses get int's arithmetic, and a class asks __get__ - #277
Merged
Merged
Conversation
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>
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
intnow has the methods a subclass inherits:__int__,__index__,__float__,__bool__and__hash__.int, as CPython's does.'C' and 'str').1 + C(2)asksC.__radd__first;1.5 + C(2)is float's add.intalso gainsreal,imag,numerator,denominator,conjugate(),as_integer_ratio()andis_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/octandchr.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".int.__format__does ('{:03d}'.format(C(6))).divmoddispatches__divmod__and__rdivmod__.__get__from a class__get__(None, cls)when it's read from a class, as it already was from an instance and throughsuper.C.__dict__['d']andvars(C)still give the raw descriptor.Testing
C3-int-subclasswith five cases, outputs from CPython 3.14.Not done yet: float and str subclasses don't get the matching rows. Enum doesn't need them.
🤖 Generated with Claude Code