python / python/mypy

[regression] no type checking beyond perceived isinstance guard failure

Aperta
#18,329 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-reachability topic-usability
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug Report

Starting in mypy 1.5.0 (at least when testing in versions available in playground), there are situations where an isinstance type guard that returns False will cause type checking to stop in the false branch. The examples below check correctly in 1.4.1 in the playground.

To Reproduce
Heres a pretty dumb example

x: str
assert isinstance(x, int)
y: str = 1 # expected error here, got none

Gist
Playground

Another example:

from typing import Optional

class Foo:
    x: Optional[str]
    
assert isinstance(Foo.x, int)
Foo.x.y.z = 1 # expected error here

These examples are lame, but there are legitimate use cases for this to work. For example, we use metaclasses to transform these type annotations on fields into class attributes that have underlying data. So a more real example in my use case (that cant be reproduced in a playground), would be:

class Foo(Model):
	x: Optional[int] = field(max_length=1)

assert isinstance(Foo.x, StringField) # under the hood, we transform `Foo.x` into a `StringField` because of the annotation
assert Foo.x.max_lengt == 1 # expectd failures since max_lengt is a typo

Note that this behavior is mostly the same with if isinstance...: (and then the rest of the code within the block fails to check), though it seemed that some of those examples were also broken in 1.4.1, which is different than when using assert.

Expected Behavior
I would expect mypy to correctly type check even if it thinks an assert isinstance or if isinstance would result in the rest of the code being unreachable.

Note that I do not want to use warn-unreachable here because that would not actually solve the underlying problem, it would just give me extra warnings and still not actually let me write the isinstance checks that I want.

Actual Behavior
Mypy appears to give up type checking entirely - and silently - if it thinks that the rest of the code is unreachable.

Your Environment
My environment for all the examples is mypy playground

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Esegui gli esempi minimi in mypy playground con mypy 1.4.1 e 1.5.0, quindi confronta i rami assert e if. Traccia il percorso del controllo dei tipi per il narrowing con isinstance e i rami irraggiungibili; il lavoro è completato quando il ramo false continua a segnalare y: str = 1 e l'attributo scritto in modo errato senza richiedere warn-unreachable.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.