Skip to content

3.10 union operator ( | ) doesn't work in subscripts when creating type aliases #10901

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

Closed
TamzinHadasa opened this issue Aug 1, 2021 · 1 comment
Labels
bug mypy got something wrong

Comments

@TamzinHadasa
Copy link

TamzinHadasa commented Aug 1, 2021

Bug Report

3.10 introduces the X | Y syntax for type annotations. Within function and variable annotations, Mypy in 3.10 treats X | Y as identical to Union[X, Y]. Within type aliases, however, it doesn't do so within subscripts to list, dict, and Callable types.

To Reproduce
In 3.10:

from typing import Callable

def test_function(n: str | int) -> str | int: pass

list_annotation_test: list[str | int] = [1, 2, 3, "cat"]  # No error.
ListAliasTest = list[str | int]  # Error.

dict_annotation_test: dict[str|int, str | int] = {1: 'a', 2: 'b'}  # No error.
DictAliasTest = dict[str|int, str | int]  # Error.

callable_annotation_test: Callable[[str | int], str | int] = test_function  # No error.
CallableAliasTest = Callable[[str | int], str | int]  # Error.

Expected Behavior

I would not expect an error from any alias testcase that doesn't arise in the corresponding annotation test case.

Actual Behavior

ListAliasTest raises the following errors:

Type expected within [...]
Invalid type alias: expression is not a valid type
Value of type "Type[List[Any]]" is not indexable

DictAliasTest is the same, just "Type[Dict[Any]]" instead of "Type[List[Any]]". CallableAliasTest raises just the "not a valid type" error.

If this isn't a bug, and there is some reason for the | operator to be, when used in subscripts within type aliases, an exception from the general assumption that X | Y == Union[X, Y], there should be a clearer error message explaining why this doesn't work.

This is my first bug report here, so apologies in advance if I've done anything wrong, and please let me know if there's anything I've forgotten to include.

Your Environment

  • Mypy version used: 0.910
  • Mypy command-line flags: --ignore-missing-imports --follow-imports=silent --show-column-numbers (same with or without --strict)
  • Python version used: 3.10.0b4; was the same on 3.10.0b1
  • Operating system and version: Windows 10 Home, 10.0.19042 Build 19042
@TamzinHadasa TamzinHadasa added the bug mypy got something wrong label Aug 1, 2021
@hauntsaninja
Copy link
Collaborator

Thanks, this should already be fixed on latest master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants