bpo-44490: Improve typing module compatibility with types.Union (GH-27048) (#27222)

(cherry picked from commit bf89ff96e6ba21bb52b8597b5e51e8ffc57e6589)

Co-authored-by: Yurii Karabas <1998uriyyo@gmail.com>
diff --git a/Lib/test/ann_module.py b/Lib/test/ann_module.py
index 0567d6d..5081e6b 100644
--- a/Lib/test/ann_module.py
+++ b/Lib/test/ann_module.py
@@ -58,3 +58,5 @@ def dec(func):
     def wrapper(*args, **kwargs):
         return func(*args, **kwargs)
     return wrapper
+
+u: int | float
diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py
index c0820fd..b6c4574 100644
--- a/Lib/test/test_grammar.py
+++ b/Lib/test/test_grammar.py
@@ -473,7 +473,7 @@ class CC(metaclass=CMeta):
     def test_var_annot_module_semantics(self):
         self.assertEqual(test.__annotations__, {})
         self.assertEqual(ann_module.__annotations__,
-                     {1: 2, 'x': int, 'y': str, 'f': typing.Tuple[int, int]})
+                     {1: 2, 'x': int, 'y': str, 'f': typing.Tuple[int, int], 'u': int | float})
         self.assertEqual(ann_module.M.__annotations__,
                               {'123': 123, 'o': type})
         self.assertEqual(ann_module2.__annotations__, {})
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index 0c72784..5602150 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -315,6 +315,8 @@ def test_repr(self):
         self.assertEqual(repr(u), 'typing.Union[typing.List[int], int]')
         u = Union[list[int], dict[str, float]]
         self.assertEqual(repr(u), 'typing.Union[list[int], dict[str, float]]')
+        u = Union[int | float]
+        self.assertEqual(repr(u), 'typing.Union[int, float]')
 
     def test_cannot_subclass(self):
         with self.assertRaises(TypeError):
@@ -1449,6 +1451,8 @@ def test_basics(self):
         with self.assertRaises(TypeError):
             issubclass(SM1, SimpleMapping)
         self.assertIsInstance(SM1(), SimpleMapping)
+        T = TypeVar("T")
+        self.assertEqual(List[list[T] | float].__parameters__, (T,))
 
     def test_generic_errors(self):
         T = TypeVar('T')
@@ -1785,6 +1789,7 @@ def test_extended_generic_rules_repr(self):
     def test_generic_forward_ref(self):
         def foobar(x: List[List['CC']]): ...
         def foobar2(x: list[list[ForwardRef('CC')]]): ...
+        def foobar3(x: list[ForwardRef('CC | int')] | int): ...
         class CC: ...
         self.assertEqual(
             get_type_hints(foobar, globals(), locals()),
@@ -1794,6 +1799,10 @@ class CC: ...
             get_type_hints(foobar2, globals(), locals()),
             {'x': list[list[CC]]}
         )
+        self.assertEqual(
+            get_type_hints(foobar3, globals(), locals()),
+            {'x': list[CC | int] | int}
+        )
 
         T = TypeVar('T')
         AT = Tuple[T, ...]
@@ -2467,6 +2476,12 @@ def foo(a: Union['T']):
         self.assertEqual(get_type_hints(foo, globals(), locals()),
                          {'a': Union[T]})
 
+        def foo(a: tuple[ForwardRef('T')] | int):
+            pass
+
+        self.assertEqual(get_type_hints(foo, globals(), locals()),
+                         {'a': tuple[T] | int})
+
     def test_tuple_forward(self):
 
         def foo(a: Tuple['T']):
@@ -2851,7 +2866,7 @@ def test_get_type_hints_from_various_objects(self):
             gth(None)
 
     def test_get_type_hints_modules(self):
-        ann_module_type_hints = {1: 2, 'f': Tuple[int, int], 'x': int, 'y': str}
+        ann_module_type_hints = {1: 2, 'f': Tuple[int, int], 'x': int, 'y': str, 'u': int | float}
         self.assertEqual(gth(ann_module), ann_module_type_hints)
         self.assertEqual(gth(ann_module2), {})
         self.assertEqual(gth(ann_module3), {})
@@ -4393,6 +4408,9 @@ def test_no_paramspec_in__parameters__(self):
         self.assertNotIn(P, list[P].__parameters__)
         self.assertIn(T, tuple[T, P].__parameters__)
 
+        self.assertNotIn(P, (list[P] | int).__parameters__)
+        self.assertIn(T, (tuple[T, P] | int).__parameters__)
+
     def test_paramspec_in_nested_generics(self):
         # Although ParamSpec should not be found in __parameters__ of most
         # generics, they probably should be found when nested in
@@ -4402,8 +4420,10 @@ def test_paramspec_in_nested_generics(self):
         C1 = Callable[P, T]
         G1 = List[C1]
         G2 = list[C1]
+        G3 = list[C1] | int
         self.assertEqual(G1.__parameters__, (P, T))
         self.assertEqual(G2.__parameters__, (P, T))
+        self.assertEqual(G3.__parameters__, (P, T))
 
 
 class ConcatenateTests(BaseTestCase):