marshmallow-code / marshmallow-code/marshmallow

v3 no longer accepts fields that call class methods

Open
#1,758 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

While upgrading from v2.21.0 to v3.10.0, I have found a difference in behaviour that I could not find in the Changelog / update docs.

I have created a contrived case to show the issue:
```
import marshmallow

class Test(marshmallow.Schema):
number = marshmallow.fields.Number()

class TestObject(object):
def number(self):
return 1

if marshmallow.__version__.startswith("3"):
print(Test().dump(TestObject()))
else:
print(Test().dump(TestObject()).data)
```

When run in `marshmallow==2.21.0`, the output is:
```
{'number': 1.0}
```

while when run in `marshmallow==3.10.0`, I get this error:
```
TypeError: float() argument must be a string or a number, not 'method'
```

Can anybody shed some light on this difference in behaviour? Is it to be expected or is it a bug?

If it is expected, what is the best way to deal with this for the upgrade?

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

Start by running the supplied marshmallow Schema and TestObject reproduction against versions 2.21.0 and 3.10.0, then trace the dump behavior for the field whose value is a class method. Done means the version difference is explained and the issue has either documented upgrade guidance or a confirmed regression with a focused regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.