Skip to content

_operator._compare_digest is incorrectly typed #15617

@jonathandung

Description

@jonathandung

When working on #15611, I realized that the internal _compare_digest function, which accepted two strings or anything that implements the buffer protocol (so-called 'bytes-like objects'), was annotated to only take two AnyStrs, the upper bound of which is str and bytes (though type checkers understand that bytearray is accepted also). Therefore, the following snippet, which prints True at runtime without fault, will fail type checking:

import _operator
class test:
    def __buffer__(self, flags, /): return memoryview(b'')
    def __release_buffer__(self, buffer, /): buffer.release()
print(_operator._compare_digest(test(), test()))

The argument annotation should be changed. Another TypeVar, constrained to str and collections.abc.Buffer, is likely needed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions