Inherit __init_subclass__ args to subclass
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.1k
- Forks
- 193
- Avg merge
- 3d 20h
- Merged PRs (30d)
- 15
Description
I'm using msgspec for API response validation, so I wrote this:
class Response(Struct, frozen=True, kw_only=True):
pass
So all of the responses should be created by passing kwargs, and they can't be modified by accident.
But when I use this:
class SomeResponse(Response):
field: int
test = SomeResponse(1) # successed, oops...
Seems that args for __init_subclass__ can't be inherited.
So how can I do to implement this? Should I use metaclass or something else?
Currently I'm using this:
RESPONSE_CONFIG = {
"kw_only": True,
"frozen": True
}
class SomeResponse(Struct, **RESPONSE_CONFIG):
field: int
It works, but ugly and I can easily forget to add CONFIG, which result in non-restricted response struct.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the Response and SomeResponse examples and inspect Struct's init_subclass handling. Determine the intended inheritance semantics for kw_only and frozen, then add focused tests showing that subclass construction remains keyword-only and immutable when those options are set on the parent.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend-api-design
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100