Skip to content

Ternary expression with () is inferred as object #7738

Closed
@jolaf

Description

@jolaf

The following code:

from typing import Sequence

def f(b: bool) -> Sequence[int]:
    return (5,) if b else ()

produces the following output:

$ python3 Test.py
$ mypy Test.py
Test.py:4: error: Incompatible return value type (got "object", expected "Sequence[int]")
Found 1 error in 1 file (checked 1 source file)
$ mypy --version
mypy 0.740
$ python3 --version
Python 3.6.8

This looks like a false positive to me.

It seems that () should be compatible with any non-empty Tuple[...] or Sequence[...]. The same goes to [].

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions