|
17 | 17 | importsys |
18 | 18 | importtypes |
19 | 19 | importtextwrap |
| 20 | +fromtypingimportUnpack |
20 | 21 | importunicodedata |
21 | 22 | importunittest |
22 | 23 | importunittest.mock |
|
40 | 41 | fromtest.test_inspectimportinspect_stock_annotations |
41 | 42 | fromtest.test_inspectimportinspect_stringized_annotations |
42 | 43 | fromtest.test_inspectimportinspect_stringized_annotations_2 |
| 44 | +fromtest.test_inspectimportinspect_stringized_annotations_pep695 |
43 | 45 |
|
44 | 46 |
|
45 | 47 | # Functions tested in this suite: |
@@ -1505,6 +1507,107 @@ def wrapper(a, b): |
1505 | 1507 | self.assertEqual(inspect.get_annotations(isa.MyClassWithLocalAnnotations), {'x':'mytype'}) |
1506 | 1508 | self.assertEqual(inspect.get_annotations(isa.MyClassWithLocalAnnotations,eval_str=True), {'x':int}) |
1507 | 1509 |
|
| 1510 | +deftest_pep695_generic_class_with_future_annotations(self): |
| 1511 | +ann_module695=inspect_stringized_annotations_pep695 |
| 1512 | +A_annotations=inspect.get_annotations(ann_module695.A,eval_str=True) |
| 1513 | +A_type_params=ann_module695.A.__type_params__ |
| 1514 | +self.assertIs(A_annotations["x"],A_type_params[0]) |
| 1515 | +self.assertEqual(A_annotations["y"].__args__[0],Unpack[A_type_params[1]]) |
| 1516 | +self.assertIs(A_annotations["z"].__args__[0],A_type_params[2]) |
| 1517 | + |
| 1518 | +deftest_pep695_generic_class_with_future_annotations_and_local_shadowing(self): |
| 1519 | +B_annotations=inspect.get_annotations( |
| 1520 | +inspect_stringized_annotations_pep695.B,eval_str=True |
| 1521 | + ) |
| 1522 | +self.assertEqual(B_annotations, {"x":int,"y":str,"z":bytes}) |
| 1523 | + |
| 1524 | +deftest_pep695_generic_class_with_future_annotations_name_clash_with_global_vars(self): |
| 1525 | +ann_module695=inspect_stringized_annotations_pep695 |
| 1526 | +C_annotations=inspect.get_annotations(ann_module695.C,eval_str=True) |
| 1527 | +self.assertEqual( |
| 1528 | +set(C_annotations.values()), |
| 1529 | +set(ann_module695.C.__type_params__) |
| 1530 | + ) |
| 1531 | + |
| 1532 | +deftest_pep_695_generic_function_with_future_annotations(self): |
| 1533 | +ann_module695=inspect_stringized_annotations_pep695 |
| 1534 | +generic_func_annotations=inspect.get_annotations( |
| 1535 | +ann_module695.generic_function,eval_str=True |
| 1536 | + ) |
| 1537 | +func_t_params=ann_module695.generic_function.__type_params__ |
| 1538 | +self.assertEqual( |
| 1539 | +generic_func_annotations.keys(), {"x","y","z","zz","return"} |
| 1540 | + ) |
| 1541 | +self.assertIs(generic_func_annotations["x"],func_t_params[0]) |
| 1542 | +self.assertEqual(generic_func_annotations["y"],Unpack[func_t_params[1]]) |
| 1543 | +self.assertIs(generic_func_annotations["z"].__origin__,func_t_params[2]) |
| 1544 | +self.assertIs(generic_func_annotations["zz"].__origin__,func_t_params[2]) |
| 1545 | + |
| 1546 | +deftest_pep_695_generic_function_with_future_annotations_name_clash_with_global_vars(self): |
| 1547 | +self.assertEqual( |
| 1548 | +set( |
| 1549 | +inspect.get_annotations( |
| 1550 | +inspect_stringized_annotations_pep695.generic_function_2, |
| 1551 | +eval_str=True |
| 1552 | + ).values() |
| 1553 | + ), |
| 1554 | +set( |
| 1555 | +inspect_stringized_annotations_pep695.generic_function_2.__type_params__ |
| 1556 | + ) |
| 1557 | + ) |
| 1558 | + |
| 1559 | +deftest_pep_695_generic_method_with_future_annotations(self): |
| 1560 | +ann_module695=inspect_stringized_annotations_pep695 |
| 1561 | +generic_method_annotations=inspect.get_annotations( |
| 1562 | +ann_module695.D.generic_method,eval_str=True |
| 1563 | + ) |
| 1564 | +params= { |
| 1565 | +param.__name__:param |
| 1566 | +forparaminann_module695.D.generic_method.__type_params__ |
| 1567 | + } |
| 1568 | +self.assertEqual( |
| 1569 | +generic_method_annotations, |
| 1570 | + {"x":params["Foo"],"y":params["Bar"],"return":None} |
| 1571 | + ) |
| 1572 | + |
| 1573 | +deftest_pep_695_generic_method_with_future_annotations_name_clash_with_global_vars(self): |
| 1574 | +self.assertEqual( |
| 1575 | +set( |
| 1576 | +inspect.get_annotations( |
| 1577 | +inspect_stringized_annotations_pep695.D.generic_method_2, |
| 1578 | +eval_str=True |
| 1579 | + ).values() |
| 1580 | + ), |
| 1581 | +set( |
| 1582 | +inspect_stringized_annotations_pep695.D.generic_method_2.__type_params__ |
| 1583 | + ) |
| 1584 | + ) |
| 1585 | + |
| 1586 | +deftest_pep_695_generics_with_future_annotations_nested_in_function(self): |
| 1587 | +results=inspect_stringized_annotations_pep695.nested() |
| 1588 | + |
| 1589 | +self.assertEqual( |
| 1590 | +set(results.E_annotations.values()), |
| 1591 | +set(results.E.__type_params__) |
| 1592 | + ) |
| 1593 | +self.assertEqual( |
| 1594 | +set(results.E_meth_annotations.values()), |
| 1595 | +set(results.E.generic_method.__type_params__) |
| 1596 | + ) |
| 1597 | +self.assertNotEqual( |
| 1598 | +set(results.E_meth_annotations.values()), |
| 1599 | +set(results.E.__type_params__) |
| 1600 | + ) |
| 1601 | +self.assertEqual( |
| 1602 | +set(results.E_meth_annotations.values()).intersection(results.E.__type_params__), |
| 1603 | +set() |
| 1604 | + ) |
| 1605 | + |
| 1606 | +self.assertEqual( |
| 1607 | +set(results.generic_func_annotations.values()), |
| 1608 | +set(results.generic_func.__type_params__) |
| 1609 | + ) |
| 1610 | + |
1508 | 1611 |
|
1509 | 1612 | classTestFormatAnnotation(unittest.TestCase): |
1510 | 1613 | deftest_typing_replacement(self): |
|