python / python/typing

ByteString, specifying length for any bytes-like variable

Open
#997 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

topic: feature
Dominant language
Python
Stars
1.8k
Forks
302
Avg merge
23h
Merged PRs (30d)
8

Description

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?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No file, test, or entry point is named. Start by reviewing the existing ByteString typing behavior and related typing discussions, then determine whether bytes-like types, fixed or bounded lengths, and writability can be represented without mypy errors. Done would require an agreed annotation design and documented type-checking behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
developer-experience
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.