TYPE_CHECKING in ternary expression
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
This issue should be marked with "topic-ternary-expression" label.
Mypy doesn't understand TYPE_CHECKING checked in a ternary expression.
Example 1
from typing import TYPE_CHECKING
from redis.asyncio import ConnectionPool
from redis.asyncio import Redis as OGRedis
from . import settings
Redis = OGRedis[bytes] if TYPE_CHECKING else OGRedis # Redis is Generic in stubs
async def func() -> None:
connection_pool = ConnectionPool.from_url(settings.redis_url)
async with Redis(connection_pool=connection_pool) as redis:
await redis.set(
name="test",
value="Hello World",
)
$ mypy project/
project/code.py:13: error: "object" has no attribute "__aenter__" [attr-defined]
project/code.py:13: error: "object" has no attribute "__aexit__" [attr-defined]
Found 2 errors in 1 file (checked 3 source files)
Example 2
from typing import TYPE_CHECKING
from redis.asyncio import ConnectionPool
from redis.asyncio import Redis as OGRedis
from . import settings
if TYPE_CHECKING: # these
Redis = OGRedis[bytes] # 4 lines
else: # are
Redis = OGRedis # the only difference
async def func() -> None:
connection_pool = ConnectionPool.from_url(settings.redis_url)
async with Redis(connection_pool=connection_pool) as redis:
await redis.set(
name="test",
value="Hello World",
)
$ mypy project/
Success: no issues found in 3 source files
That's either a bug or an expected behavior that can be documented. I can imagine a programmer spending an hour debugging and searching why his/her code isn't validated properly.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia riproducendo entrambi gli esempi Python con mypy e confronta come viene gestito TYPE_CHECKING nell’espressione ternaria rispetto alla forma if/else. Segui il punto di ingresso del controllo dei tipi dell’espressione ternaria per determinare se il comportamento è un bug o se dovrebbe essere documentato. Il lavoro è completato quando l’esempio viene convalidato correttamente oppure il comportamento diverso viene documentato chiaramente, con una copertura di regressione se il comportamento cambia.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100