Skip to content

python: fix typing for API responses#16694

Closed
multani wants to merge 1 commit intoOpenAPITools:masterfrom
multani:py-typing-3
Closed

python: fix typing for API responses#16694
multani wants to merge 1 commit intoOpenAPITools:masterfrom
multani:py-typing-3

Conversation

@multani
Copy link
Copy Markdown
Contributor

@multani multani commented Sep 30, 2023

The *_with_http_info methods, instead of returning a generic ApiResponse object now return the actual object the method relates to.

The type returned is the same as the methods that call the *_with_http_info methods.
The documentation also reflects the correct type.

To simplify the template generation, the return type information is generated in the Java generator code.

@wing328
@krjakbrjak
@fa0311

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/configs/*.yaml
    ./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/java*.
    For Windows users, please run the script in Git BASH.
  • File the PR against the correct branch: master (upcoming 7.1.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@fa0311
Copy link
Copy Markdown
Contributor

fa0311 commented Sep 30, 2023

How do I get response headers?

@multani
Copy link
Copy Markdown
Contributor Author

multani commented Sep 30, 2023

How do I get response headers?

Ah yes, I'll change the type to an Union 👍

@multani multani marked this pull request as draft September 30, 2023 12:52
@fa0311
Copy link
Copy Markdown
Contributor

fa0311 commented Sep 30, 2023

typing supports generics type
https://docs.python.org/3/library/typing.html#generics

@fa0311
Copy link
Copy Markdown
Contributor

fa0311 commented Sep 30, 2023

The current api/*.py may need to be significantly revamped as it is not suited for giving type hints.

@multani
Copy link
Copy Markdown
Contributor Author

multani commented Sep 30, 2023

The current api/*.py may need to be significantly revamped as it is not suited for giving type hints.

Do you have any specific ideas in mind already?

@fa0311
Copy link
Copy Markdown
Contributor

fa0311 commented Sep 30, 2023

Do you have any specific ideas in mind already?

No.

@robertschweizer
Copy link
Copy Markdown
Contributor

robertschweizer commented Oct 8, 2023

The goal of this PR is to have correct type annotations for the case that _return_http_data_only=True, right?

I think outside users are not supposed to set this flag, but rather use the plain client methods (not the _with_http_info methods) in order to get only the response object. So for users of the generated client, this PR makes the type annotations actually more complex without describing a valid use-case.

A nicer solution could be to remove the _return_http_data_only flag and always return ApiResponse.data in the plain client methods. That would always cause the construction of ApiResponse, but that has no logic, so it should be cheap. Type annotations and docstrings would be shorter and thus more user-friendly.

@multani
Copy link
Copy Markdown
Contributor Author

multani commented Oct 9, 2023

Thanks @fa0311 and @robertschweizer for the suggestions, I'll take a look at how to proceed.

From my understood, it would be better to:

  • Remove _return_http_data_only completely
  • Transform the _with_http_info methods to return a simpler answer, like only ApiResponse with the data inside
  • Have the "normal" methods (not the _with_http_info ones) to return the high-level type

This could also simplify the ApiClient class: at the moment, the deserialization to high-level types is done in this class. If I implement the above, this means:

  • ApiClient always returns ApiResponses
  • The deserialization to high-level types moves closer to where the types are actually used

This may require larger changes in APiClient to achieve this and removing response_types_map from there. I'll take a look.

The `*_with_http_info` methods, instead of returning a generic
ApiResponse object now return the actual object the method relates to.
@multani
Copy link
Copy Markdown
Contributor Author

multani commented Oct 11, 2023

So, I made good progress on this, but this is also getting more complicated.

I created an issue to discuss about these changes, outside of this pull request: #16788

I will push my last changes in this pull request: it has most of what I discuss in #16788 except the new ApiRequest I mentioned: instead, most of the code is duplicated for now (hence, why I think we would need this).

There are still several mypy issues, but I think they are simpler to tackle after that:

  • There are multiple ApiResponse classes defined, and they don't have the same interface.
  • Some methods return Optional[Foo] instead of Foo as currently typed.

I haven't checked the rest yet.

@multani
Copy link
Copy Markdown
Contributor Author

multani commented Oct 11, 2023

I'll close this and check what we should actually do in #16788 instead.

I think the final result once #16788 is closed would be similar to this pull request, but it could be done in multiple smaller PRs, instead of this big one with many changes.

@multani multani closed this Oct 11, 2023
@multani multani deleted the py-typing-3 branch October 13, 2023 15:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants