Skip to content

ByteString, specifying length for any bytes-like variable #997

Open

Description

@covert-encryption

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?

Activity

  1. hauntsaninja commented on Dec 31, 2021

    @hauntsaninja
    Collaborator

    Seems like a good use for PEP 593 Annotated.

  2. srittau commented on Jan 2, 2022

    @srittau
    Collaborator

    Duplicate of #593.

  3. James-E-A commented on Feb 11, 2025

    @James-E-A

    @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”.

  4. srittau commented on Feb 12, 2025

    @srittau
    Collaborator

    Thanks, I seem to have misunderstood the suggestion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: featureDiscussions about new features for Python's type annotations

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions