Skip to content

file.write for files opened in binary mode #368

@heiner

Description

@heiner

Summary

Given this input test.py:

f = open("myfile", "ab+")
f.write(b"mybytes")

Running ty results in

$ uvx ty check test.py
WARN ty is pre-release software and not ready for production use. Expect to encounter bugs, missing features, and fatal errors.
error[invalid-argument-type]: Argument to bound method `write` is incorrect
 --> /Users/heiner/src/test.py:2:9
  |
1 | f = open("myfile", "ab+")
2 | f.write(b"mybytes")
  |         ^^^^^^^^^^ Expected `str`, found `Literal[b"mybytes"]`
  |
info: Function defined here
   --> stdlib/_io.pyi:125:9
    |
123 |     def __next__(self) -> str: ...  # type: ignore[override]
124 |     def detach(self) -> BinaryIO: ...
125 |     def write(self, s: str, /) -> int: ...
    |         ^^^^^       ------ Parameter declared here
126 |     def writelines(self, lines: Iterable[str], /) -> None: ...  # type: ignore[override]
127 |     def readline(self, size: int = -1, /) -> str: ...  # type: ignore[override]
    |
info: rule `invalid-argument-type` is enabled by default

Found 1 diagnostic

Version

ty 0.0.1-alpha.1 (12f466e 2025-05-13)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions