bpo-45166: fixes `get_type_hints` failure on `Final` (GH-28279) (GH-28560)

Co-authored-by: Ɓukasz Langa <lukasz@langa.pl>
Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com>
(cherry picked from commit 784905dbeff68cf788bbeefe0a675af1af04affc)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
diff --git a/Lib/test/ann_module5.py b/Lib/test/ann_module5.py
new file mode 100644
index 0000000..837041e
--- /dev/null
+++ b/Lib/test/ann_module5.py
@@ -0,0 +1,10 @@
+# Used by test_typing to verify that Final wrapped in ForwardRef works.
+
+from __future__ import annotations
+
+from typing import Final
+
+name: Final[str] = "final"
+
+class MyClass:
+    value: Final = 3000
diff --git a/Lib/test/ann_module6.py b/Lib/test/ann_module6.py
new file mode 100644
index 0000000..6791756
--- /dev/null
+++ b/Lib/test/ann_module6.py
@@ -0,0 +1,7 @@
+# Tests that top-level ClassVar is not allowed
+
+from __future__ import annotations
+
+from typing import ClassVar
+
+wrong: ClassVar[int] = 1
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index c84ff0f..4f250f2 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -2969,7 +2969,7 @@ async def __aexit__(self, etype, eval, tb):
 
 # Definitions needed for features introduced in Python 3.6
 
-from test import ann_module, ann_module2, ann_module3
+from test import ann_module, ann_module2, ann_module3, ann_module5, ann_module6
 from typing import AsyncContextManager
 
 class A:
@@ -3333,6 +3333,22 @@ class C(Generic[T]): pass
                          (Concatenate[int, P], int))
         self.assertEqual(get_args(list | str), (list, str))
 
+    def test_forward_ref_and_final(self):
+        # https://bugs.python.org/issue45166
+        hints = get_type_hints(ann_module5)
+        self.assertEqual(hints, {'name': Final[str]})
+
+        hints = get_type_hints(ann_module5.MyClass)
+        self.assertEqual(hints, {'value': Final})
+
+    def test_top_level_class_var(self):
+        # https://bugs.python.org/issue45166
+        with self.assertRaisesRegex(
+            TypeError,
+            r'typing.ClassVar\[int\] is not valid as type argument',
+        ):
+            get_type_hints(ann_module6)
+
 
 class CollectionsAbcTests(BaseTestCase):