question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Motivation

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:open
  • Created 2 years ago
  • Reactions:3
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
deckar01commented, Sep 15, 2021

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).

@meta(
    type_='tests',
    include=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'I', 'j', 'k', ...],
)
class TestSchema(Schema):
    foo = fields.String()
    bar = fields.String()
@meta(type_='tests')
@meta(include=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'I', 'j', 'k', ...])
class TestSchema(Schema):
    foo = fields.String()
    bar = fields.String()
0reactions
deckar01commented, May 24, 2022

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found