rhbvkleef / dict_deserializer

A JSON-oriented converter and validator
BSD 3-Clause "New" or "Revised" License
1 stars 0 forks source link

Does not work with annotation string #2

Open Horstage opened 5 years ago

Horstage commented 5 years ago

Type annotations can be Strings and with Python 4.0 will always be handled as such (PEP 484). As of Python 3.7 you can from __future__ import annotations to replicated Py4.0 behavior. This package does not work with strings as annotation.

I already stumbled on this. Referencing to parent or child objects of the same class is rather common (I believe)

from typing import Optional
class Foo:
    bar: int # works fine
    baz: "str"  # does not work, but you could simply remove quotation marks for now
    child: "Optional[MyClass]"  # does not work
    parent: "Optional[MyClass]"

The only solution I see right now is to use eval on the annotations. Please prove me wrong.

Horstage commented 5 years ago

Further investigation show that you could get the type hints via typing.get_type_hints(Foo).

rhbvkleef commented 3 years ago

Oh, I don't like this... I can see why they're doing this, but it makes things a bi tharder. Thanks for letting me know :)