4242from typing import Self , LiteralString
4343from typing import TypeAlias
4444from typing import ParamSpec , Concatenate , ParamSpecArgs , ParamSpecKwargs
45- from typing import TypeGuard , TypeIs , NoDefault
45+ from typing import TypeForm , TypeGuard , TypeIs , NoDefault
4646import abc
4747import textwrap
4848import typing
@@ -5890,6 +5890,7 @@ def test_subclass_special_form(self):
58905890 Final [int ],
58915891 Literal [1 , 2 ],
58925892 Concatenate [int , ParamSpec ("P" )],
5893+ TypeForm [int ],
58935894 TypeGuard [int ],
58945895 TypeIs [range ],
58955896 ):
@@ -7358,6 +7359,7 @@ class C(Generic[T]): pass
73587359 self .assertEqual (get_args (Required [int ]), (int ,))
73597360 self .assertEqual (get_args (NotRequired [int ]), (int ,))
73607361 self .assertEqual (get_args (TypeAlias ), ())
7362+ self .assertEqual (get_args (TypeForm [int ]), (int ,))
73617363 self .assertEqual (get_args (TypeGuard [int ]), (int ,))
73627364 self .assertEqual (get_args (TypeIs [range ]), (range ,))
73637365 Ts = TypeVarTuple ('Ts' )
@@ -10646,6 +10648,72 @@ def test_no_isinstance(self):
1064610648 issubclass (int , TypeIs )
1064710649
1064810650
10651+ class TypeFormTests (BaseTestCase ):
10652+ def test_basics (self ):
10653+ TypeForm [int ] # OK
10654+ self .assertEqual (TypeForm [int ], TypeForm [int ])
10655+
10656+ def foo (arg ) -> TypeForm [int ]: ...
10657+ self .assertEqual (gth (foo ), {'return' : TypeForm [int ]})
10658+
10659+ with self .assertRaises (TypeError ):
10660+ TypeForm [int , str ]
10661+
10662+ def test_repr (self ):
10663+ self .assertEqual (repr (TypeForm ), 'typing.TypeForm' )
10664+ cv = TypeForm [int ]
10665+ self .assertEqual (repr (cv ), 'typing.TypeForm[int]' )
10666+ cv = TypeForm [Employee ]
10667+ self .assertEqual (repr (cv ), 'typing.TypeForm[%s.Employee]' % __name__ )
10668+ cv = TypeForm [tuple [int ]]
10669+ self .assertEqual (repr (cv ), 'typing.TypeForm[tuple[int]]' )
10670+
10671+ def test_cannot_subclass (self ):
10672+ with self .assertRaisesRegex (TypeError , CANNOT_SUBCLASS_TYPE ):
10673+ class C (type (TypeForm )):
10674+ pass
10675+ with self .assertRaisesRegex (TypeError , CANNOT_SUBCLASS_TYPE ):
10676+ class D (type (TypeForm [int ])):
10677+ pass
10678+ with self .assertRaisesRegex (TypeError ,
10679+ r'Cannot subclass typing\.TypeForm' ):
10680+ class E (TypeForm ):
10681+ pass
10682+ with self .assertRaisesRegex (TypeError ,
10683+ r'Cannot subclass typing\.TypeForm\[int\]' ):
10684+ class F (TypeForm [int ]):
10685+ pass
10686+
10687+ def test_call (self ):
10688+ objs = [
10689+ 1 ,
10690+ "int" ,
10691+ int ,
10692+ tuple [int , str ],
10693+ Tuple [int , str ],
10694+ ]
10695+ for obj in objs :
10696+ with self .subTest (obj = obj ):
10697+ self .assertIs (TypeForm (obj ), obj )
10698+
10699+ with self .assertRaises (TypeError ):
10700+ TypeForm ()
10701+ with self .assertRaises (TypeError ):
10702+ TypeForm ("too" , "many" )
10703+
10704+ def test_cannot_init_type (self ):
10705+ with self .assertRaises (TypeError ):
10706+ type (TypeForm )()
10707+ with self .assertRaises (TypeError ):
10708+ type (TypeForm [Optional [int ]])()
10709+
10710+ def test_no_isinstance (self ):
10711+ with self .assertRaises (TypeError ):
10712+ isinstance (1 , TypeForm [int ])
10713+ with self .assertRaises (TypeError ):
10714+ issubclass (int , TypeForm )
10715+
10716+
1064910717SpecialAttrsP = typing .ParamSpec ('SpecialAttrsP' )
1065010718SpecialAttrsT = typing .TypeVar ('SpecialAttrsT' , int , float , complex )
1065110719
@@ -10747,6 +10815,7 @@ def test_special_attrs(self):
1074710815 typing .Never : 'Never' ,
1074810816 typing .Optional : 'Optional' ,
1074910817 typing .TypeAlias : 'TypeAlias' ,
10818+ typing .TypeForm : 'TypeForm' ,
1075010819 typing .TypeGuard : 'TypeGuard' ,
1075110820 typing .TypeIs : 'TypeIs' ,
1075210821 typing .TypeVar : 'TypeVar' ,
@@ -10761,6 +10830,7 @@ def test_special_attrs(self):
1076110830 typing .Literal [1 , 2 ]: 'Literal' ,
1076210831 typing .Literal [True , 2 ]: 'Literal' ,
1076310832 typing .Optional [Any ]: 'Union' ,
10833+ typing .TypeForm [Any ]: 'TypeForm' ,
1076410834 typing .TypeGuard [Any ]: 'TypeGuard' ,
1076510835 typing .TypeIs [Any ]: 'TypeIs' ,
1076610836 typing .Union [Any ]: 'Any' ,
0 commit comments