lidatong / dataclasses-json

Easily serialize Data Classes to and from JSON
MIT License
1.34k stars 151 forks source link

>0.5.8 issue with beartype>0.10.0 #450

Closed a-mckinley closed 10 months ago

a-mckinley commented 11 months ago

Description

I realise this is not likely to be a real "bug", but I discovered an issue using dataclasses-json in conjunction with beartype>0.10.0 with dataclasses-json >0.5.8.

Beartype complains about:

beartype.roar.BeartypeDecorHintForwardRefException: Forward reference "dataclasses_json.api.SchemaType[A]" syntactically invalid as module attribute name.

Code snippet that reproduces the issue

from dataclasses import dataclass
from dataclasses_json import dataclass_json
from beartype import beartype

@beartype
@dataclass_json
@dataclass
class Testing(object):
    data: str

t = Testing(data='thing')
print(t)

Describe the results you expected

beartype compatibility working

Python version you are using

3.11.3

Environment description

Working environments:

pip install -U beartype==0.15.0 dataclasses_json==0.5.8 pip install -U beartype==0.10.0 dataclasses_json==0.5.13

Failing environment:

pip install -U beartype==0.15.0 dataclasses_json==0.5.13

george-zubrienko commented 11 months ago

@a-mckinley can you try with subclass instead of the annotation?

a-mckinley commented 11 months ago

Hi @george-zubrienko, changing to use the subclass instead of the decorator eliminates the beartype error.

from dataclasses import dataclass
from dataclasses_json import DataClassJsonMixin
from beartype import beartype

@beartype
@dataclass
class Testing(DataClassJsonMixin):
    data: str

t = Testing(data='thing')
print(t)
george-zubrienko commented 11 months ago

Decorator has some issues we can't easily solve rn - thus #442 is in discussion. For v0 (current) API, I'd recommend to either avoid the decorator entirely or pair it with the subclass. We are looking into solving issues it causes with v1 API