-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Open
Labels
Description
Feature
I would like the following to work:
from typing import assert_type
def _(foo: str | None) -> None:
assert_type(["a", "b", foo] * bool(foo), list[str])playground url: https://mypy-play.net/?gist=f6454b1662920bc8df00dfe047a80044
To my understanding, this would require 2 things that mypy currently doesn't do as shown by the above playground:
- Understanding
boolis a truthyness typeguard. ie roughly treatingbool(a)as:def bool[T](a: T | |Literal['', False, 0] | None) -> TypeGuard[T]: ... - Doing type-narrowing on
list.__mul__with a type-guard.
Pitch
Let me quote @jaraco directly, as this is a pattern used a lot in his ecosystem (which includes setuptools): jaraco/jaraco.context#16 (comment)
I very much prefer to use algebraic expressions over branching logic. The expressiveness of multiplying by a bool is important to the aesthetic of simplicity here.
Reactions are currently unavailable