Skip to content

Unexpected side effects if dump is defined in the data class when using schema.dump #77

@sveinse

Description

@sveinse

Running py 3.8 on win10, 32-bit. Using desert 7ca1e23 from git.

When making a schema from data class A, calling schema.dump(a) to serialize an object a, will fail the serialization if class A contains a method named dump.

def test_calling_dump():
    @attr.s
    class A:
        x: int = attr.ib(default=0)
        def dump(self, *args, **kw):
            raise Exception(f"A.dump({args},{kw}) should not be called")

    schema = desert.schema(A)
    a = A()
    schema.dump(a)

If it happens that the defintion of A.dump() is incompatible with schema.dump(), a very strange error is returned that is confusing at best:

  File "c:\sveinse\myfile\settings.py", line 153, in dumps
    load = schema.dump(self)
TypeError: dump() takes 1 positional argument but 2 were given

The calling of methods in the data class from the schema object can be extended to any arbitrary function names, e.g. foof as shown below. Note that if schema.foof(a) is called, then A.foof will be called with arguments foof(schema_object, self), not adhering to the defined prototype def foof(self,...). This can lead to very unexpected results.

def test_calling_foof():
    @attr.s
    class A:
        x: int = attr.ib(default=0)
        def foof(self, *args, **kw):
            raise Exception(f"A.foof({type(self)},{args},{kw}) should not be called")

    schema = desert.schema(A)
    a = A()
    schema.foof(a)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions