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

Can't set RATELIMIT_DEFAULT in .env #134

Open
Alurith opened this issue Mar 23, 2023 · 4 comments
Open

Can't set RATELIMIT_DEFAULT in .env #134

Alurith opened this issue Mar 23, 2023 · 4 comments

Comments

@Alurith
Copy link

Alurith commented Mar 23, 2023

Describe the bug
If i try to set the variable RATELIMIT_DEFAULT in my .env file, I get this error
ValueError: Config 'RATELIMIT_DEFAULT' has value '["12/10seconds"]'. Not a valid NoneType.

This is how I create the limiter, w/o any other configuration.
limiter = Limiter(key_func=get_remote_address)

Your app:

  • fastapi 0.91.0
  • slowapi 0.1.7

Additional context
I tried writing the ratelimit string in these ways:

  • ["12/10seconds"]
  • "12/10seconds"
@thentgesMindee
Copy link
Collaborator

Hi, did you try '["12/10seconds"]' ? this is the output of json.dumps(["12/10seconds"]), guess it might work (didn't try it, sorry)

@Alurith
Copy link
Author

Alurith commented Mar 23, 2023

Hi @thentgesMindee I tried as you said RATELIMIT_DEFAULT='["12/10seconds"]' but didn't work.

@thentgesMindee
Copy link
Collaborator

Alright, looking at the code, I think there might be an issue in the code:
in extension.py, here is the line in charge of loading this env var (C.DEFAULT_LIMITS being "RATELIMIT_DEFAULT")

conf_limits: Optional[StrOrCallableStr] = self.get_app_config(
    C.DEFAULT_LIMITS, None
)

When looking at the code for get_app_config:

def get_app_config(self, key: str, default_value: T = None) -> T:
    """
    Place holder until we find a better way to load config from app
    """
    return self.app_config(key, default=default_value, cast=type(default_value))

It means, it will try to cast any limit to None since None is the default value, hence your error Not a valid NoneType.

An alternative, without having to fix the code if your change is urgent, would be to use a different environment variable, and pass it manually with default_limits kwargs when initializing your Limiter instance.

@thentgesMindee
Copy link
Collaborator

Also, this made me see this the comment Place holder until we find a better way to load config from app, @laurentS maybe we could use pydantic's BaseSettings class to defer this config loading logic to a library specialized on that ? (Might not be the right place to start this discussion, feel free to move it somewhere else)

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

No branches or pull requests

2 participants