Skip to content

Feature Request: Type-narrowing of a list item for an inline list multiplied with a type-guard on said item #11344

@Avasam

Description

@Avasam

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])

playground url: https://pyright-play.net/?code=GYJw9gtgBALgngBwJYDsDmUkQWEMoCGAzkQKZ4D68CpAULQCanBQUAUwYYAXFETCCgAfKADkwKUgEooAWgB8YiaW60o6zCzYAjLgBsOXKVNUazhEuRhVEpQ2AA0fAVLUbiZStTsBtAEQEfk5%2B2kFQnGAAulAAVFC6YAYRUk56SPw%2B-CCRUkA

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions