Optional types not being narrowed after a logical-and followed by a test of one of the operands
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 20.6k
- Forks
- 3.3k
- Métricas de merge de PR
- Métricas de PR pendientes
Descripción
(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
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza con la reproducción mínima en Python del issue y confirma el tipo inferido de org_name después de las condiciones de and lógico. Rastrea el comportamiento del estrechamiento de tipos para org_name y org_id y añade cobertura para este caso. La tarea estará completada cuando org_name se infiera como str en la llamada a get_org_id sin romper el estrechamiento de tipos opcionales relacionado.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- compilers
- Tipo de issue
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bien especificado
- Aptitud para principiantes
- 35/100