Update type related modules and tests from CPython

This commit is contained in:
CPython Developers
2022-08-09 05:13:33 +09:00
committed by Jeong YunWon
parent 15fdf0cf8b
commit 75eebc49ba
3 changed files with 28 additions and 5 deletions

View File

@@ -1,6 +1,7 @@
import ast
import sys
import unittest
from test import support
funcdef = """\
@@ -422,7 +423,7 @@ class TypeCommentTests(unittest.TestCase):
arg = tree.argtypes[0]
self.assertEqual(arg.id, "int")
self.assertEqual(tree.returns.value.id, "List")
self.assertEqual(tree.returns.slice.value.id, "str")
self.assertEqual(tree.returns.slice.id, "str")
tree = parse_func_type_input("(int, *str, **Any) -> float")
self.assertEqual(tree.argtypes[0].id, "int")
@@ -430,6 +431,14 @@ class TypeCommentTests(unittest.TestCase):
self.assertEqual(tree.argtypes[2].id, "Any")
self.assertEqual(tree.returns.id, "float")
tree = parse_func_type_input("(*int) -> None")
self.assertEqual(tree.argtypes[0].id, "int")
tree = parse_func_type_input("(**int) -> None")
self.assertEqual(tree.argtypes[0].id, "int")
tree = parse_func_type_input("(*int, **str) -> None")
self.assertEqual(tree.argtypes[0].id, "int")
self.assertEqual(tree.argtypes[1].id, "str")
with self.assertRaises(SyntaxError):
tree = parse_func_type_input("(int, *str, *Any) -> float")

View File

@@ -1302,6 +1302,17 @@ class ClassCreationTests(unittest.TestCase):
self.assertEqual(D.__orig_bases__, (c,))
self.assertEqual(D.__mro__, (D, A, object))
def test_new_class_with_mro_entry_genericalias(self):
L1 = types.new_class('L1', (typing.List[int],), {})
self.assertEqual(L1.__bases__, (list, typing.Generic))
self.assertEqual(L1.__orig_bases__, (typing.List[int],))
self.assertEqual(L1.__mro__, (L1, list, typing.Generic, object))
L2 = types.new_class('L2', (list[int],), {})
self.assertEqual(L2.__bases__, (list,))
self.assertEqual(L2.__orig_bases__, (list[int],))
self.assertEqual(L2.__mro__, (L2, list, object))
def test_new_class_with_mro_entry_none(self):
class A: pass
class B: pass
@@ -1417,6 +1428,11 @@ class ClassCreationTests(unittest.TestCase):
for bases in [x, y, z, t]:
self.assertIs(types.resolve_bases(bases), bases)
def test_resolve_bases_with_mro_entry(self):
self.assertEqual(types.resolve_bases((typing.List[int],)),
(list, typing.Generic))
self.assertEqual(types.resolve_bases((list[int],)), (list,))
def test_metaclass_derivation(self):
# issue1294232: correct metaclass calculation
new_calls = [] # to check the order of __new__ calls

6
Lib/types.py vendored
View File

@@ -82,7 +82,7 @@ def resolve_bases(bases):
updated = False
shift = 0
for i, base in enumerate(bases):
if isinstance(base, type):
if isinstance(base, type) and not isinstance(base, GenericAlias):
continue
if not hasattr(base, "__mro_entries__"):
continue
@@ -282,9 +282,7 @@ def coroutine(func):
@functools.wraps(func)
def wrapped(*args, **kwargs):
coro = func(*args, **kwargs)
if (# XXX RUSTPYTHON TODO: iterable coroutine
False and
coro.__class__ is CoroutineType or
if (coro.__class__ is CoroutineType or
coro.__class__ is GeneratorType and coro.gi_code.co_flags & 0x100):
# 'coro' is a native coroutine object or an iterable coroutine
return coro