Skip to content

Allow Optional type annotation #307

@ScarletBlizzard

Description

@ScarletBlizzard

Error when calling Consumer method with return type annotation Optional[...] if response is None:

...
 File "/home/max/.cache/pypoetry/virtualenvs/autom-tnujRd_K-py3.10/lib/python3.10/site-packages/uplink/builder.py", line 100, in __call__
    self._request_definition.define_request(request_builder, args, kwargs)
  File "/home/max/.cache/pypoetry/virtualenvs/autom-tnujRd_K-py3.10/lib/python3.10/site-packages/uplink/commands.py", line 287, in define_request
    self._method_handler.handle_builder(request_builder)
  File "/home/max/.cache/pypoetry/virtualenvs/autom-tnujRd_K-py3.10/lib/python3.10/site-packages/uplink/decorators.py", line 62, in handle_builder
    annotation.modify_request(request_builder)
  File "/home/max/.cache/pypoetry/virtualenvs/autom-tnujRd_K-py3.10/lib/python3.10/site-packages/uplink/returns.py", line 66, in modify_request
    converter = self._get_converter(request_builder, return_type)
  File "/home/max/.cache/pypoetry/virtualenvs/autom-tnujRd_K-py3.10/lib/python3.10/site-packages/uplink/returns.py", line 57, in _get_converter
    return request_builder.get_converter(
  File "/home/max/.cache/pypoetry/virtualenvs/autom-tnujRd_K-py3.10/lib/python3.10/site-packages/uplink/helpers.py", line 96, in get_converter
    return self._converter_registry[converter_key](*args, **kwargs)
  File "/home/max/.cache/pypoetry/virtualenvs/autom-tnujRd_K-py3.10/lib/python3.10/site-packages/uplink/converters/__init__.py", line 52, in __call__
    converter = self._converter_factory(*args, **kwargs)
  File "/home/max/.cache/pypoetry/virtualenvs/autom-tnujRd_K-py3.10/lib/python3.10/site-packages/uplink/converters/__init__.py", line 112, in chain
    converter = func(factory)(*args, **kwargs)
  File "/home/max/.cache/pypoetry/virtualenvs/autom-tnujRd_K-py3.10/lib/python3.10/site-packages/uplink/converters/typing_.py", line 127, in create_response_body_converter
    return self._base_converter(type_)
  File "/home/max/.cache/pypoetry/virtualenvs/autom-tnujRd_K-py3.10/lib/python3.10/site-packages/uplink/converters/typing_.py", line 121, in _base_converter
    if issubclass(type_.__origin__, self.typing.Sequence):
  File "/home/max/.pyenv/versions/3.10.13/lib/python3.10/typing.py", line 1158, in __subclasscheck__
    return issubclass(cls, self.__origin__)
  File "/home/max/.pyenv/versions/3.10.13/lib/python3.10/abc.py", line 123, in __subclasscheck__
    return _abc_subclasscheck(cls, subclass)
TypeError: issubclass() arg 1 must be a class

To Reproduce
Create a Consumer with method that consumes API endpoint that returns None (it is shown as "null" if using curl). Add Optional[SomeType] return type annotation to this method. Call this method to get the error

Expected behavior
Absence of this error. Instead, simply return None

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions