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

feat: allow Requests to be sent to exempt_when #160

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

colin99d
Copy link

@colin99d colin99d commented Apr 11, 2023

Creates a way to use the request object in the exempt_when function.

Now allows functions like this to be used:

def test_exempt(x) -> bool:
    print(x.headers.get("User-Agent"))
    return False

Fixes #155

Lines 702 and 706 were automatically removed by my linter because they are unused. Let me know if you want them back. Also, let me know if you want ruff added to CI for code linting.

@davidstrouk
Copy link

+1

Today we return None as part of the key_func function in order to bypass the limit, instead of using the exempt_when function. Then we get the error kipping limit: %s. Empty value found in parameters..

This PR would enable us to use exempt_when and make the code cleaner.

@colin99d
Copy link
Author

@laurentS anything else you want to see here for this to be mergeable?

@laurentS
Copy link
Owner

@laurentS anything else you want to see here for this to be mergeable?

@colin99d thanks for submitting this PR! I just took a quick look, but I saw the PR changes the arguments to a method that's probably public, which means it would probably break some users' code (at least their typechecks would complain). Can the extra arg be made optional to avoid that?

@laurentS laurentS changed the title Allows Requests to be sent to exempt_when feat: allow Requests to be sent to exempt_when May 24, 2023
@colin99d
Copy link
Author

@laurentS im so sorry I just sorry your comment, changes added!!!

Copy link
Collaborator

@Rested Rested left a comment

Choose a reason for hiding this comment

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

Added a suggestion to avoid inspecting on each request! Also would be worth adding a test perhaps with the example you gave in the PR description i reckon!

slowapi/wrappers.py Show resolved Hide resolved
slowapi/wrappers.py Outdated Show resolved Hide resolved
@colin99d
Copy link
Author

@laurentS anything else you want to see before this is merged?

Co-authored-by: Reuben Thomas-Davis <[email protected]>
laurentS
laurentS previously approved these changes Aug 21, 2023
@laurentS
Copy link
Owner

@laurentS anything else you want to see before this is merged?

@colin99d I merged @Rested 's suggestions, can you just check that tests pass, etc..., and I think we're good to go then.

@colin99d
Copy link
Author

@laurentS it looks like the change by @Rested causes all the tests to fail. Any idea how to fix it?

@colin99d
Copy link
Author

Also, I got a little carried away and switch from Flake8 to Ruff. Its pretty much Flake8 but way better in every way.

@laurentS
Copy link
Owner

Maybe we should try to do one thing per PR. Let's focus this one on the feature you initially proposed, and we can add ruff in a separate one.
I won't have time to look into this for the next few days, but it looks like there's a bug in the github ci file preventing the checks from running, no?

@colin99d
Copy link
Author

I reverted ruff changes. All tests fail because of this line:
self._exempt_when_takes_request = len(inspect.signature(self.exempt_when).parameters) == 1

@laurentS
Copy link
Owner

I reverted ruff changes. All tests fail because of this line: self._exempt_when_takes_request = len(inspect.signature(self.exempt_when).parameters) == 1

It just needs reformatting. Can you run black on your code?
I can't commit to your branch, so you'll have to do it.

It should probably look like this:

        self._exempt_when_takes_request = (
            len(inspect.signature(self.exempt_when).parameters) == 1
        )

@thentgesMindee
Copy link
Collaborator

Not related to this PR but maybe we could implement pre-commit on the repo, so that anyone wanting to contribute would just need to do one pre-commit install when first cloning their repo to have all code formatting applied automatically when they commit

@colin99d
Copy link
Author

colin99d commented Oct 5, 2023

@laurentS changes in, can we merge now?

@colin99d
Copy link
Author

colin99d commented Jan 5, 2024

@laurentS are we waiting on anything else here?

@laurentS
Copy link
Owner

laurentS commented Jan 5, 2024

@laurentS are we waiting on anything else here?

I think CI checks should be green before merging :) Logs seem to have been removed by github, but I think there was something pretty clear in there as to what needs fixing.

@colin99d
Copy link
Author

colin99d commented Jan 5, 2024

Can you please rerun them so I can see what it is?

@laurentS
Copy link
Owner

laurentS commented Jan 5, 2024

Can you please rerun them so I can see what it is?

I can't seem to rerun CI on github, weird. I pulled your changes locally, try poetry run mypy . and you'll see it is not happy about an unhandled case (when exempt_when is None) in wrappers.py:33.
And in general, you should be able to trigger a CI run by pushing an empty commit (or just doing some cosmetic change in the code).

@colin99d
Copy link
Author

colin99d commented Jan 5, 2024

@laurentS done!

laurentS
laurentS previously approved these changes Jan 8, 2024
Copy link
Owner

@laurentS laurentS left a comment

Choose a reason for hiding this comment

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

Nice! My only comment is that is_exempt is not a property anymore, so it would be good to add a note about breaking changes due to this, as it's bound to break somebody else's code.

@colin99d
Copy link
Author

colin99d commented Mar 8, 2024

Ok, where would you like the note added?

@ecly
Copy link

ecly commented May 16, 2024

@colin99d I would assume it should be added to the 'CHANGELOG.md'.

But mostly when doing the next release, so feels like this could merged @laurentS / @Rested ?

@colin99d
Copy link
Author

@laurentS yeah just let me know exactly where you want this note added so we can get this merged.

@laurentS
Copy link
Owner

laurentS commented Jun 4, 2024

Yes, the changelog is a good place for that. If you make sure to include "breaking change" in the wording, it'll probably help users as well. Thanks for your contribution!

@colin99d
Copy link
Author

colin99d commented Jun 4, 2024

@laurentS done!

@Rested
Copy link
Collaborator

Rested commented Jun 4, 2024

Looks great! think adding some usage docs here

* **exempt_when**: function returning a boolean indicating whether to exempt
and https://github.com/laurentS/slowapi/blob/master/docs/examples.md and a small test around here
def test_exempt_decorator(self, build_starlette_app):
would be ideal before merging!

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.

Feature | Can this support individual limits by IP address or user token?
6 participants