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 #12393

Closed
Tracked by #4819
AlexWaygood opened this issue Mar 20, 2022 · 8 comments
Closed
Tracked by #4819

False-positive error when using Callable in a PEP 604 type alias #12393

AlexWaygood opened this issue Mar 20, 2022 · 8 comments
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 Mar 20, 2022

Bug Report

The following snippet works fine at runtime on 3.10+. It should be accepted as a valid type alias definition on Python 3.10+ (and should be accepted in stub files regardless of Python version). However, mypy raises a false-positive error. The error does not go away if you use typing.TypeAlias:

from typing import Callable, TypeAlias

A = Callable[[], str] | int  # error: Unsupported left operand type for | ("object")
B: TypeAlias = Callable[[], str] | int  # error: Unsupported left operand type for | ("object")

Mypy does accept the following (with the order of types reversed) as a valid type alias on Python 3.10+ and in stub files:

C = int | Callable[[], str]  # No error using Python 3.10 (in a `.py file), or in a stub file on any Python version.

Much like with #12392, this error is only reproduceable if Callable is the outermost type. The following produces no errors:

D = list[Callable[[], str] | int]

There is also an additional bug that only exists for stub files running with --python-version set to 3.9 or lower, when using a Callable that has a PEP 604 union as its return type or a PEP 604 union in its parameter specification, and when the Callable is itself in a PEP 604 union:

X = int | Callable[[], str | bool]  # error: Unsupported left operand type for | ("Type[str]")
Y = int | Callable[[str | bool], str]  # error: Unsupported left operand type for | ("Type[str]")

To Reproduce

  • Mypy playground link here for the errors which affect stubs and .py files.
  • For the stub-specific errors, paste the code samples into a .pyi file, then run mypy on the file with --python-version=3.9.

Expected Behavior

No errors should be reported.

Cc. @JelleZijlstra

@AlexWaygood AlexWaygood added the bug mypy got something wrong label Mar 20, 2022
@JelleZijlstra JelleZijlstra added the topic-pep-604 PEP 604 (union | operator) label Mar 20, 2022
@AlexWaygood AlexWaygood added the topic-type-alias TypeAlias and other type alias issues label Mar 24, 2022
@AlexWaygood AlexWaygood added the affects-typeshed Anything that blocks a typeshed change label Mar 31, 2022
@AlexWaygood AlexWaygood added the false-positive mypy gave an error on correct code label Jul 7, 2022
@gvanrossum
Copy link
Member

I hit this with T = Callable[[], int] | None. No PEP 604 TypeAlias needed in this repro.

@AlexWaygood
Copy link
Member Author

I hit this with T = Callable[[], int] | None. No PEP 604 TypeAlias needed in this repro.

That is a PEP 604 type alias, and it's the same as my first example :-)

If you were to write that type alias without PEP 604, it would be T = Union[Callable[[], int], None] or T = Optional[Callable[[], int]]

@gvanrossum
Copy link
Member

gvanrossum commented Jul 14, 2022 via email

@ilevkivskyi
Copy link
Member

The original example works correctly on master (assuming you use 3.10+).

@AlexWaygood
Copy link
Member Author

The original example works correctly on master (assuming you use 3.10+).

Not in a stub file on 3.9, I don't think?

@ilevkivskyi
Copy link
Member

Hm, I just tried and first example works in a stub on 3.9, but the last example actually fails in stub on 3.9

X = int | Callable[[], str | bool]  # error: Unsupported left operand type for | ("Type[str]")
Y = int | Callable[[str | bool], str]  # error: Unsupported left operand type for | ("Type[str]")

Maybe open a separate issue to track specifically this case?

@AlexWaygood
Copy link
Member Author

Maybe open a separate issue to track specifically this case?

Okay, I will open a separate issue. On mypy master these all still cause mypy to emit false positives in a stub file with --python-version 3.9:

# test.pyi

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]

@AlexWaygood
Copy link
Member Author

Okay, I will open a separate issue.

(#14158 is the follow-up issue for stubs, for those following along.)

lucascolley added a commit to lucascolley/scipy that referenced this issue Dec 4, 2023
I tried to fix but `mypy` complains along the lines of python/mypy#12393
lucascolley added a commit to lucascolley/scipy that referenced this issue Dec 5, 2023
I tried to fix but `mypy` complains along the lines of python/mypy#12393
stefanv pushed a commit to scipy/scipy that referenced this issue Dec 8, 2023
* MAINT: `noqa` for awkward UP007 violation

I tried to fix but `mypy` complains along the lines of python/mypy#12393

* MAINT: lint: enable line-length check in `cluster`

* MAINT: lint: enable line-length check in `integrate`
[lint only]

* MAINT: lint: enable line-length check in `interpolate`
[lint only]

* MAINT: lint: enable line-length check in `io`
[lint only]

* MAINT: lint: enable line-length check in `ndimage`
[lint only]

* MAINT: lint: enable line-length check in `signal`
[lint only]

* MAINT: lint: enable line-length check in `linalg`
[lint only]

* MAINT: lint: enable line-length check in `spatial`
[lint only]

* MAINT: lint: enable line-length check in `sparse`
[lint only]

* MAINT: lint: enable line-length check in `_lib`
[lint only]

* MAINT: lint: enable line-length check in `stats`
[lint only]

* MAINT: lint: enable line-length check in `special`
[lint only]

* MAINT: lint: enable line-length check in `optimize`
[lint only]

* MAINT: lint: enable line-length check in `benchmarks`
[lint only]

* MAINT: lint: enable line-length check in `tools`

* MAINT: lint: `per-file-ignores` clean-up

* MAINT: PR 19609 revisions
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

No branches or pull requests

4 participants