1cimport cython 2 3@cython.test_assert_path_exists( 4 '//PythonCapiCallNode/PythonCapiFunctionNode[@cname="Py_TYPE"]') 5def get_type_of(a): 6 """ 7 >>> get_type_of(object()) is object 8 True 9 """ 10 return type(a) 11 12@cython.test_assert_path_exists( 13 '//PythonCapiCallNode/PythonCapiFunctionNode[@cname="Py_TYPE"]') 14def get_type_through_local(a): 15 """ 16 >>> get_type_of(object()) is object 17 True 18 """ 19 t = type(a) 20 return t 21 22@cython.test_assert_path_exists( 23 '//PythonCapiCallNode/PythonCapiFunctionNode[@cname="Py_TYPE"]') 24@cython.test_fail_if_path_exists( 25 '//PythonCapiCallNode/PythonCapiFunctionNode[@cname="__Pyx_Type"]', 26 '//NameNode[@name="type"]') 27def test_type(a, t): 28 """ 29 >>> test_type(object(), object) 30 True 31 """ 32 return type(a) and type(a) is t and type(a) == t 33 34@cython.test_assert_path_exists('//NameNode[@name="type"]') 35def type_type(): 36 """ 37 >>> type_type()(object()) is object 38 True 39 """ 40 return type 41 42cpdef type pass_type(type x): 43 """ 44 >>> pass_type(int) == int 45 True 46 >>> class MyType(object): pass 47 >>> pass_type(MyType) == MyType 48 True 49 >>> pass_type(object()) 50 Traceback (most recent call last): 51 TypeError: Argument 'x' has incorrect type (expected type, got object) 52 """ 53 return x 54