lidatong / dataclasses-json

Easily serialize Data Classes to and from JSON
MIT License
1.36k stars 153 forks source link

[BUG] Type hinting #479

Closed u3Izx9ql7vW4 closed 1 year ago

u3Izx9ql7vW4 commented 1 year ago

Description

I was trying to add type hinting for functions accepting classes accepting dataclass_json, but I'm still getting an error saying that what I'm passing in is missing to_json and from_json functions. What should be the proper typing for dataclass_json decorated classes?

Code snippet that reproduces the issue

@dataclass_json
@dataclass
class SomeStruct:
    foo : int

class DataClassType(Protocol):
    __dataclass_fields__: ClassVar[Dict]
    to_json: Callable
    from_json: Callable

def myfunc(a: Type[DataClassType], data):
     return a.to_json()

data = SomeStruct(1)
myfunc(a=SomeStruct, data)

Describe the results you expected

Should not throw type hint error

Python version you are using

3.10

Environment description

dataclasses-json 0.5.14

george-zubrienko commented 1 year ago

Hi @user293811! In 0.6.0 @ringohoffman did some fixes to annotation type hints, so please try with that one. In general, if you want type hinting working correctly in all IDEs, please subclass DataClassJsonMixin instead using the annotation.