reagento / adaptix

An extremely flexible and configurable data model conversion library.
https://adaptix.readthedocs.io
Apache License 2.0
366 stars 23 forks source link

A Schema instance used twice uses the first parsed type on the other fields #147

Closed SamWarden closed 2 years ago

SamWarden commented 2 years ago

python3.9.6 dataclass-factory==2.11

If to use a Schema instance twice, a Factory will try to use the parser of the first parsed field on the others, although they can be of a different type

from dataclasses import dataclass

from dataclass_factory import Factory, Schema

@dataclass
class Cls:
    a: tuple
    b: str

schema = Schema()
factory = Factory(schemas={
    str: schema,
    tuple: schema,
})

data = {'a': ('some', 'words'), 'b': 'hello world'}
cls = factory.load(data, Cls)

assert isinstance(cls, Cls) # True
assert isinstance(cls.a, tuple) # True
assert isinstance(cls.b, str), 'cls.b is a tuple' # False

A Factory with schemas for each type parses as expected

factory = Factory(schemas={
    str: Schema(),
    tuple: Schema(),
})

cls = factory.load(data, Cls)

assert isinstance(cls, Cls) # True
assert isinstance(cls.a, tuple) # True
assert isinstance(cls.b, str) # True