2

How do I fix the mypy error Overloaded function signatures 1 and 2 overlap with incompatible return types?

Here are my methods:

    @typing.overload
    def _find_pets_by_tags(
        self,
        query_params: typing.Union[
            QueryParametersDictInput,
            QueryParametersDict
        ],
        security_index: typing.Optional[int] = None,
        server_index: typing.Optional[int] = None,
        stream: bool = False,
        timeout: typing.Optional[typing.Union[int, float, typing.Tuple]] = None,
        skip_deserialization: typing_extensions.Literal[False] = False
    ) -> response_200.ApiResponse: ...

    @typing.overload
    def _find_pets_by_tags(
        self,
        query_params: typing.Union[
            QueryParametersDictInput,
            QueryParametersDict
        ],
        security_index: typing.Optional[int] = None,
        server_index: typing.Optional[int] = None,
        stream: bool = False,
        timeout: typing.Optional[typing.Union[int, float, typing.Tuple]] = None,
        skip_deserialization: typing_extensions.Literal[True] = ...
    ) -> api_response.ApiResponseWithoutDeserialization: ...

    def _find_pets_by_tags(
        self,
        query_params: typing.Union[
            QueryParametersDictInput,
            QueryParametersDict
        ],
        security_index: typing.Optional[int] = None,
        server_index: typing.Optional[int] = None,
        stream: bool = False,
        timeout: typing.Optional[typing.Union[int, float, typing.Tuple]] = None,
        skip_deserialization: bool = False
    ):

And mypy complains that: Overloaded function signatures 1 and 2 overlap with incompatible return types [misc]

But

  • how are these methods overlapping? They require two different literal bool inputs
  • What makes the return types incompatible? The overload requires two different class instances are returned. Both of them inherit from an api_client.ApiResponse base class

1 Answer 1

3

how are these methods overlapping? They require two different literal bool inputs

No they don't. skip_deserialization is optional, not required, in both overloads. A call that doesn't pass skip_deserialization can match both overloads.

What makes the return types incompatible? The overload requires two different class instances are returned. Both of them inherit from an api_client.ApiResponse base class

It doesn't matter that both return types have a common supertype. (All Python types have a common supertype object anyway.) The fact remains that they are different types, and code written to expect one type may not be compatible with the other.


Since the actual default for skip_deserialization is False, it should not be optional in the overload that expects True. Since the parameter follows multiple arguments that are optional, you may need to change your overload structure, or change how this method takes that argument - for example, you might want to make it keyword-only (and while you're at it, it might make sense to make a bunch of the other arguments keyword-only too).

If you don't want to change how the method takes arguments, then one way to write the overloads would be to write two separate overloads, one where the argument is passed by keyword (and the other arguments remain optional), and one where the argument is passed positionally (requiring all previous arguments to be passed positionally, thus making them mandatory in that overload).

Sign up to request clarification or add additional context in comments.

2 Comments

My overloads are hard coding in required values for skip_deserialization or trying to. Did I do it wrong?
@spacether: The = ... in skip_deserialization: typing_extensions.Literal[True] = ... says that parameter is optional.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.