Can't set RATELIMIT_DEFAULT in .env
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"
Hi, did you try '["12/10seconds"]' ? this is the output of json.dumps(["12/10seconds"]), guess it might work (didn't try it, sorry)
Hi @thentgesMindee I tried as you said RATELIMIT_DEFAULT='["12/10seconds"]' but didn't work.
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.
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)