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.
When working on #15611, I realized that the internal
_compare_digestfunction, which accepted two strings or anything that implements the buffer protocol (so-called 'bytes-like objects'), was annotated to only take twoAnyStrs, the upper bound of which isstrandbytes(though type checkers understand thatbytearrayis accepted also). Therefore, the following snippet, which printsTrueat runtime without fault, will fail type checking:The argument annotation should be changed. Another
TypeVar, constrained tostrandcollections.abc.Buffer, is likely needed.