Assigning a variable from either of two optional variables

Aperta
#11,404 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

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

Direzione di ricerca

Usa il riproduttore Python fornito come primo punto di ingresso ed esegui mypy sull’assegnazione x or y dopo il ritorno anticipato. Traccia come viene analizzato questo flusso di controllo; il lavoro è completato quando l’esempio accetta other come str senza indebolire il comportamento di Optional segnalato altrove.

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

Descrizione

feature topic-type-narrowing

Bug Report

consider the following example:

from typing import Optional


def test_something(x: Optional[str] = None, y: Optional[str] = None) -> str:
    if not (x or y):
        return "early"

    other: str = x or y  # <<< mypy complains that other should be Optional[str]

    return other

am i not able to assume that other would be of type str given that if neither x nor y is given then the function would return early and the assignment of other would never happen? in other words, at the assignment of other, surely one or both of x or y would be of type str, therefore other would be of type str?

to further the argument, if i modify the example like so:

#!/usr/bin/env python3

from typing import Optional


def test_something(x: Optional[str] = None, y: Optional[str] = None) -> str:
    if not (x or y):
        return "early"

    other: str = x or y

    print(f"x = {x}")
    print(f"y = {y}")
    print(f"other class: {type(other)}")
    print()

    return other

test_something()
test_something(x="hello")
test_something(y="goodbye")
test_something(x="hello", y="goodbye")

and run it i get:

x = hello
y = None
other class: <class 'str'>

x = None
y = goodbye
other class: <class 'str'>

x = hello
y = goodbye
other class: <class 'str'>

To Reproduce

run mypy on the example above

Expected Behavior

mypy would not complain about typing other as str

Actual Behavior

running mypy on the above example yields:

.../test.py: note: In function "test_something":
.../test.py:8:18: error: Incompatible types in assignment (expression has type "Optional[str]", variable has type "str")  [assignment]
        other: str = x or y
                     ^
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 0.910
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files):
strict = true
pretty = true
show_column_numbers = true
show_error_codes = true
show_error_context = true
  • Python version used: 3.7.9
  • Operating system and version: MacOSX 10.15.7 Catalina
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Merge medio
1g 18h
PR unite (30g)
54

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.

Altre issue di python/mypy

Tutte le issue di python/mypy

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.