Skip to content

Fix instantiating a class that fulfills an expectation its base-class missed - #532

Merged
Paebbels merged 1 commit into
devfrom
claude/extendedtype-expects-new
Oct 11, 2026
Merged

Paebbels merged 1 commit into
devfrom
claude/extendedtype-expects-new

Conversation

@pytooling-claude

Copy link
Copy Markdown

Bug Fixes

  • pyTooling.MetaClasses:
    • A class that fulfilled an expectation its base-class missed (a mixin-class' expects naming a member only the derived class defines) couldn't be instantiated with parameters, and neither could a class derived from it:

      class Root(metaclass=ExtendedType, slots=True):
        _a: int
        def __init__(self, a): self._a = a
      
      class Mx(metaclass=ExtendedType, mixin=True, expects=("Run",)):
        pass
      
      class Mid(Root, Mx):        # misses 'Run'
        pass
      
      class Leaf(Mid):
        def Run(self): pass
      
      Leaf(1)   # TypeError: object.__new__() takes exactly one argument (the type to instantiate)
      

      ExtendedType replaced Mid.__new__ by one raising UnfulfilledExpectationError, and put object.__new__ back into Leaf's dictionary. That hits python/cpython#105888: Leaf keeps the inherited slot_tp_new, so object.__new__ rejects every parameter. A constructor without parameters worked.

    • The path for a class that isn't abstract anymore already worked around it with wrapped_new, which calls object.__new__ with the class only. Both paths share that workaround now in ExtendedType._restoreNewMethod.

    • Mid(1) still raises UnfulfilledExpectationError with the same message and notes.

Known Issues

  • Instance creation of such a class without parameters is slower, because __new__ is a Python function now, as for a concrete class derived from an abstract one. Median of 4 interleaved rounds (best of 7 × 200 000), dev → this PR:

    Case 3.14 3.13
    Leaf() 189 → 253 ns 107 → 174 ns
    Leaf(1) raised → 312 ns raised → 204 ns
    Concrete() (derived from an abstract class, for comparison) 270 → 264 ns 175 → 174 ns
    Root(), Root(1) (control) ±3 % ±1 %

    Class creation is unchanged within noise (±7 %). Avoiding the Python-level __new__ needs the same decision as for concrete subclasses of abstract classes (ExtendedType: speed analysis and improvements #531).

Documentation

  • doc/News.rst: entry under Bug Fixes.

Unit Tests

  • New testcase class Instantiation in tests/unit/MetaClasses/ExpectedMembers.py: positional and keyword parameters through the fulfilling class, a class derived from it, an abstract class between the incomplete and the fulfilling class, a constructor without parameters, and the incomplete class still rejecting instantiation with the same message. The four tests passing parameters fail on dev.

Related Issues and Pull-Requests

@pytooling-claude pytooling-claude Bot added bug Something isn't working MetaClass Generally about meta-classes. ExtendedType A meta-class extending 'type'. labels Oct 8, 2026
@pytooling-claude
pytooling-claude Bot requested a review from Paebbels as a code owner October 8, 2026 09:30
@codacy-production

codacy-production Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Not up to standards ⛔

🔴 Issues 1 minor

Alerts:
⚠ 1 issue (≤ 0 issues of at least minor severity)

Results:
1 new issue

Category Results
UnusedCode 1 minor

View in Codacy

🟢 Metrics 14 complexity · 0 duplication

Metric Results
Complexity 14
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@codecov

codecov Bot commented Oct 8, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (dev@165db01). Learn more about missing BASE report.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@          Coverage Diff           @@
##             dev     #532   +/-   ##
======================================
  Coverage       ?   87.18%           
======================================
  Files          ?       59           
  Lines          ?    14670           
  Branches       ?     2279           
======================================
  Hits           ?    12790           
  Misses         ?     1488           
  Partials       ?      392           
Flag Coverage Δ
unittests 87.18% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

… missed

A class fulfilling an expectation its base-class missed (a mixin-class'
'expects' naming a member only the derived class defines) couldn't be
instantiated with parameters, and neither could a class derived from it:

    TypeError: object.__new__() takes exactly one argument

'_wrapNewMethodIfExpectationUnfulfilled' put 'object.__new__' back into the
class' dictionary, which hits python/cpython#105888: the class keeps
'slot_tp_new', and 'object.__new__' then rejects every parameter.
'_wrapNewMethodIfAbstract' already worked around it with 'wrapped_new'.
Both paths share that workaround now in '_restoreNewMethod'.

Instance creation (ns, median of 4 interleaved rounds, best of 7 x 200k,
core 6), dev -> branch:
- 3.14: Leaf() 189 -> 253, Leaf(1) raised -> 312; a plain class and a
  concrete class derived from an abstract one unchanged (+-3 %).
- 3.13: Leaf() 107 -> 174, Leaf(1) raised -> 204; controls unchanged.
A zero-argument constructor of such a class now pays the Python-level
'__new__' like a class that isn't abstract anymore (Concrete(): 264 / 174).
Class creation: unchanged within noise (+-7 %).

Co-Authored-By: Patrick Lehmann <Paebbels@gmail.com>
@pytooling-claude
pytooling-claude Bot force-pushed the claude/extendedtype-expects-new branch from e1557cf to 64832f8 Compare October 11, 2026 21:37
@Paebbels
Paebbels merged commit 13056bb into dev Oct 11, 2026
15 of 20 checks passed
@Paebbels
Paebbels deleted the claude/extendedtype-expects-new branch October 11, 2026 21:42
This was referenced Oct 20, 2026
@pytooling-claude pytooling-claude Bot added this to the v10.0 milestone Oct 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working ExtendedType A meta-class extending 'type'. MetaClass Generally about meta-classes.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants