Optional types not being narrowed after a logical-and followed by a test of one of the operands
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
(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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit der minimalen Python-Reproduktion im Issue und bestätige den abgeleiteten Typ von org_name nach den logischen Und-Bedingungen. Verfolge das Verhalten der Typ-Eingrenzung für org_name und org_id und füge eine Abdeckung für diesen Fall hinzu. Erledigt ist die Aufgabe, wenn org_name beim Aufruf von get_org_id als str abgeleitet wird, ohne die verwandte Eingrenzung optionaler Typen zu beeinträchtigen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 35/100