TYPE_CHECKING in ternary expression

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

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

評価

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

調査の方向性

まず、両方の Python の例を mypy で再現し、三項式と if/else 形式で TYPE_CHECKING がどのように扱われるかを比較します。三項式の型チェックのエントリーポイントを追跡して、その動作がバグなのか、ドキュメント化すべきものなのかを判断します。例が正しく検証されるか、異なる動作が明確にドキュメント化され、動作が変更される場合は回帰テストによるカバレッジが追加されれば完了です。

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

説明

documentation topic-ternary-expression

This issue should be marked with "topic-ternary-expression" label.

Mypy doesn't understand TYPE_CHECKING checked in a ternary expression.

Example 1
from typing import TYPE_CHECKING

from redis.asyncio import ConnectionPool
from redis.asyncio import Redis as OGRedis

from . import settings

Redis = OGRedis[bytes] if TYPE_CHECKING else OGRedis  # Redis is Generic in stubs


async def func() -> None:
    connection_pool = ConnectionPool.from_url(settings.redis_url)
    async with Redis(connection_pool=connection_pool) as redis:
        await redis.set(
            name="test",
            value="Hello World",
        )
$ mypy project/
project/code.py:13: error: "object" has no attribute "__aenter__"  [attr-defined]
project/code.py:13: error: "object" has no attribute "__aexit__"  [attr-defined]
Found 2 errors in 1 file (checked 3 source files)
Example 2
from typing import TYPE_CHECKING

from redis.asyncio import ConnectionPool
from redis.asyncio import Redis as OGRedis

from . import settings

if TYPE_CHECKING:  # these
    Redis = OGRedis[bytes]  # 4 lines
else:  # are
    Redis = OGRedis  # the only difference


async def func() -> None:
    connection_pool = ConnectionPool.from_url(settings.redis_url)
    async with Redis(connection_pool=connection_pool) as redis:
        await redis.set(
            name="test",
            value="Hello World",
        )
$ mypy project/
Success: no issues found in 3 source files

That's either a bug or an expected behavior that can be documented. I can imagine a programmer spending an hour debugging and searching why his/her code isn't validated properly.

主要言語
Python
スター
20.6k
フォーク
3.3k
平均マージ
1日 18時間
マージ済み PR(30日)
54

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

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

はじめの一歩

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

python/mypy のほかの issue

python/mypy の issue をすべて見る

似ている issue

Python の issue をもっと見る

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

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