python / python/typeshed

Opaque type for io.TextIOWrapper seek and tell?

Open
#7,921 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

topic: io
Dominant language
Python
Stars
5.1k
Forks
2.1k
Avg merge
1d 19h
Merged PRs (30d)
82

Description

The following prints 18446744073709551620 (as I discovered in https://github.com/python/cpython/issues/93077):

import io
stream = io.TextIOWrapper(io.BytesIO(b'"")\r\nx\r\n'))
stream.read(1)
stream.read(1)
stream.read(1)
print('-----> tell:', stream.tell())

This is not a bug. The documentation says that .tell() returns "an opaque number" that "does not usually represent a number of bytes in the underlying binary storage".

In fact, calling .seek() with anything else than 0 or a return value of .tell() "produces undefined behaviour". I just relied on that today, without knowing it. I wrote this code:

def _upgrade_high_scores_file(file: IO[str], old_version: int) -> None:
    ...
    file.seek(len(b"catris high scores file v"))
    ...

This shouldn't pass the type check IMO.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the io.TextIOWrapper seek() and tell() annotations in typeshed alongside the Python documentation quoted in the issue. A complete change would represent tell() results as opaque values and prevent seek() from accepting arbitrary offsets while still allowing 0 and a value returned by tell().

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.