python / python/mypy

Optional types not being narrowed after a logical-and followed by a test of one of the operands

Aperta
#6,835 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

false-positive feature priority-2-low
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

(Sorry if the title is overly specific)

  • Are you reporting a bug, or opening a feature request?

Bug

  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.
from typing import Optional

def get_org_id(org_name: str) -> str:
    return 'some_org_id'

def create(org_name: Optional[str], org_id: Optional[str]):
    if not org_name and not org_id:
        raise ValueError('No org specified')
    if not org_id:
        org_id = get_org_id(org_name)
  • What is the actual behavior/output?

The inferred type of org_name at the last line is Optional[str] (technically Union[builtins.str, None]).

  • What is the behavior/output you expect?

The type should be narrowed to str.

  • What are the versions of mypy and Python you are using?
    Do you see the same issue after installing mypy from Git master?

Python 3.7.3
mypy: 0.701

  • What are the mypy flags you are using? (For example --strict-optional)

No flags

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 con la riproduzione minima in Python presente nell’issue e conferma il tipo dedotto di org_name dopo le condizioni di AND logico. Traccia il comportamento del restringimento dei tipi per org_name e org_id, quindi aggiungi la copertura per questo caso. Il lavoro è completato quando org_name viene dedotto come str nella chiamata a get_org_id senza compromettere il restringimento dei tipi opzionali correlato.

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
Specificata chiaramente
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.