Skip to content

fix: support single string and callable in default_limits (#239) - #314

Open
agustin18 wants to merge 1 commit into
laurentS:masterfrom
agustin18:fix/support-single-limit-strings-and-callables
Open

agustin18 wants to merge 1 commit into
laurentS:masterfrom
agustin18:fix/support-single-limit-strings-and-callables

Conversation

@agustin18

Copy link
Copy Markdown

When initializing Limiter with default_limits="1/second" (a string) instead of a list ["1/second"], set(default_limits) iterated over individual characters of the string ('1', '/', 's', ...), causing LimitGroup to fail parsing the rate limit with a ValueError during request handling. Similarly, passing a callable raised TypeError: 'function' object is not iterable.

Changes

  • Add _normalize_limits helper to coerce single string, bytes, callable, or sequence into a list.
  • Apply normalization to default_limits, application_limits, and in_memory_fallback in Limiter.__init__.
  • Update type annotations to Union[Sequence[StrOrCallableStr], StrOrCallableStr].
  • Add parameterized unit tests covering single strings and callables for default limits, application limits, and in-memory fallback in both Starlette and FastAPI test suites.
  • Update CHANGELOG.md.

Fixes #239

Coerce single limit string or callable into a list in Limiter.__init__ for default_limits, application_limits, and in_memory_fallback. Previously, passing a single string like default_limits="1/second" caused set(default_limits) to iterate individual characters ('1', '/', ...), failing rate limit parsing and crashing at request time.
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.

🐛 Bug Report: slowapi crashes when default_limits is not a list

1 participant