Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

Improves endpoint response type hints, handles all error responses corner case #20

Merged
merged 7 commits into from
Oct 4, 2022

Conversation

spacether
Copy link
Contributor

@spacether spacether commented Oct 4, 2022

  • Removes last result type hint in endpoint
  • Removes generic ApiResponse which is never returned
  • Add handling of case when all responses are error codes (4XX or 5XX)
  • Used mypy to verify type hints for boolean value with a default per the below mypy check
import typing


@typing.overload
def hi(skip_deserialization: typing.Literal[True]) -> typing.Literal[True]: ...

@typing.overload
def hi(skip_deserialization: typing.Literal[False] = ...) -> typing.Literal[False]: ...

@typing.overload
def hi(skip_deserialization: bool = ...) -> typing.Literal[True, False]: ...

def hi(skip_deserialization: bool = False):
    return skip_deserialization

import random
c: bool = bool(random.randint(0,1))
first = hi()
second = hi(skip_deserialization=True)
third = hi(skip_deserialization=False)
fourth = hi(skip_deserialization=c)

reveal_type(hi())
reveal_type(hi(skip_deserialization=True))
reveal_type(hi(skip_deserialization=False))
reveal_type(hi(skip_deserialization=c))

which resulted in:

overload.py:23: note: Revealed type is "Literal[False]"
overload.py:24: note: Revealed type is "Literal[True]"
overload.py:25: note: Revealed type is "Literal[False]"
overload.py:26: note: Revealed type is "builtins.bool"

So those are how I wrote the skip_deserialization inputs in the type hints
It doesn't work in pycharm because of https://youtrack.jetbrains.com/issue/PY-38074/Ignore-return-types-of-fallback-overloads-for-functions-exactly-matched-by-a-literal-type
but it DOES work in VScode

Corner cases for endpoint response type hinting:

  • all error responses should go to -> ApiResponeWithoutDeserialization (this PR adds handling of this case)
  • only default response -> ApiResponseForDefault (templates handle this)
  • only default response and it is for an error (there is no way to detect this use case)

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh
    ./bin/utils/export_docs_generators.sh
    
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/python*.
    For Windows users, please run the script in Git BASH.

@spacether spacether marked this pull request as draft October 4, 2022 05:18
@spacether spacether force-pushed the feat_improves_endpoint_type_hints branch from e4f1915 to 0173515 Compare October 4, 2022 06:29
@spacether spacether changed the title Removes last result type hint in endpoint Improves endpoint type hints Oct 4, 2022
@spacether spacether changed the title Improves endpoint type hints Improves endpoint response type hints, handles all error responses corner case Oct 4, 2022
@spacether spacether added this to the 1.0.0 milestone Oct 4, 2022
@spacether spacether marked this pull request as ready for review October 4, 2022 18:44
@spacether spacether merged commit 8f9b901 into master Oct 4, 2022
@spacether spacether deleted the feat_improves_endpoint_type_hints branch October 4, 2022 18:49
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant