python / python/mypy

[regression] no type checking beyond perceived isinstance guard failure

Ouverte
#18,329 4 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

bug topic-reachability topic-usability
Langage dominant
Python
Étoiles
20.6k
Forks
3.3k
Métriques de merge des PR
Métriques de PR en attente

Description

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

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Exécutez les exemples minimaux dans mypy playground avec mypy 1.4.1 et 1.5.0, puis comparez les branches assert et if. Suivez le chemin de vérification des types pour le narrowing avec isinstance et les branches inatteignables ; c’est terminé si la branche false signale toujours y: str = 1 et l’attribut mal orthographié sans nécessiter warn-unreachable.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
python
Domaine
compilers
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.