Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Unsoundness withtyping.IO and friends #1994

Open
Labels
topic: otherOther topics not covered
@JelleZijlstra

Description

@JelleZijlstra

The classestyping.IO,typing.BinaryIO, andtyping.TextIO look like they want to be Protocols or ABCs, but in fact they are defined as regular generic classes, both at runtime and in typeshed. However, they are meant to encompass the concrete IO classes defined in theio module, and in typeshed we implement that by having these classes inherit fromtyping.*IO classes, even though there is no such inheritance at runtime.

This can easily lead to unsound behavior:

importio,typingdeff(x:int|io.BytesIO)->int:ifisinstance(x,typing.BinaryIO):returnx.fileno()returnxf(io.BytesIO())+1# boom

Type checkers thinkBytesIO is a subclass ofBinaryIO, because that's how it's defined in typeshed, but in fact it isn't at runtime.

I can see a few solutions:

  1. Special-casetyping.*IO in the spec and say that type checkers should rejectisinstance()/issubclass() calls involving them.
  2. Deprecate thetyping.*IO classes and eventually remove them, nudging people to use their own Protocols (or the newio.Reader/io.Writer) instead. This is conceptually clean but may be annoying for a lot of users; the typing classes are nice to use in simple application code.
  3. Make these classes actually (runtime-checkable?) Protocols at runtime, though they would be unwieldily large.

Even if we do (2) or (3) type checkers might still want to do (1) since it will be a while before the relevant runtime changes take effect.

(Noticed this while looking intopython/cpython#133492 .)

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: otherOther topics not covered

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp