Skip to content

BufferedWriter is not compatible with SupportsWrite[AnyStr] #5243

@tmke8

Description

@tmke8

Describe the bug
Sorry for the rambling title – I couldn't think of a good description of this:

import shutil
from typing import TYPE_CHECKING, AnyStr
from io import BufferedWriter

if TYPE_CHECKING:
    from _typeshed import SupportsRead, SupportsWrite


def f(s: SupportsRead[AnyStr], t: SupportsWrite[AnyStr]) -> None:  # with TypeVar
    shutil.copyfileobj(s, t)


def g(s: SupportsRead[bytes], t: SupportsWrite[bytes]) -> None:  # with concrete type
    shutil.copyfileobj(s, t)


def h(src: SupportsRead[bytes], tgt: BufferedWriter) -> None:
    f(src, tgt)  # E: Argument of type "BufferedWriter" cannot be assigned to parameter "t" of type "SupportsWrite[AnyStr@f]" in function "f"
    g(src, tgt)

Expected behavior
If I can pass tgt succesfully to g, then I don't see why it would fail with f. Clearly, BufferedWriter is compatible with SupportsWrite[bytes].

VS Code extension or command-line
command-line

Additional context
This happened after updating to the most recent version. I believe there were typeshed changes around this code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    addressed in next versionIssue is fixed and will appear in next published versionbugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions