Meta Decorator
See original GitHub issueMotivation
I often have lots of small schemas containing unique Meta
classes with only a few attributes. The Meta
class sometimes requires more lines than the schema definition and makes a file containing multiple schemas harder to read. This is especially true when using marshmallow-jsonapi
due to type_
.
Proposal
Provide a meta
decorator that will inject its kwargs into a Meta
class for the class it is wrapping.
Example
Before:
class TestSchema(Schema):
foo = fields.String()
bar = fields.String()
class Meta:
type_ = 'tests'
ordered = True
After:
@meta(type_='tests', ordered=True)
class TestSchema(Schema):
foo = fields.String()
bar = fields.String()
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:7 (6 by maintainers)
Top Results From Across the Web
Documentation - Decorators - TypeScript
TypeScript Decorators overview. ... Decorators provide a way to add both annotations and a meta-programming syntax for class declarations and members.
Read more >Meta decorator with parameters, defined in explicit functions
I want an explicit replacement for a common decorator idiom. There is a clever one-line decorator that has been copy-pasted without explanation ...
Read more >Decorators, Callables, and Function Metadata in Python
We are ready to explore more on decorators in this discussion using metadata and callables. Function Metadata. Metadata is maintained within a function...
Read more >Meta-Programming in Python
A decorator is a way of adding new functionality to an existing function without modifying its original structure. For instance, we have these...
Read more >Advanced use of Python decorators and metaclasses
This post wants to show how to use metaclasses and decorators to create a powerful class that can be inherited and customized by...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Inheritance is another good use case where
Meta
is likely to only need a few attributes. I was initially imagining spreading the parent meta manually like@meta(..., **vars(ParentSchema.Meta))
, but that doesn’t actually work. Implicitly inheriting into a new class would be convenient.A use case for stacking might be if you have enough attributes to justify a meta class, but still preferred the decorator syntax. It would avoid opening and indenting the meta arguments into a block (which a linter might do automatically).
I published a module for this functionality. I think this should just be a community library for now. Once I cover it with tests I will add it to the wiki and close this issue.
https://github.com/deckar01/marshmallow-meta