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

Resolve types correctly (PEP 563, postponed annotations) #5461

Closed
wants to merge 15 commits into from

Conversation

mastercoms
Copy link

@mastercoms mastercoms commented Oct 5, 2022

This commit fixes various problems stemming from incorrect evaluation of types in function signatures.

First, we use Python's own typing.get_type_hints method to ensure we can resolve type hints with the correct scope. Python handles classes, wrapped functions which are not considered in the current implementation. I also prefer this approach as it will most likely be the best at handling any future changes to Python.

However, unfortunately, get_type_hints resolves type hints for the class __init__ function when passed a callable class, so I had to add some checks to see if the __call__ function should be passed in rather than the Callable itself. If the callable is an actual function of some form, we can pass itself to get_type_hints. If the callable is a type annotation, we need to call the class type's __init__ function, and thus we pass that into get_type_hints. Otherwise, we pass in the __call__ function.

Finally, the return type with the old approach was not resolved, so it used the default return_annotation=typing.Any. get_type_hints is able to retrieve return type information as well, so I was able to pass in the return annotation and thus the return type is no longer erased. In the case that no return type annotation is found, we pass in the default of Any again to represent the unknown status of the type.

@github-actions
Copy link
Contributor

github-actions bot commented Oct 5, 2022

📝 Docs preview for commit 07fd3b6 at: https://633def1f4e0be700889c7cfe--fastapi.netlify.app

@mastercoms mastercoms force-pushed the patch-1 branch 2 times, most recently from 5f2e2ac to c2378bf Compare October 5, 2022 21:02
@mastercoms
Copy link
Author

Currently failing due to python/mypy#5079

@github-actions
Copy link
Contributor

github-actions bot commented Oct 5, 2022

📝 Docs preview for commit e4bc092 at: https://633df31cb6573709f4f8792d--fastapi.netlify.app

@mastercoms mastercoms changed the title Resolve types correctly Resolve types correctly (PEP 563, postponed annotations) Oct 5, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Oct 6, 2022

📝 Docs preview for commit 0dc7591 at: https://633e820e75af827a0c144fd4--fastapi.netlify.app

@github-actions
Copy link
Contributor

github-actions bot commented Oct 6, 2022

📝 Docs preview for commit fe9d827 at: https://633e88b0ec49c476bb822d6d--fastapi.netlify.app

@github-actions
Copy link
Contributor

github-actions bot commented Oct 6, 2022

📝 Docs preview for commit c6c279d at: https://633ebec666be07349e88ef16--fastapi.netlify.app

@github-actions
Copy link
Contributor

github-actions bot commented Oct 6, 2022

📝 Docs preview for commit d0b0c11 at: https://633ec2c8b16274004cf10f4d--fastapi.netlify.app

@codecov
Copy link

codecov bot commented Oct 6, 2022

Codecov Report

Base: 100.00% // Head: 100.00% // No change to project coverage 👍

Coverage data is based on head (7350706) compared to base (cf73051).
Patch coverage: 100.00% of modified lines in pull request are covered.

❗ Current head 7350706 differs from pull request most recent head 4fd7b04. Consider uploading reports for the commit 4fd7b04 to get more accurate results

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #5461   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          540       540           
  Lines        13969     13947   -22     
=========================================
- Hits         13969     13947   -22     
Impacted Files Coverage Δ
fastapi/dependencies/utils.py 100.00% <100.00%> (ø)
fastapi/routing.py 100.00% <0.00%> (ø)
tests/test_datastructures.py 100.00% <0.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@github-actions
Copy link
Contributor

github-actions bot commented Oct 6, 2022

📝 Docs preview for commit af686a5 at: https://633ec456e6bb360061861352--fastapi.netlify.app

@mastercoms
Copy link
Author

Now building and passing tests, waiting on #5456 to fix some checks.

@github-actions
Copy link
Contributor

📝 Docs preview for commit 7350706 at: https://634b34d37268307b95f8fc7e--fastapi.netlify.app

@github-actions
Copy link
Contributor

📝 Docs preview for commit fd01fa8 at: https://6384d47e14b0af109c74e5cf--fastapi.netlify.app

@github-actions
Copy link
Contributor

📝 Docs preview for commit 3a51083 at: https://639ce6c12c118d073e47facd--fastapi.netlify.app

@github-actions
Copy link
Contributor

github-actions bot commented Feb 7, 2023

📝 Docs preview for commit 8fddf4f at: https://63e27637ee4cdf05497ab55a--fastapi.netlify.app

@github-actions
Copy link
Contributor

github-actions bot commented Feb 7, 2023

📝 Docs preview for commit 4fd7b04 at: https://63e281e2c68e580b386f349f--fastapi.netlify.app

@mastercoms
Copy link
Author

I'll be looking to update this for 0.89.1 soon.

@@ -247,36 +247,36 @@ def is_scalar_sequence_field(field: ModelField) -> bool:

def get_typed_signature(call: Callable[..., Any]) -> inspect.Signature:
signature = inspect.signature(call)
globalns = getattr(call, "__globals__", {})
is_true_function = (

Choose a reason for hiding this comment

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

It might look a bit nicer here if you pass a tuple of arguments to isinstance.

if isinstance(call, (FunctionType, MethodType, BuiltinFunctionType))

Copy link
Author

Choose a reason for hiding this comment

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

Oh neat, thanks!

@mastercoms
Copy link
Author

mastercoms commented Apr 2, 2023

The project seems to have taken a different path on this, so closing since I cannot provide updates for new usage of the old annotations.

@mastercoms mastercoms closed this Apr 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment