Incorrect type narrowing via isinstance after temporary type narrowing on initial object
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 48/100
調査の方向性
Start by reproducing the reported narrowing behavior in the linked Pyrefly sandbox, focusing on the foo and foo2 examples and their reveal_type results. Trace the type-checker's handling of isinstance and attribute narrowing; done means the final narrowing results and bar diagnostics match the expectations shown in the issue.
索引モデルが issue の本文から書いたものです。
説明
Describe the Bug
I have a function in which I pass some object with an attribute x of class A. A.x has type int | None. A has a subclass B which narrows the type of B.x to just int.
I can use an isinstance(o, B) check to narrow o.x to int.
However after checking if o.x where o is not narrowed to B, the instance check fails to apply any further narrowing, regardless of what was done with the if o.x check.
Checking if o.x while o is narrowed to B has no such effect.
This snippet illustrates the problem:
from typing import reveal_type
import dataclasses
@dataclasses.dataclass(frozen=True)
class A:
x: int | None
y: int
@dataclasses.dataclass(frozen=True)
class B(A):
x: int
y: int | None
def foo(o: A):
reveal_type(o.x) # int | None <- correct
bar(o.x) # error <- correct
if isinstance(o, B):
reveal_type(o.x) # int <- correct
bar(o.x) # no error <- correct
if o.x: pass
reveal_type(o.x) # int <- correct
bar(o.x) # no error <- correct
if o.x: pass
reveal_type(o.x) # int | None <- correct
bar(o.x) # error <- correct
if isinstance(o, B):
reveal_type(o.x) # int | None <- incorrect. correct: int
bar(o.x) # error <- incorrect
if o.x: return
reveal_type(o.x) # Literal[0] | None <- correct.
bar(o.x) # error <- correct
if isinstance(o, B):
reveal_type(o.x) # Literal[0] | None <- incorrect. correct: int
bar(o.x) # error <- incorrect
def bar(x: int):
pass
The same thing happens for less narrow types in a subclass (which yields a bad-override error on definition, but still shouldn't behave like this).
def foo2(o: A):
reveal_type(o.y) # int <- correct
bar(o.y) # no error <- correct
if isinstance(o, B):
reveal_type(o.y) # int | None <- correct
bar(o.y) # error <- correct
if o.y: pass
reveal_type(o.y) # int | None <- correct
bar(o.y) # error <- correct
if o.y: pass
if isinstance(o, B):
reveal_type(o.y) # int <- incorrect. correct: int | None
bar(o.y) # no error <- incorrect
if o.y: return
reveal_type(o.y) # Literal[0] <- correct.
bar(o.y) # no error <- correct
if isinstance(o, B):
reveal_type(o.y) # Literal[0] <- incorrect. correct: Literal[0] | None
bar(o.y) # no error <- incorrect
I first encountered this bug with pyrefly 1.2.0, but I confirmed that is still occurs with 1.3.1.
Sandbox Link
(Only applicable for extension issues) IDE Information
No response
- 主要言語
- Rust
- スター
- 7.2k
- フォーク
- 578
- PR マージ指標
- 30日以内にマージされた PR はありません
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
facebook/pyrefly のほかの issue
-
`errors` config docs don't make sense対応中かも @i098 が今日担当しました。 オープンdocumentation has-open-pr
難易度 1/5 1時間未満 初心者へのやさしさ 82/100
-
難易度 3/5 1〜2日 初心者へのやさしさ 45/100
-
narrowing typechecking
難易度 4/5 3〜5日 初心者へのやさしさ 45/100
-
Handle editable installs with .pth files対応中かも @connernilsen が 1 日前に担当しました。 オープンconfiguration
-
難易度 3/5 1〜2日 初心者へのやさしさ 58/100
facebook/pyrefly の issue をすべて見る
似ている issue
-
comp-view potential bug
難易度 2/5 1〜3時間 初心者へのやさしさ 80/100
ClickHouse/ClickHouse#128410 ·
メンテナーはふだん 1 日以内に返信
-
area: app data-loss priority: high type: bug
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
メンテナーはふだん 1 日以内に返信
-
skip-release
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
horizon-rust/horizon#5505 ·
メンテナーはふだん 1 日以内に返信
-
area: cloud S3: minor
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
manaflow-ai/cmux#29709 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
area:mem good first issue priority:low type:docs
難易度 2/5 1〜3時間 初心者へのやさしさ 90/100
メンテナーはふだん 1 日以内に返信