@@ -37,6 +37,28 @@ def assert_single_case_match_block(self, checker, match_type):
3737 "Expr" ,
3838 ])
3939
40+ def assert_function_def_has_one_type_var (self , checker ):
41+ checker .check_children (
42+ "FunctionDef" ,
43+ [
44+ "def" ,
45+ " " ,
46+ "foo" ,
47+ "" ,
48+ "[" ,
49+ "" ,
50+ "TypeVar" ,
51+ "" ,
52+ "]" ,
53+ "" ,
54+ "(" , "" , "arguments" , "" , ")" ,
55+ "" ,
56+ ":" ,
57+ "\n " ,
58+ "Pass" ,
59+ ],
60+ )
61+
4062 def test_operator_support_completeness (self ):
4163 ast_ops = {
4264 n .__name__
@@ -1524,6 +1546,97 @@ def test_type_alias(self):
15241546 "Subscript" ,
15251547 ])
15261548
1549+ def test_type_var_simple (self ):
1550+ source = dedent ("""\
1551+ def foo[S, T](x):
1552+ pass
1553+ """ )
1554+ ast_frag = patchedast .get_patched_ast (source , True )
1555+ checker = _ResultChecker (self , ast_frag )
1556+
1557+ checker .check_children (
1558+ "FunctionDef" ,
1559+ [
1560+ "def" ,
1561+ " " ,
1562+ "foo" ,
1563+ "" ,
1564+ "[" ,
1565+ "" ,
1566+ "TypeVar" ,
1567+ "" ,
1568+ "," ,
1569+ " " ,
1570+ "TypeVar" ,
1571+ "" ,
1572+ "]" ,
1573+ "" ,
1574+ "(" , "" , "arguments" , "" , ")" ,
1575+ "" ,
1576+ ":" ,
1577+ "\n " ,
1578+ "Pass" ,
1579+ ],
1580+ )
1581+
1582+ def test_type_var_with_constraint (self ):
1583+ source = dedent ("""\
1584+ def foo[T = D](x):
1585+ pass
1586+ """ )
1587+ ast_frag = patchedast .get_patched_ast (source , True )
1588+ checker = _ResultChecker (self , ast_frag )
1589+
1590+ self .assert_function_def_has_one_type_var (checker )
1591+
1592+ checker .check_children ("TypeVar" , [
1593+ "T" ,
1594+ " " ,
1595+ "=" ,
1596+ " " ,
1597+ "Name" ,
1598+ ])
1599+
1600+ def test_type_var_with_default_value (self ):
1601+ source = dedent ("""\
1602+ def foo[T: (A, B)](x):
1603+ pass
1604+ """ )
1605+ ast_frag = patchedast .get_patched_ast (source , True )
1606+ checker = _ResultChecker (self , ast_frag )
1607+
1608+ self .assert_function_def_has_one_type_var (checker )
1609+
1610+ checker .check_children ("TypeVar" , [
1611+ "T" ,
1612+ "" ,
1613+ ":" ,
1614+ " " ,
1615+ "Tuple" ,
1616+ ])
1617+
1618+ def test_type_var_with_constraint_and_default_value (self ):
1619+ source = dedent ("""\
1620+ def foo[T: (A, B) = D](x):
1621+ pass
1622+ """ )
1623+ ast_frag = patchedast .get_patched_ast (source , True )
1624+ checker = _ResultChecker (self , ast_frag )
1625+
1626+ self .assert_function_def_has_one_type_var (checker )
1627+
1628+ checker .check_children ("TypeVar" , [
1629+ "T" ,
1630+ "" ,
1631+ ":" ,
1632+ " " ,
1633+ "Tuple" ,
1634+ " " ,
1635+ "=" ,
1636+ " " ,
1637+ "Name" ,
1638+ ])
1639+
15271640
15281641class _ResultChecker :
15291642 def __init__ (self , test_case , ast ):
0 commit comments