8000 tuple length type narrowing · Issue #2474 · microsoft/pyright · GitHub
[go: up one dir, main page]

Skip to content
tuple length type narrowing #2474
@GBeauregard

Description

@GBeauregard

Describe the bug
pyright should support type narrowing based on tuple length to avoid the false positive in the following code snippet:

from typing import Union

MultiTuple = Union[tuple[object], tuple[object, object]]

def process_tuples(tup: MultiTuple) -> object:
    if len(tup) == 1:
        return tup[0]
    else:
        first, second = tup
        return second
  /home/gregory/Downloads/testcase/pytest/tuplesize.py:9:9 - error: Tuple size mismatch: expected 2 but received 1 (reportGeneralTypeIssues)

This came up in the rich codebase, and there's a corresponding mypy issue with an open PR: python/mypy#1178

Expected behavior
pyright should be able to type narrow tuples based on len()

VS Code extension or command-line
pyright 1.1.180 Linux commandline

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

      0