Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Tracked by #4819
AlexWaygood opened this issue Nov 21, 2022 · 4 comments · Fixed by #14181
Assignees
Labels
affects-typeshed Anything that blocks a typeshed change bug mypy got something wrong false-positive mypy gave an error on correct code topic-pep-604 PEP 604 (union | operator) topic-type-alias TypeAlias and other type alias issues

Comments

@AlexWaygood
Copy link
Member

AlexWaygood commented Nov 21, 2022

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

@AlexWaygood AlexWaygood added bug mypy got something wrong false-positive mypy gave an error on correct code topic-pep-604 PEP 604 (union | operator) topic-type-alias TypeAlias and other type alias issues affects-typeshed Anything that blocks a typeshed change labels Nov 21, 2022
@ilevkivskyi
Copy link
Member

cc @JukkaL who recently worked on this.

@JukkaL JukkaL self-assigned this Nov 24, 2022
JukkaL added a commit that referenced this issue Nov 24, 2022
Fix aliases like this and other aliases involving new-style unions:
```
A = type[int] | str
```

Fixes #12392. Fixes #14158.
JukkaL added a commit that referenced this issue Nov 24, 2022
JukkaL added a commit that referenced this issue Nov 25, 2022
Fix aliases like this and other aliases involving new-style unions:
```
A = type[int] | str
```

Fixes #12392. Fixes #14158.
@adam-grant-hendry
Copy link

Question: I have this issue in Python 3.8 in a normal .py file, but am able to get around it by 'stringifying' the type:

from __future__ import annotations

from typing import TYPE_CHECKING, Callable

from poetry.console.application import Application

if TYPE_CHECKING:
    import sys

    from poetry.poetry import Poetry

    if sys.version_info >= (3, 10):
        from typing import TypeAlias
    else:
        from typing_extensions import TypeAlias

ApplicationFactory: TypeAlias = 'Callable[[Poetry | None], Application]'
PoetryFactory: TypeAlias = 'Callable[[str | None], Poetry]'

Is this intended behavior for 3.8 with from __future__ import annotations, or should I not need to stringify the type?

@AlexWaygood
Copy link
Member Author

AlexWaygood commented Aug 25, 2023

@adam-grant-hendry that's correct behaviour from mypy. It'll fail at runtime on Python <3.10 if you don't stringify the alias, even with from __future__ import annotations. Mypy alerting you to this fact is a feature, not a bug.

from __future__ import annotations only impacts the way Python evaluates annotations at runtime; it doesn't have any impact on how Python evaluates ordinary assignments at runtime. Both ApplicationFactory and PoetryFactory are using PEP-604 syntax in assignments rather than in annotations.

@adam-grant-hendry
Copy link

that's correct behaviour from mypy

Excellent, thank you for confirming! Glad to know I did this properly 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects-typeshed Anything that blocks a typeshed change bug mypy got something wrong false-positive mypy gave an error on correct code topic-pep-604 PEP 604 (union | operator) topic-type-alias TypeAlias and other type alias issues
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants