python / python/mypy

Some string `in` `Union[bytes, str]` does not produce an error

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

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

bug
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

Given the following code

from typing import Union

foo: Union[str, bytes]  
"zoo" in foo  # no error
"zoo" + foo  # err: Unsupported operand types for + ("str" and "bytes")

I would have expected that mypy complains about str not compatible with bytes just like it does for the + operand.

I've tried the current typeshed version at 3d140160 because it just looked like there was a change here in Oct 2020 after mypy 0.790.

For example it now defines for class(str):

    def __contains__(self, o: Union[str, str]) -> bool: ...  # type: ignore
...
    def __iter__(self) -> Iterator[str]: ...

(__contains__ does not trigger here but its Union[str, str] is weird.)

Your Environment

mypy 0.790
python 3.8
builtin typeshed and @3d140160
Win 10

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

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

はじめの一歩

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

調査の方向性

まず mypy 0.790 で Union[str, bytes] の例を再現し、次に str.contains の typeshed 定義と、関連する Union の処理を調べます。"zoo" in foo"zoo" + foo の診断を比較し、包含式が加算式と一貫して互換性のない str/bytes のケースを報告すれば完了です。

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

評価

技術スタック
python
領域
devtools
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

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

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