Skip to content

Type annotation wrong on TemplateStream.dump? #1983

Open
@alicederyn

Description

@alicederyn

This PR changed the hint for dump from IO to IO[bytes]: #1968

However, I believe if encoding is not provided (or explicitly None), this should be IO[str]? I certainly get a runtime error (TypeError: a bytes-like object is required, not 'str') if I try to change my code to pass in an IO[bytes] to satisfy mypy, while the code works fine at runtime if I just # type: ignore the new mypy errors.

I believe the type hint needs to change to an @overload:

@overload
def dump(
        self,
        fp: t.Union[str, t.IO[bytes]],
        encoding: str,
        errors: t.Optional[str] = "strict",
    ) -> None:
    ...

@overload
def dump(
        self,
        fp: t.Union[str, t.IO[str]],
        encoding: None = None,
        errors: t.Optional[str] = "strict",
    ) -> None:
    ...

Activity

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