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.

Function fields break with Python 3 type annotations

See original GitHub issue

Let’s take the following simple test case:

from marshmallow import fields, Schema

def get_split_words(value: str):
    return value.split(';')

class TestSchema(Schema):
    name = fields.String(required=True)
    friends = fields.Function(serialize=None, deserialize=get_split_words)

data = {'name': 'Bruce Wayne', 'friends': 'Clark;Alfred;Robin'}
result = TestSchema().load(data)

That results in an error:

Traceback (most recent call last):
  File "mmtest.py", line 14, in <module>
    result = TestSchema().load(data)
  File "C:\tools\py35-venv\lib\site-packages\marshmallow\schema.py", line 542, in load
    result, errors = self._do_load(data, many, partial=partial, postprocess=True)
  File "C:\tools\py35-venv\lib\site-packages\marshmallow\schema.py", line 610, in _do_load
    index_errors=self.opts.index_errors,
  File "C:\tools\py35-venv\lib\site-packages\marshmallow\marshalling.py", line 294, in deserialize
    index=(index if index_errors else None)
  File "C:\tools\py35-venv\lib\site-packages\marshmallow\marshalling.py", line 67, in call_and_store
    value = getter_func(data)
  File "C:\tools\py35-venv\lib\site-packages\marshmallow\marshalling.py", line 287, in <lambda>
    data
  File "C:\tools\py35-venv\lib\site-packages\marshmallow\fields.py", line 263, in deserialize
    output = self._deserialize(value, attr, data)
  File "C:\tools\py35-venv\lib\site-packages\marshmallow\fields.py", line 1199, in _deserialize
    return self._call_or_raise(self.deserialize_func, value, attr)
  File "C:\tools\py35-venv\lib\site-packages\marshmallow\fields.py", line 1203, in _call_or_raise
    if len(utils.get_func_args(func)) > 1:
  File "C:\tools\py35-venv\lib\site-packages\marshmallow\utils.py", line 344, in get_func_args
    return inspect.getargspec(func).args
  File "C:\tools\Python35-32\lib\inspect.py", line 1045, in getargspec
    raise ValueError("Function has keyword-only arguments or annotations"
ValueError: Function has keyword-only arguments or annotations, use getfullargspec() API which can support them

The reason is: The marshmallow.utils module uses Python’s inspect.getargspec for inspecting the functions used in Function fields. However, getargspec is deprecated since Python 3.0 (https://docs.python.org/3/library/inspect.html#inspect.getargspec) and throws a ValueError when called on a function with type annotations.

The error message suggests to use getfullargspec instead, but that has been deprecated since version 3.5, too (https://docs.python.org/3/library/inspect.html#inspect.getfullargspec). So the most future-proof way is probably to use inspect.signature instead (https://docs.python.org/3/library/inspect.html#inspect.signature).

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
sabinemcommented, Nov 4, 2016

I would like to work on this, if that’s okay with the community. I am a Newbie with contributing, so tell me if I am doing anything the wrong way. But I have been told that you should announce if you want to work on something. This seems like a bug and should be not too hard to fix.

0reactions
sloriacommented, Nov 18, 2016

This is now fixed in the latest PyPI release (2.10.4).

Read more comments on GitHub >

github_iconTop Results From Across the Web

typing — Support for type hints — Python 3.11.1 documentation
The Python runtime does not enforce function and variable type annotations. They can be used by third party tools such as type checkers,...
Read more >
Type hints cheat sheet - mypy 0.991 documentation
Type hints cheat sheet#. This document is a quick cheat sheet showing how to use type annotations for various common types in Python....
Read more >
Understanding type annotation in Python - LogRocket Blog
To annotate return value type, add -> immediately after closing the parameter parentheses, just before the function definition colon( : ): def ...
Read more >
Python Type Checking (Guide) - Real Python
Function Annotations ; Variable Annotations; Type Comments ... 6RANKS = "2 3 4 5 6 7 8 9 10 J Q K A".split()...
Read more >
Using Python's Type Annotations - DEV Community ‍ ‍
Annotating Functions & Methods ... We can use the expected variable's type when writing and calling functions to ensure we are passing and...
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