Skip to content

Narrow input type annotation for GLib.Bytes.new #198

@phistep

Description

@phistep

The type annotation for GLib.Bytes.new() input argument data is confusingly narrow: Sequence[int].

def new(cls, data: typing.Optional[typing.Sequence[int]] = None) -> Bytes: ...

Yet the following code works without a problem:

import io

import gi
from gi.repository import GLib

data = b"hi mom."
array = GLib.Bytes.new(data)
array = GLib.Bytes.new(bytearray(data))
array = GLib.Bytes.new(memoryview(data))
array = GLib.Bytes.new(io.BytesIO(data).getvalue())

I think the type annotation should reflect this.

I made a PR that changes the type to typing.Union[typing.Sequence[int], bytes] but I'm neither sure that this will not have unwanted consequences for other classes nor that this is exhaustive for all the possible values shown above or what else might be legal to pass. I hope someone more knowledgeable can figure out what the maximal type annotation would be.

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