Skip to content

The misuse of smart union in pydantic #72

@numb3r3

Description

@numb3r3

The string value "1" in doc.tags will be automatically converted to bool True when using .to_pydantic_model. This would not make sense.

The reproduce script:

from docarray import Document

doc = Document(tags={'a': '1', 'b': 1, 'c': '2'})
print(f'origin:')
print(doc.tags)

pd_doc_tags = doc.to_pydantic_model().tags
print(f'pydantic: ')
print(pd_doc_tags)


proto_tags = doc.to_dict(protocol='protobuf')['tags']
print(f'protobuf: ')
print(proto_tags)


json_tags = doc.to_dict(protocol='jsonschema')['tags']
print(f'jsonschema')
print(proto_tags)

yields the following output:

origin:
{'a': '1', 'b': 1, 'c': '2'}
pydantic: 
{'a': True, 'b': True, 'c': 2.0}
protobuf: 
{'a': '1', 'c': '2', 'b': 1.0}
jsonschema
{'a': '1', 'c': '2', 'b': 1.0}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions