Skip to content

no deserializer for Union[Literal[...], str] and payload of type str #47

Open
@NiklasRosenstein

Description

@NiklasRosenstein
from typing import Literal
from databind.json import load

MyType = Literal["Current", "FromEnvVar"] | str
assert load("hello", MyType) == "hello"

Expected behavior:

The value is deserialized as str would and returned as is.

Actual behavior:

> python test.py
Traceback (most recent call last):
  File "/home/coder/git/scm/test.py", line 7, in <module>
    assert load("hello", MyType) == "hello"
  File "/home/coder/git/scm/.venvs/3.10/lib/python3.10/site-packages/databind/json/__init__.py", line 67, in load
    return get_object_mapper().deserialize(value, type_, filename, settings)
  File "/home/coder/git/scm/.venvs/3.10/lib/python3.10/site-packages/databind/core/mapper.py", line 104, in deserialize
    self.convert(Direction.DESERIALIZE, value, datatype, Location(filename, None, None), settings),
  File "/home/coder/git/scm/.venvs/3.10/lib/python3.10/site-packages/databind/core/mapper.py", line 76, in convert
    return context.convert()
  File "/home/coder/git/scm/.venvs/3.10/lib/python3.10/site-packages/databind/core/context.py", line 123, in convert
    return self.convert_func(self)
  File "/home/coder/git/scm/.venvs/3.10/lib/python3.10/site-packages/databind/core/converter.py", line 86, in convert
    raise errors[0][1]
  File "/home/coder/git/scm/.venvs/3.10/lib/python3.10/site-packages/databind/core/converter.py", line 80, in convert
    return converter.convert(ctx)
  File "/home/coder/git/scm/.venvs/3.10/lib/python3.10/site-packages/databind/core/converter.py", line 87, in convert
    raise NoMatchingConverter(self, ctx, errors)
databind.core.converter.NoMatchingConverter: no deserializer for `TypeHint(typing.Union[typing.Literal['Current', 'FromEnvVar'], str])` and payload of type `str`

Trace:
    $: TypeHint(typing.Union[typing.Literal['Current', 'FromEnvVar'], str])

Metadata

Metadata

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions