python / python/mypy

Type detection broken on attribute override and not-equal-assertion

Aperta
#11,226 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Bug Report

I commonly find myself writing simplified list init parameters. So, if the user provides only a type X, I automatically convert that to list type [X]. In doing so, I found two issue that prevent a proper type detection. See the example code below.

To Reproduce

#!/usr/bin/env python3
from typing import Union, List, Optional

def test(a: Union[List[str], str, None] = None) -> Optional[List[str]]:
    if type(a) == str:
        a = [a]
    assert(type(a) != str)
    return a

Expected Behavior

  1. First, the type(a) == str check and consequent list assignment should remove the str type from the Union choices.
  2. Second, the assert(!=) check should also remove the str type from the Union options.

Actual Behavior

Both checks do not work and the type is still guessed incorrectly:

screen

Your Environment

  • Mypy version used: 0.910
  • Mypy command-line flags: pip install
  • Mypy configuration options from mypy.ini (and other config files): -
  • Python version used: 3.8
  • Operating system and version: macOS 10.15

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

Inizia eseguendo il riproduttore Python fornito con mypy e analizza il comportamento del restringimento dei tipi per l’assegnazione type(a) == str e il controllo assert(type(a) != str). Il lavoro è completato quando entrambi i rami rimuovono str dalla Union inferita e la funzione viene accettata con il tipo di ritorno atteso Optional[List[str]].

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

Valutazione

Stack tecnologico
python
Ambito
devtools
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.