python / python/typeshed

Opaque type for io.TextIOWrapper seek and tell?

オープン
#7,921 コメント 1 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

topic: io
主要言語
Python
スター
5.1k
フォーク
2.1k
平均マージ
1日 19時間
マージ済み PR(30日)
82

説明

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.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、typeshed にある io.TextIOWrapper seek() と tell() のアノテーションを、issue で引用されている Python ドキュメントと併せて確認してください。完全な変更では、tell() の結果を不透明な値として表現し、0 と tell() が返した値は引き続き許可しつつ、seek() が任意のオフセットを受け取らないようにします。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
tooling
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。