Description
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