prkumar / uplink

A Declarative HTTP Client for Python
https://uplink.readthedocs.io/
MIT License
1.07k stars 61 forks source link

Pydantic v2 is not supported #297

Open djbios opened 1 year ago

djbios commented 1 year ago

Describe the bug Probably, current version of uplink is not supporting pydantic v2

To Reproduce

from uplink import Body, Consumer, post
from pydantic import BaseModel, AnyHttpUrl  # v2.0.3

class A(BaseModel):
    url: AnyHttpUrl

class C(Consumer):
    @post('http://localhost')
    def test(self, a: Body(type=A)):
        ...

a = A(url='http://localhost')
C().test(a=a)

gives

Traceback (most recent call last):
  File "/home/djbios/.config/JetBrains/PyCharm2022.2/scratches/scratch_31.py", line 16, in <module>
    C().test(a=a)
  File "/home/djbios/.pyenv/versions/3.10.8/lib/python3.10/site-packages/uplink/builder.py", line 100, in __call__
    self._request_definition.define_request(request_builder, args, kwargs)
  File "/home/djbios/.pyenv/versions/3.10.8/lib/python3.10/site-packages/uplink/commands.py", line 284, in define_request
    self._argument_handler.handle_call(
  File "/home/djbios/.pyenv/versions/3.10.8/lib/python3.10/site-packages/uplink/arguments.py", line 154, in handle_call
    self.handle_call_args(request_builder, call_args)
  File "/home/djbios/.pyenv/versions/3.10.8/lib/python3.10/site-packages/uplink/arguments.py", line 159, in handle_call_args
    annotation.modify_request(request_builder, call_args[name])
  File "/home/djbios/.pyenv/versions/3.10.8/lib/python3.10/site-packages/uplink/arguments.py", line 183, in modify_request
    converted_value = converter(value) if converter else value
  File "/home/djbios/.pyenv/versions/3.10.8/lib/python3.10/site-packages/uplink/converters/interfaces.py", line 6, in __call__
    return self.convert(*args, **kwargs)
  File "/home/djbios/.pyenv/versions/3.10.8/lib/python3.10/site-packages/uplink/converters/pydantic_.py", line 34, in convert
    return _encode_pydantic(value)
  File "/home/djbios/.pyenv/versions/3.10.8/lib/python3.10/site-packages/uplink/converters/pydantic_.py", line 25, in _encode_pydantic
    return _encode_pydantic(pydantic_encoder(obj))
  File "/home/djbios/.pyenv/versions/3.10.8/lib/python3.10/site-packages/uplink/converters/pydantic_.py", line 20, in _encode_pydantic
    return {_encode_pydantic(k): _encode_pydantic(v) for k, v in obj.items()}
  File "/home/djbios/.pyenv/versions/3.10.8/lib/python3.10/site-packages/uplink/converters/pydantic_.py", line 20, in <dictcomp>
    return {_encode_pydantic(k): _encode_pydantic(v) for k, v in obj.items()}
  File "/home/djbios/.pyenv/versions/3.10.8/lib/python3.10/site-packages/uplink/converters/pydantic_.py", line 25, in _encode_pydantic
    return _encode_pydantic(pydantic_encoder(obj))
  File "/home/djbios/.pyenv/versions/3.10.8/lib/python3.10/site-packages/typing_extensions.py", line 2562, in wrapper
    return __arg(*args, **kwargs)
  File "/home/djbios/.pyenv/versions/3.10.8/lib/python3.10/site-packages/pydantic/deprecated/json.py", line 103, in pydantic_encoder
    raise TypeError(f"Object of type '{obj.__class__.__name__}' is not JSON serializable")
TypeError: Object of type 'Url' is not JSON serializable

Expected behavior Url is pydantic type and should be serialized to str. The issue caused by calling deprecated pydantic_encoder

Additional context Not sure if there are other cases of new pydantic not supported, but probably uplink should require pydantic<2

leiserfg commented 1 year ago

I have intentions of adding support for Pydantic 2, but @prkumar does not seem to be available sadly.