Skip to content

v0.1.3: builtin subclass arithmetic (V2 part 2)#270

Merged
tamnd merged 1 commit into
mainfrom
v0.1.3
Apr 29, 2026
Merged

v0.1.3: builtin subclass arithmetic (V2 part 2)#270
tamnd merged 1 commit into
mainfrom
v0.1.3

Conversation

@tamnd

@tamnd tamnd commented Apr 29, 2026

Copy link
Copy Markdown
Owner

Summary

  • class N(int): pass; N(5) + 1 returns 6, N(5).bit_length() works, isinstance(N(5), int) is True, {N(5): "a"}[5] returns "a", and S("hi").upper() returns "HI". Same for str / list / dict / bytes subclasses.
  • Builtin types stay as *object.BuiltinFunc. Subclass instances carry a BuiltinValue payload constructed at instantiation; after every dunder dispatch the binary/unary/compare/contains/getitem paths call unboxBuiltin so the type-specific Go switches handle the unboxed operands. Results are plain builtin types unless the class overrides the dunder — matches CPython.
  • Closes V2 part 2 of roadmap 1541. Spec at notes/Spec/1500/1544_goipy_v0103_builtin_subclass_arith.md. v0.1.4 lands the typing fallout.

Test plan

  • go build ./...
  • go test ./vm/...
  • Fixture 349 (internal/testdata/349_builtin_subclass_arith.py) matches CPython 3.14 byte-for-byte
  • No regression on existing 348 fixtures (full suite green)

Class N(int): pass; N(5) + 1 returns 6, N(5).bit_length() works,
isinstance(N(5), int) is True, {N(5): "a"}[5] returns "a", and
S("hi").upper() returns "HI". Same for str/list/dict/bytes
subclasses.

Builtin types stay as *object.BuiltinFunc; instead the subclass
instance carries a BuiltinValue payload built at construction time.
After dunder dispatch, binary/unary/compare/contains/getitem paths
unbox both operands so the type-specific Go switches handle them
transparently. Operator results are plain builtin types unless the
class overrides the dunder, matching CPython.

object.Class gets BuiltinBase + BuiltinMRO; object.Instance gets
BuiltinValue. __build_class__ detects builtin-type bases. Class call
path auto-constructs BuiltinValue from args when no user __init__.
isinstance/issubclass walk the BuiltinBase relation. getAttr falls
back to BuiltinValue so inherited methods work. instance hashes/eq/
repr/str/truthy fall through to the payload when no override.

Spec at notes/Spec/1500/1544_goipy_v0103_builtin_subclass_arith.md.
Fixture 349.
@tamnd tamnd merged commit 61f1b4e into main Apr 29, 2026
1 check passed
@tamnd tamnd deleted the v0.1.3 branch April 29, 2026 09:38
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