-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Feature Request: Type-narrowing of a list item for an inline list multiplied with a type-guard on said item #11344
Description
If you have a question about a behavior that you’re seeing in pyright, consider posting to the Pyright discussion forum.
Is your feature request related to a problem? Please describe.
Multiplying an inline list with a typeguard results in List[Unknown]. I'm opening this as a feature request, not a bug, as the feature I want is better described by the current title, and List[Unknown] seems like an intentional symptom of that missing feature.
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.
Describe the solution you’d like
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])As shown by the playground, unlike mypy, pyright already does type-narrowing on bool, so I'm essentially asking to propagate that type narrowing to inline lists `mul'ed with a typeguard.