Skip to content

Add support for typing.ByteString/collections.abc.ByteString #52

Closed

Description

@jcrist

When parsing type signatures for structs, it'd be useful to support a more abstract bytes-like object. For encoding this would result in no major changes (encoding only deals with concrete types). For decoding, this would decode the type as bytes (unless an extension type was used).

Activity

  1. jakirkham commented on Sep 9, 2021

    @jakirkham

    This would be great and supporting both makes sense.

    It's worth noting the docs say...

    Deprecated since version 3.9: collections.abc.ByteString now supports []. See PEP 585 and Generic Alias Type.

  2. jakirkham commented on Sep 9, 2021

    @jakirkham

    Also interesting in collections.abc.ByteString they mention memoryviews are supported, but that doesn't appear to be the case.

    import collections.abc
    import typing
    
    print(isinstance(memoryview(bytes()), collections.abc.ByteString))
    print(isinstance(memoryview(bytes()), typing.ByteString))
    False
    False
    
  3. jcrist commented on Sep 9, 2021

    @jcrist
    MemberAuthor

    Oh, interesting. That certainly seems like a bug (at least in the documentation). mypy does count memoryviews here. You can make the runtime check work by calling

    collections.abc.ByteString.register(memoryview)
    

    but that applies a global change that may break things elsewhere (but is probably harmless).

  4. jakirkham commented on Sep 9, 2021

    @jakirkham

    Good point

    This upstream issue ( https://bugs.python.org/issue33864 ) looks related. Seems like there is confusion as to if/how this should be included

    Independently does annotating with memoryview work currently?

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions