Skip to content

stdlib html.escape is annotated as taking AnyStr, but actually crashes when called with bytes #15730

@max-kamps

Description

@max-kamps

Tested in Python version 3.14, not sure if other versions have the same behavior.

html.escape has the following signature:

def escape(s: AnyStr, quote: bool = True) -> AnyStr: ...

But calling html.escape with a bytes or bytearray argument raises the following exception:

File ".../lib/python3.14/html/__init__.py", line 19, in escape
    s = s.replace("&", "&") # Must be done first!
TypeError: a bytes-like object is required, not 'str'

Either the type annotation should be changed to str instead of AnyStr, or someone could update the standard library to support bytes

Metadata

Metadata

Assignees

No one assigned

    Labels

    stubs: false negativeType checkers do not report an error, but should

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions