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
bool is a truthyness typeguard. ie roughly treating bool(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.
Feature
I would like the following to work:
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:
boolis a truthyness typeguard. ie roughly treatingbool(a)as:def bool[T](a: T | |Literal['', False, 0] | None) -> TypeGuard[T]: ...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)