Repository navigation
Conversation
…nd float At runtime, IntElement and FloatElement are not subclasses of int and float: they implement numeric operators by applying them to the .pyval of both operands, and lack every non-dunder method and attribute of int and float. Inheriting from int and float in the stubs meant that isinstance(elem, int) narrowed incorrectly, that elements were accepted where int or float is required (json.dumps(), format(), round() all fail at runtime), and that names like .real or .is_integer were typed as int/float attributes instead of child element lookups. It also needed ignore comments for every type checker, since _Element and int/float are incompatible disjoint bases. Declare the dunders explicitly instead, following typeshed's int and float, with number elements accepted as the other operand: - NumberElement: conversions, hashing, equality and truthiness, which all its subclasses share. - IntElement: __index__, arithmetic, bitwise and comparison operators, with int, float or complex results depending on the other operand. - FloatElement: the same minus the bitwise operators, which fail at runtime. - BoolElement: widen the &, | and ^ overloads to accept IntElement, and add their reflected versions (part of abelcheung#13). The 3-argument __pow__ overload comes first and FloatElement.__rdivmod__ accepts IntElement, so that pow(IntElement, int, int) and divmod(IntElement, FloatElement) match typeshed's protocols. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This branch has not been deployed
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.
At runtime,
IntElementandFloatElementare not subclasses ofintandfloat: they implement numeric operators by applying them to the.pyvalof both operands, and lack every non-dunder method and attribute ofintandfloat. Inheriting fromintandfloatin the stubs meant thatisinstance(elem, int)narrowed incorrectly, that elements were accepted whereintorfloatis required (json.dumps(),format(),round()all fail at runtime), and that names like.realor.is_integerwere typed asint/floatattributes instead of child element lookups. It also needed ignore comments for every type checker, since_Elementandint/floatare incompatible disjoint bases.Declare the dunders explicitly instead, following typeshed's
intandfloat, with number elements accepted as the other operand:NumberElement: conversions, hashing, equality and truthiness, which all its subclasses share.IntElement:__index__, arithmetic, bitwise and comparison operators, withint,floatorcomplexresults depending on the other operand.FloatElement: the same minus the bitwise operators, which fail at runtime.BoolElement: widen the&,|and^overloads to acceptIntElement, and add their reflected versions (part of Reverse operator on BoolElement return incorrect result #13).The 3-argument
__pow__overload comes first andFloatElement.__rdivmod__acceptsIntElement, so thatpow(IntElement, int, int)anddivmod(IntElement, FloatElement)match typeshed's protocols.