ermakov-oleg / serpyco-rs

Fast Python dataclasses serialization
MIT License
30 stars 2 forks source link

Generic inheritance #137

Closed buxx closed 7 months ago

buxx commented 7 months ago

Hi

Should this case work ?

T = typing.TypeVar("T")

@dataclasses.dataclass
class Parent(typing.Generic[T]):
    value: T

@dataclasses.dataclass
class Child(Parent[int]):
    pass

serializer = serpyco_rs.Serializer(Child)
result = serializer.dump(Child(42))
print(result)

It produces the following error :

Traceback (most recent call last):
  File "/home/bastiensevajol/Projets/indicators/venv/lib/python3.12/site-packages/serpyco_rs/_describe.py", line 411, in _replace_generics
    t = generics_map[t]
        ~~~~~~~~~~~~^^^
KeyError: ~T

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/bastiensevajol/.pythonz/pythons/CPython-3.12.2/lib/python3.12/runpy.py", line 198, in _run_module_as_main
    return _run_code(code, main_globals, None,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/bastiensevajol/.pythonz/pythons/CPython-3.12.2/lib/python3.12/runpy.py", line 88, in _run_code
    exec(code, run_globals)
  File "/home/bastiensevajol/.vscode/extensions/ms-python.debugpy-2024.2.0-linux-x64/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/__main__.py", line 39, in <module>
    cli.main()
  File "/home/bastiensevajol/.vscode/extensions/ms-python.debugpy-2024.2.0-linux-x64/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 430, in main
    run()
  File "/home/bastiensevajol/.vscode/extensions/ms-python.debugpy-2024.2.0-linux-x64/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 284, in run_file
    runpy.run_path(target, run_name="__main__")
  File "/home/bastiensevajol/.vscode/extensions/ms-python.debugpy-2024.2.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 321, in run_path
    return _run_module_code(code, init_globals, run_name,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/bastiensevajol/.vscode/extensions/ms-python.debugpy-2024.2.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 135, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/home/bastiensevajol/.vscode/extensions/ms-python.debugpy-2024.2.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 124, in _run_code
    exec(code, run_globals)
  File "/home/bastiensevajol/Projets/indicators/indicators/data.py", line 65, in <module>
    serializer = serpyco_rs.Serializer(Child)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/bastiensevajol/Projets/indicators/venv/lib/python3.12/site-packages/serpyco_rs/_main.py", line 43, in __init__
    type_info = describe_type(t)
                ^^^^^^^^^^^^^^^^
  File "/home/bastiensevajol/Projets/indicators/venv/lib/python3.12/site-packages/serpyco_rs/_describe.py", line 221, in describe_type
    entity_type = _describe_entity(
                  ^^^^^^^^^^^^^^^^^
  File "/home/bastiensevajol/Projets/indicators/venv/lib/python3.12/site-packages/serpyco_rs/_describe.py", line 305, in _describe_entity
    type_ = _replace_generics(types.get(field.name, field.type), generics)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/bastiensevajol/Projets/indicators/venv/lib/python3.12/site-packages/serpyco_rs/_describe.py", line 413, in _replace_generics
    raise RuntimeError(f'Unfilled TypeVar: {exc.args[0]}') from exc
RuntimeError: Unfilled TypeVar: ~T
ermakov-oleg commented 7 months ago

I think yes, it should work. I will try to fix this soon.