Skip to content

Replace AnyStr with str | bytes union type#464

Closed
kimjune01 wants to merge 1 commit intopallets:mainfrom
kimjune01:fix-anystr-typing
Closed

Replace AnyStr with str | bytes union type#464
kimjune01 wants to merge 1 commit intopallets:mainfrom
kimjune01:fix-anystr-typing

Conversation

@kimjune01
Copy link
Copy Markdown

Fixes #451.

`AnyStr` constrains a function to use either `str` or `bytes` consistently
across all parameters and return type. Several Quart APIs accept both
independently — `AnyStr` was incorrectly restricting callers.

Replace with explicit `str | bytes` union where the constraint isn't needed.

Prior work: #452 addressed the same issue but has been inactive for 6 months.

AnyStr is deprecated in Python 3.13 and was being used incorrectly
throughout the codebase. According to Python typing documentation,
AnyStr should only be used as a TypeVar constraint for generic
functions, not for parameters that accept either str or bytes.

This commit replaces all AnyStr usage with the explicit str | bytes
union type, which is the correct way to annotate parameters that
can accept either type.

Changes:
- Remove AnyStr imports from all affected modules
- Replace AnyStr type hints with str | bytes in:
  - Function parameters (data, send, receive)
  - Protocol method signatures
  - Signal handler type comments

Fixes pallets#451
@davidism davidism closed this May 9, 2026
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.

Incorrect typing using AnyStr

2 participants