Skip to content

False-positive error when using Callable in a PEP 604 type alias in a stub file with --python-version=3.9 #14158

Closed
@AlexWaygood

Description

@AlexWaygood

Bug Report

In a stub file, with --python-version=3.9, mypy emits false positive errors for the following type aliases:

from typing import Callable
from typing_extensions import TypeAlias

X = int | Callable[[], str | bool]
XX: TypeAlias = int | Callable[[], str | bool]
Y = int | Callable[[str | bool], str]
YY: TypeAlias = int | Callable[[str | bool], str]

For all of these, mypy's error message is:

error: Unsupported left operand type for | ("Type[str]")  [operator]

Note that this snippet works fine in a .py or .pyi file with --python-version 3.10. It's only --python-version 3.9 with a .pyi file that has the bug.

To Reproduce

  1. cd into an up-to-date local clone of mypy with an editable install.
  2. Copy the above snippet into a test.pyi file.
  3. Run mypy test.pyi --python-version 3.9

Expected Behavior

No error should be emitted. PEP 604 syntax should be legal in stub files, even with --python-version 3.9.

Actual Behavior

False-positive errors are emitted.

Your Environment

  • Mypy version used: c660354
  • Mypy command-line flags: --python-version 3.9
  • Python version used: 3.10

Cc. @ilevkivskyi (x-ref #12393).

Metadata

Metadata

Assignees

Labels

affects-typeshedAnything that blocks a typeshed changebugmypy got something wrongfalse-positivemypy gave an error on correct codetopic-pep-604PEP 604 (union | operator)topic-type-aliasTypeAlias and other type alias issues

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions