[Python] Fix allOf inheritance by using **kwargs throughout inheritance chain#1393
Open
ericfitz wants to merge 1 commit intoswagger-api:masterfrom
Open
[Python] Fix allOf inheritance by using **kwargs throughout inheritance chain#1393ericfitz wants to merge 1 commit intoswagger-api:masterfrom
ericfitz wants to merge 1 commit intoswagger-api:masterfrom
Conversation
…ce chain
When a child model extends a parent via allOf, the generated __init__
method was consuming parameters from kwargs, preventing them from
reaching parent classes. This caused deserialization failures.
The fix changes how inheritance is handled:
1. Child classes (with parent) now use **kwargs for all parameters:
- Extract values with kwargs.get('name')
- Pass the full **kwargs to parent (nothing consumed/removed)
2. Root classes (without parent) now accept **kwargs:
- Keep named parameters for clean API
- Add **kwargs to absorb extra parameters from child classes
Before (broken):
```python
class Child(Parent):
def __init__(self, name=None, type=None, child_only=None, *args, **kwargs):
self.name = name # consumed from kwargs
self.type = type # consumed from kwargs
Parent.__init__(self, *args, **kwargs) # name, type NOT passed!
```
After (fixed):
```python
class Child(Parent):
def __init__(self, **kwargs):
name = kwargs.get('name')
type = kwargs.get('type')
child_only = kwargs.get('child_only')
self.name = name
self.type = type
Parent.__init__(self, **kwargs) # full kwargs passed!
class Parent:
def __init__(self, name=None, type=None, **kwargs): # accepts extra kwargs
self.name = name
self.type = type
```
This ensures kwargs flows through the entire inheritance chain.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
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.
Summary
When a child model extends a parent via
allOf, the generated__init__method was consuming parameters from kwargs, preventing them from reaching parent classes. This caused deserialization failures with errors like:ValueError: Invalid value for 'name' (None), must not be 'None'TypeError: Parent.__init__() got an unexpected keyword argument 'child_field'The Problem
Consider this OpenAPI spec with inheritance:
The old generated code had two problems:
Problem 1: Python extracts named parameters from
**kwargsinto local variables, so whenParent.__init__is called, those parameters are no longer in kwargs.Problem 2: Root classes didn't accept
**kwargs, so if a child class has fields the parent doesn't know about, it fails with "unexpected keyword argument".The Fix
For child classes (with parent): Use
**kwargsfor all parameters and extract withkwargs.get():For root classes (without parent): Add
**kwargsto accept extra parameters:This ensures the full kwargs dict flows through the entire inheritance chain, and each class extracts what it needs without breaking the chain.
Test Plan
allOfinheritanceDiagnosis Notes
This bug manifests as errors like:
ValueError: Invalid value for 'property_name' (None), must not be 'None'ValueError: Invalid value for 'property_name' (None), must be one of [...]Repro Notes
🤖 Generated with Claude Code
🧍🏻Manually reviewed and edited by a real human, not just AI slop