Incorrect type narrowing via isinstance after temporary type narrowing on initial object
I maintainer di solito rispondono entro 2 giorni
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 48/100
Direzione di ricerca
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.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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
- Lingua principale
- Rust
- Stelle
- 7.1k
- Fork
- 564
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di facebook/pyrefly
-
[tensor-shapes] torch.lerp rejects tensor weights and omits their broadcast shapeForse già presa @mangeshraut712 l’ha presa 1 giorno fa. Apertapytorch tensor-shapes
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
I maintainer di solito rispondono entro 2 giorni
-
[tensor-shapes] torch.empty overloads omit requires_gradForse già presa @Cha-Imaa l’ha presa 2 giorni fa. Apertapytorch tensor-shapes
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
facebook/pyrefly#5359 · 2 commenti · 1 assegnatario ·
I maintainer di solito rispondono entro 2 giorni
-
Pyrefly fails to report TypedDict mismatch in the presence of (off-by-default) open-unpacking errorForse già presa @rchen152 l’ha presa oggi. Apertatypechecking
facebook/pyrefly#5401 · 1 assegnatario ·
I maintainer di solito rispondono entro 2 giorni
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 52/100
I maintainer di solito rispondono entro 2 giorni
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 48/100
I maintainer di solito rispondono entro 2 giorni
Tutte le issue di facebook/pyrefly
Issue simili
-
QuartoHub: have the Document name before the filename in the browser tabForse già presa @cscheid l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
quarto-dev/q2#930 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
muse P3
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
HerringtonDarkholme/hd-lang#923 · 1 commento ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
longbridge/gpui-kit#3712 ·
I maintainer di solito rispondono entro 1 giorno
-
documentation enhancement SIEM
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
ADORSYS-GIS/wazuh-agent-status#265 ·
I maintainer di solito rispondono entro 2 giorni