Type-narrowing does not apply across 2 branches having the same condition within the same block
Ninguém assumiu esta issue ainda.
Avaliação
- Dificuldade
- 4/5
- Tempo estimado
- 3-5 dias
- Facilidade para iniciantes
- 42/100
Direção de pesquisa
Comece com o reprodutor a.py e execute mypy com --strict-optional para confirmar o diagnóstico atual. Rastreie o estreitamento de tipo para os dois blocos if created: repetidos e, em seguida, adicione cobertura mostrando que o segundo uso de a é aceito sem erros.
Escrita pelo modelo de indexação a partir do texto da issue.
Descrição
-
Are you reporting a bug, or opening a feature request?
Feature request -
Please insert below the code you are checking with mypy,
from typing import Optional
def my_func() -> bool:
return True
created: bool = my_func()
a: Optional[int] = None
if created:
a = 1
z = a + 1 # This is OK
if created:
x = a + 1 # Unsupported operand types for + ("None" and "int")
- What is the actual behavior/output?
$ mypy a.py
a.py:13: error: Unsupported operand types for + ("None" and "int")
a.py:13: note: Left operand is of type "Optional[int]"
Found 1 error in 1 file (checked 1 source file)
- What is the behavior/output you expect?
No errors. Mypy should be able to narrow the type of a to just int in the second if block, because it narrowed it to that type already the first if block and they have the exact same condition.
You can argue that it's easy to workaround it be merging the if blocks, but in real-life code, there could easily be intervening code that is not trivial to move around.
-
What are the versions of mypy and Python you are using?
mypy 0.750+dev.5b73e2a5e62d3da723ac517fabacaa2a0c8cdf3c
Python 3.7.4 -
What are the mypy flags you are using? (For example --strict-optional)
--strict-optional
- Linguagem predominante
- Python
- Estrelas
- 20.6k
- Forks
- 3.3k
- Merge médio
- 1d 18h
- PRs com merge (30d)
- 54
Guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Mais de python/mypy
-
bug
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 75/100
-
bug
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 78/100
-
bug
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 76/100
-
documentation
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 72/100
-
bug topic-configuration topic-error-reporting
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 68/100
Todas as issues de python/mypy
Issues semelhantes
-
area/auth bug comp/agent P3 platform/discord type/security
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 88/100
NousResearch/hermes-agent#117848 ·
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 74/100
bancolombia/sentinel#23 ·
-
test md AbertaCI
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 74/100
langchain-ai/deepagents#6450 ·
-
bug client
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 88/100