11from __future__ import annotations
22
33import os
4- from typing import Any , Dict , Generic , Iterable , Mapping , TypeVar , Union
4+ from collections .abc import Iterable , Mapping
5+ from typing import Any , Generic , TypeVar
56from typing_extensions import Self , assert_type
67
78###################################################################
1516# https://github.com/python/mypy/issues/12358
1617# bad = dict()
1718good : dict [str , str ] = dict ()
18- assert_type (good , Dict [str , str ])
19+ assert_type (good , dict [str , str ])
1920
20- assert_type (dict (arg = 1 ), Dict [str , int ])
21+ assert_type (dict (arg = 1 ), dict [str , int ])
2122
2223_KT = TypeVar ("_KT" )
2324_VT = TypeVar ("_VT" )
@@ -37,11 +38,11 @@ def __getitem__(self, __k: _KT) -> _VT:
3738
3839
3940kt1 : KeysAndGetItem [int , str ] = KeysAndGetItem ({0 : "" })
40- assert_type (dict (kt1 ), Dict [int , str ])
41+ assert_type (dict (kt1 ), dict [int , str ])
4142dict (kt1 , arg = "a" ) # type: ignore
4243
4344kt2 : KeysAndGetItem [str , int ] = KeysAndGetItem ({"" : 0 })
44- assert_type (dict (kt2 , arg = 1 ), Dict [str , int ])
45+ assert_type (dict (kt2 , arg = 1 ), dict [str , int ])
4546
4647
4748def test_iterable_tuple_overload (x : Iterable [tuple [int , str ]]) -> dict [int , str ]:
@@ -53,12 +54,12 @@ def test_iterable_tuple_overload(x: Iterable[tuple[int, str]]) -> dict[int, str]
5354dict (i1 , arg = "a" ) # type: ignore
5455
5556i2 : Iterable [tuple [str , int ]] = [("a" , 1 ), ("b" , 2 )]
56- assert_type (dict (i2 , arg = 1 ), Dict [str , int ])
57+ assert_type (dict (i2 , arg = 1 ), dict [str , int ])
5758
5859i3 : Iterable [str ] = ["a.b" ]
5960i4 : Iterable [bytes ] = [b"a.b" ]
60- assert_type (dict (string .split ("." ) for string in i3 ), Dict [str , str ])
61- assert_type (dict (string .split (b"." ) for string in i4 ), Dict [bytes , bytes ])
61+ assert_type (dict (string .split ("." ) for string in i3 ), dict [str , str ])
62+ assert_type (dict (string .split (b"." ) for string in i4 ), dict [bytes , bytes ])
6263
6364dict (["foo" , "bar" , "baz" ]) # type: ignore
6465dict ([b"foo" , b"bar" , b"baz" ]) # type: ignore
@@ -71,19 +72,19 @@ def test_iterable_tuple_overload(x: Iterable[tuple[int, str]]) -> dict[int, str]
7172int_value = 1
7273
7374assert_type (d_any ["key" ], Any )
74- assert_type (d_any .get ("key" ), Union [ Any , None ] )
75- assert_type (d_any .get ("key" , None ), Union [ Any , None ] )
75+ assert_type (d_any .get ("key" ), Any | None )
76+ assert_type (d_any .get ("key" , None ), Any | None )
7677assert_type (d_any .get ("key" , any_value ), Any )
7778assert_type (d_any .get ("key" , str_value ), Any )
7879assert_type (d_any .get ("key" , int_value ), Any )
7980
8081assert_type (d_str ["key" ], str )
81- assert_type (d_str .get ("key" ), Union [ str , None ] )
82- assert_type (d_str .get ("key" , None ), Union [ str , None ] )
82+ assert_type (d_str .get ("key" ), str | None )
83+ assert_type (d_str .get ("key" , None ), str | None )
8384# Pyright has str instead of Any here
8485assert_type (d_str .get ("key" , any_value ), Any ) # pyright: ignore[reportAssertTypeFailure]
8586assert_type (d_str .get ("key" , str_value ), str )
86- assert_type (d_str .get ("key" , int_value ), Union [ str , int ] )
87+ assert_type (d_str .get ("key" , int_value ), str | int )
8788
8889# Now with context!
8990result : str
@@ -184,16 +185,16 @@ def test_dict_dot_or(
184185 assert_type (a | b , dict [int , int ])
185186 assert_type (b | a , dict [int , int ])
186187
187- assert_type (a | c , dict [Union [ int , str ], Union [ int , str ] ])
188+ assert_type (a | c , dict [int | str , int | str ])
188189
189190 # arbitrary mappings are not accepted by `dict.__or__`;
190191 # it has to be a subclass of `dict`
191192 a | d # type: ignore
192193
193194 # but Mappings such as `os._Environ` or `CustomMappingWithDunderOr`,
194195 # which define `__ror__` methods that accept `dict`, are fine:
195- assert_type (a | os .environ , dict [Union [ str , int ], Union [ str , int ] ])
196- assert_type (os .environ | a , dict [Union [ str , int ], Union [ str , int ] ])
196+ assert_type (a | os .environ , dict [int | str , int | str ])
197+ assert_type (os .environ | a , dict [int | str , int | str ])
197198
198199 assert_type (c | os .environ , dict [str , str ])
199200 assert_type (c | e , dict [str , str ])
0 commit comments