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

Type Annotations for Before Hook #2234

Closed

Conversation

nZac
Copy link

@nZac nZac commented Jun 22, 2024

Summary of Changes

Despite the poorly named branch, this PR brings a brings type annotations to the before hook. The approach is a proof of concept and I'll welcome suggestions. I've not typed all the functions beneath it, just the top level API though that should be accomplished at some point. See the commit messages for more details.

Related Issues

The existing type issues are closed already, so N/A for now.

Pull Request Checklist

  • Applied changes to both WSGI and ASGI code paths and interfaces (where applicable).
  • Added tests for changed code.
  • Prefixed code comments with GitHub nick and an appropriate prefix.
  • Coding style is consistent with the rest of the framework.
  • Updated documentation for changed code.
    • Added docstrings for any new classes, functions, or modules.
    • Updated docstrings for any modifications to existing code.
    • Updated both WSGI and ASGI docs (where applicable).
    • Added references to new classes, functions, or modules to the relevant RST file under docs/.
    • Updated all relevant supporting documentation files under docs/.
    • A copyright notice is included at the top of any new modules (using your own name or the name of your organization).
    • Changed/added classes/methods/functions have appropriate versionadded, versionchanged, or deprecated directives.
  • Changes (and possible deprecations) have towncrier news fragments under docs/_newsfragments/, with the file name format {issue_number}.{fragment_type}.rst. (Run towncrier --draft to ensure it renders correctly.)

@nZac nZac force-pushed the generic/request-response branch 2 times, most recently from d0b2358 to c574069 Compare June 22, 2024 04:23
Copy link

codecov bot commented Jun 22, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (9b27c71) to head (46e542c).

Current head 46e542c differs from pull request most recent head 22844be

Please upload reports for the commit 22844be to get more accurate results.

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #2234   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           63        63           
  Lines         6931      6896   -35     
  Branches      1258      1258           
=========================================
- Hits          6931      6896   -35     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

This adds typing information for the before hook as a proof of concept. I used
ParamSpec as a means to introspect the hooks arguments to type check the *args
and **kwargs arguments.

A problem with using ParamSpec is that additional arguments to the before
decorator become impossible (so far as I can see) to specify a type. is_async,
in needs to come after the positional args for backwards compat, but that is not allowed with
ParamSpec.

For now, I've just silenced the type error and we can see if there is a better way
@nZac nZac force-pushed the generic/request-response branch from c574069 to 46e542c Compare June 22, 2024 04:33
setup.cfg Outdated
@@ -54,6 +54,7 @@ include_package_data = True
packages = find:
python_requires = >=3.7
install_requires =
typing_extensions
Copy link
Member

Choose a reason for hiding this comment

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

it would be great if we could require this only for a pthon version lower of a certain version.

Since at the moment we need ParamSpec I guess we could make it install only for < 3.10

Copy link
Member

Choose a reason for hiding this comment

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

Yes, we want to keep Falcon free from third party dependencies.
It is OK to add dependencies for soon EOL 3.8 or 3.9.

@@ -79,6 +91,8 @@ def do_something(req, resp, resource, params):
*action*.
"""

is_async = kwargs.get('is_async', False)
Copy link
Member

Choose a reason for hiding this comment

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

if we keep is_async: bool = False, listed explicitly this line is not needed

Copy link
Member

Choose a reason for hiding this comment

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

I would like to deprecate is_async now that Cython 3.0 properly marks coroutine functions if run on Python 3.10+. I will create a separate issue for this.

Copy link
Author

@nZac nZac Jun 24, 2024

Choose a reason for hiding this comment

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

If we can drop is_async this PR becomes possible. After I pushed this branch I continued my testing and realized this wont work the way I thought it would. I'm not seeing a way to actually type before and after methods in their current state.

I could be missing something though 🤷🏼‍♂️

@CaselIT
Copy link
Member

CaselIT commented Aug 12, 2024

this was implemented in #2183

@CaselIT
Copy link
Member

CaselIT commented Aug 12, 2024

will leave a todo in the code to improve that part

@CaselIT CaselIT closed this Aug 12, 2024
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