fix: use sentinel to distinguish unset config_filename from explicit None#280
Open
rahulpar2103 wants to merge 1 commit into
Open
fix: use sentinel to distinguish unset config_filename from explicit None#280rahulpar2103 wants to merge 1 commit into
rahulpar2103 wants to merge 1 commit into
Conversation
…None Previously, config_filename=None and the default (no argument passed) had identical behavior: auto-detect .env. This left no clean way to disable file loading, which breaks Docker/K8s deployments where COPY . . copies .env into the image but env vars are injected by the orchestration layer. Introduce _UNSET sentinel so that: - Limiter(key_func=...) -> auto-detects .env (unchanged) - Limiter(key_func=..., config_filename=None) -> disables file loading - Limiter(key_func=..., config_filename='/path/to/other.env') -> loads that file Fixes laurentS#256
laurentS
reviewed
Jun 13, 2026
laurentS
left a comment
Owner
There was a problem hiding this comment.
@rahulpar2103 thanks for raising this PR. The problem indeed does feel annoying.
I'd be tempted to remove the backwards compatible behaviour here, and only load a config file if explicitly passed. This would both simplify the code in slowapi and follow what Starlette 1.0+ seems to do (and avoid this weird "None means .env" behaviour).
@detrax you raised the original issue, any thoughts on this?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When
COPY . .is used in a Dockerfile, any local.envfile gets bakedinto the image. Inside the container,
os.path.isfile(".env")returnsTrueand slowapi loads it, potentially overriding env vars injected by Docker
--env-fileor Kubernetes secrets. The only workaround was the undocumentedconfig_filename="/dev/null"trick.The deeper issue:
config_filename=None(the default) andconfig_filename=None(explicitly passed) were indistinguishable, so therewas no clean API surface to say "don't load any file."
Closes #256
Fix
Introduce a module-level
_UNSET = object()sentinel. The parameter defaultchanges from
Noneto_UNSET. The logic inside__init__is:config_filename is _UNSET→ existing auto-detect behavior (backward-compatible)config_filename is None→ skip file loading entirelyconfig_filenameis a string → load that specific file (unchanged)Backward compatibility
Anyone who was calling
Limiter(key_func=..., config_filename=None)explicitlywas getting auto-detect behavior, identical to passing nothing. That call now
disables file loading. In practice no one passes the default explicitly, but
this is worth noting in the changelog.
Tests added
test_config_filename_none_disables_dotenv_loading: verifies thatconfig_filename=Nonedoes not load a.envfile present in cwdtest_config_filename_unset_loads_dotenv_when_present: verifies thatomitting the argument still auto-loads
.env(no regression)test_config_filename_unset_no_dotenv_does_not_error: verifies thatthe no-file case does not raise (the original bug scenario)
Docker note
As a complementary mitigation, users should add
.envto.dockerignoreto prevent credentials from being baked into images regardless of library behavior.