radical-cybertools / radical.utils

Utility classes and tools for various radical projects
Other
8 stars 6 forks source link

incomplete type casting in `TypedDict` #397

Closed andre-merzky closed 9 months ago

andre-merzky commented 10 months ago
import pprint

import radical.utils as ru

class Foo(ru.TypedDict):
    _schema  = {'val': int}
    _default = {'val':  42}

class Bar(ru.TypedDict):
    _schema  = {'foo' :  Foo,
                'foos': [Foo]}

bar = Bar(foo=Foo(val=42),
          foos=[Foo(val=42), Foo(val=84)])

pprint.pprint(bar)
pprint.pprint(bar.as_dict())
pprint.pprint(Bar(bar.as_dict()))

results in

Bar: {'foo': Foo: {'val': 42}, 'foos': [Foo: {'val': 42}, Foo: {'val': 84}]}
{'foo': {'val': 42}, 'foos': [{'val': 42}, {'val': 84}]}
Bar: {'foo': Foo: {'val': 42}, 'foos': [{'val': 42}, {'val': 84}]}

Note that in the last output line, the key foo has a value of type Foo, as expected - but the key foos should have a value of type List[Foo] (like originally in output line 1), but instead is a List[int] as in the from_dict it was created from (output line two). The casting we do to TypedDict values does not happen on nested data structures - but they should if so specified in the schema.

I gave this a high priority as it complicates handling of RP Slots structures which are about to be exposed on API level.

andre-merzky commented 10 months ago

Note that verify actually corrects this, it is only the construction from a from_dict which is incomplete.

andre-merzky commented 9 months ago

invalid - _check and _cast cover this.