Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix python-fastapi free-form objects mapping and forward ref type hints #9723

Conversation

chludwig-haufe
Copy link
Contributor

This PR implements the proposed fixes for #9720 and #9721:

  • Free-form object model attributes are mapped to the type hint Dict[str, Any].
  • To support forward type references in the type hints generated for (recursive) model classes, add from __future__ import annotations to the model template. (This is supported in Python >= 3.7.)

With these changes in place, the generated FastAPI service could actually be started.

Python technical committee: @taxpon, @frol, @mbohlool, @cbornet, @kenjones-cisco, @tomplus, @Jyhess, @arun-nalla, @spacether

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/java*.
    For Windows users, please run the script in Git BASH.
  • File the PR against the correct branch: master, 5.1.x, 6.0.x
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Forward type references will be supported by default from Python 3.10 on only. Until then (and starting with Python 3.7), we can opt in by a __future__ import, cf. https://docs.python.org/3.9/whatsnew/3.7.html?highlight=forward#pep-563-postponed-evaluation-of-annotations
@chludwig-haufe
Copy link
Contributor Author

@krjakbrjak As the author of the python-fastapi generator, you might also be interested in this PR.

Copy link
Contributor

@krjakbrjak krjakbrjak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having recursive definitions is a valid use case. LGTM. Thanks.

@wing328 wing328 merged commit f3e3a72 into OpenAPITools:master Jun 14, 2021
@wing328
Copy link
Member

wing328 commented Jun 15, 2021

@chludwig-haufe I just spotted the following errors in the CI:

You should consider upgrading via the '/home/travis/virtualenv/python3.6.7/bin/python -m pip install --upgrade pip' command.
ImportError while loading conftest '/home/travis/build/OpenAPITools/openapi-generator/samples/server/petstore/python-fastapi/tests/conftest.py'.
tests/conftest.py:5: in <module>
    from openapi_server.main import app as application
src/openapi_server/main.py:15: in <module>
    from openapi_server.apis.pet_api import router as PetApiRouter
src/openapi_server/apis/pet_api.py:20: in <module>
    from openapi_server.models.api_response import ApiResponse
E     File "/home/travis/build/OpenAPITools/openapi-generator/samples/server/petstore/python-fastapi/src/openapi_server/models/api_response.py", line 3
E       from __future__ import annotations
E                                        ^
E   SyntaxError: future feature annotations is not defined

Can you please take a look when you've time?

@chludwig-haufe
Copy link
Contributor Author

@wing328 I see in your command line Python 3.6. __future__.annotations requires Python >= 3.7. That's why I changed the documented requirements in 78452d6. Did I miss a file?

@krjakbrjak
Copy link
Contributor

krjakbrjak commented Jun 15, 2021

@chludwig-haufe @wing328 python 3.6 is set as default. Since the dockerfile is already generated and uses 3.7, it makes sense to build the image (which is built only when the tests pass). I opened #9771 (it addresses this issue)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants