marshmallow-code / marshmallow-code/marshmallow

Add decorator syntax for method fields?

Open
#738 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feedback welcome
Dominant language
Python
Stars
7.2k
Forks
738
Avg merge
1d 23h
Merged PRs (30d)
7

Description

The current syntax for method fields is somewhat awkward, especially around naming:

```python
class MySchema(Schema):
is_valid = fields.Method('get_is_valid')

def get_is_valid(obj):
return something_something_something(obj)
```

At a slight cost to consistency, we could make this more ergonomic with syntax like:

```python
class MySchema(Schema):
@fields.method
def is_valid(obj):
return something_something_something(obj)
```

And/or:

```python
class MySchema(Schema):
@fields.method(dump_to='foo')
def is_valid(obj):
return something_something_something(obj)
```

Does this sound reasonable?

If so, is the naming okay? And should `@fields.method` be allowed as shorthand when not passing any other arguments in setting up the field, or should it always be `@fields.method()`?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no files or tests. Start by locating the existing fields.Method implementation and the Schema field-declaration handling, then review the proposed decorator forms and their naming options. Done would require an agreed API design plus corresponding implementation and test coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.