New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Passing a NamedTuple **class** rather than instance to .dumps
results in an error
#301
Comments
What is the intended use case of passing such a nametuple class through the encoder? What do you expect to happen here other than perhaps a different error message? |
I'm working on capturing all the arguments of a given function call and serializing them to JSON. Since the arguments can be anything, I'd expect simplejson to encode builtin objects and call the For example, if I pass in a random class (not NamedTuple), then simplejson would call |
This is implemented in v3.18.0 |
simplejson has functionality to encode namedtuple instances using their
_asdict()
method. However, when passing the underlying namedtuple type tojson.dumps
, there's an error:._asdict() missing 1 required positional argument: 'self'
simplejson/simplejson/_speedups.c
Lines 385 to 399 in 8dce5b0
The reason is that
_is_namedtuple
only checks for the existence of a method named_asdict()
. Calling_asdict()
from the object instance requires no further arguments asself
is already provided. However, calling_asdict()
from the namedtuple class requires an additional argument,self
.To fix this,
_is_namedtuple
function should further check that the object is an instance rather than a class, or that the_asdict()
function is indeed callable with zero arguments.The text was updated successfully, but these errors were encountered: