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

Add support for strings and __future__ annotations #451

Merged
merged 3 commits into from
Sep 29, 2019

Conversation

dmontagu
Copy link
Collaborator

@dmontagu dmontagu commented Aug 21, 2019

This pull request adds support for forward references, string annotations, and from __future__ import annotations in decorated endpoint signatures.

@tiangolo
Copy link
Member

Nice! Good job!

This PR would introduce a small breaking change [...]

No problem with that. We bump the version to release it.

Before this is ready to merge [...]

Perfect. Let me know when you can check those things.

@dmontagu
Copy link
Collaborator Author

@tiangolo Thanks for the feedback!

I actually discussed this with Ivan Levkivskyi on the python/typing gitter, and he suggested I make a bpo post (which I did here: https://bugs.python.org/issue37923). So far it hasn't gotten much attention 😕 but I figured I'd at least ask the question.

Having dug a little more into the various related PEPs and python stdlib source, I feel pretty good about the approach in this PR, so I'm inclined to run with it, and just add more edge-case handling and tests for them. Hopefully in the future there will be first class support for this built directly into the inspect or typing packages, and we can remove any custom/redundant logic.

I'll try to get this into a mergeable state this weekend.

@dmontagu dmontagu force-pushed the string-annotations branch 2 times, most recently from 495f6ea to 8d3f05a Compare September 1, 2019 01:15
@dmontagu
Copy link
Collaborator Author

dmontagu commented Sep 1, 2019

@tiangolo I reworked the implementation, and now it should be 100% backward compatible (no changes to the annotations in the case of optionals, etc.), and only change any of the behavior when string-valued annotations are present. (And the only behavior change will be that it works instead of raising errors.)

@codecov
Copy link

codecov bot commented Sep 1, 2019

Codecov Report

Merging #451 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master   #451   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files         251    251           
  Lines        5988   6001   +13     
=====================================
+ Hits         5988   6001   +13
Impacted Files Coverage Δ
fastapi/dependencies/utils.py 100% <100%> (ø) ⬆️
tests/test_security_oauth2.py 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 203e105...e071ee5. Read the comment docs.

@codecov
Copy link

codecov bot commented Sep 1, 2019

Codecov Report

Merging #451 into master will decrease coverage by 0.16%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #451      +/-   ##
==========================================
- Coverage     100%   99.83%   -0.17%     
==========================================
  Files         251      251              
  Lines        5988     5899      -89     
==========================================
- Hits         5988     5889      -99     
- Misses          0       10      +10
Impacted Files Coverage Δ
fastapi/dependencies/utils.py 100% <100%> (ø) ⬆️
tests/test_security_oauth2.py 100% <100%> (ø) ⬆️
fastapi/openapi/models.py 100% <100%> (ø) ⬆️
fastapi/utils.py 100% <100%> (ø) ⬆️
tests/test_union_inherited_body.py 65.51% <0%> (-34.49%) ⬇️
...est_additional_responses_custom_validationerror.py 100% <0%> (ø) ⬆️
docs/src/app_testing/tutorial003.py 100% <0%> (ø) ⬆️
...t_query_params_str_validations/test_tutorial001.py 100% <0%> (ø) ⬆️
tests/test_custom_swagger_ui_redirect.py 100% <0%> (ø) ⬆️
docs/src/first_steps/tutorial001.py 100% <0%> (ø) ⬆️
... and 64 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 580cf8f...a902d6b. Read the comment docs.

Copy link
Member

@tiangolo tiangolo left a comment

Choose a reason for hiding this comment

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

Awesome job! 🎉 🚀

I think it's fine to leave the tests with your modifications and test both things at the same time. I just requested comments on top of them to remind us to not "refactor" them into their non-string version.

tests/test_security_oauth2.py Show resolved Hide resolved
tests/test_security_oauth2.py Show resolved Hide resolved
tests/test_security_oauth2.py Show resolved Hide resolved
@dmontagu
Copy link
Collaborator Author

@tiangolo

  • I made the requested changes.
  • I also updated the # type: ignore comments to:
    1. be compatible with the recent mypy 0.730 release (which is automatically used in the build)
    2. remove unused ignores (detected by calling mypy fastapi --disallow-untyped-defs --warn-unused-ignore)
  • Do you think the docs should be updated anywhere? It seems like release notes is probably enough

@tiangolo tiangolo self-requested a review September 29, 2019 21:17
Copy link
Member

@tiangolo tiangolo left a comment

Choose a reason for hiding this comment

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

Awesome! Thanks a lot!

LGTM 🎉 🚀

@tiangolo tiangolo merged commit d8fe307 into fastapi:master Sep 29, 2019
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.

2 participants