typing icon indicating copy to clipboard operation
typing copied to clipboard

ByteString, specifying length for any bytes-like variable

Open covert-encryption opened this issue 3 years ago • 4 comments

This request is a combination of two additions to ByteString that apparently are not supported yet, or at least I could find no discussion of them.

  • Support any bytes-like buffers, not only bytes, bytearray and memoryview
  • Allow specifying the length of the buffer expected (in my case it usually must be exactly 32 bytes)

Ideally, Python typing would allow specifying bytes-like of (a) any length, (b) fixed length or (c) min and/or max length. Potentially also separately specifying if a writable buffer is expected.

While I don't expect for mypy to be able to check for all of that, it would at least be nice to include such information in the annotations for documentation purposes, avoiding the use of docstrings for parameter definitions where names and types are descriptive enough. Is there any way to do this without causing mypy errors, considering that I would rather have no type checking but documentative annotations than what the current implementation can do?

covert-encryption avatar Dec 31 '21 15:12 covert-encryption

Seems like a good use for PEP 593 Annotated.

hauntsaninja avatar Dec 31 '21 17:12 hauntsaninja

Duplicate of #593.

srittau avatar Jan 02 '22 07:01 srittau

@srittau Are you sure this issue is a duplicate of that one?

  • Support any bytes-like buffers, not only bytes, bytearray and memoryview

  • Allow specifying the length of the buffer expected (in my case it usually must be exactly 32 bytes)

PEP 688 doesn't even contain the word “length”.

James-E-A avatar Feb 11 '25 20:02 James-E-A

Thanks, I seem to have misunderstood the suggestion.

srittau avatar Feb 12 '25 09:02 srittau