Skip to content

codecs._Decoder is incorrectly annotated as taking input: bytes, while at runtime it receives memoryview #10656

@denballakh

Description

@denballakh

How to check runtime bevahiour:

# main.py
import codecs

def decode(input: bytes, errors: str = 'strict') -> tuple[str, int]:
    print(type(input))
    return '', len(input)

def encode(input: str, errors: str = 'strict') -> tuple[bytes, int]:
    raise NotImplementedError

def search(name: str) -> codecs.CodecInfo | None:
    if name == 'test':
        return codecs.CodecInfo(encode, decode)
    return None

codecs.register(search)
import a

# a.py:
# coding: test
a = 1

When i run this code, decode prints <class 'memoryview'>, which is inconsistent with codecs._Decoder annotation.
Annotating input as memoryview doesnt help, because codecs.CodecInfo accepts only function that takes input arg of type bytes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions