@@ -57,17 +57,18 @@ def pytest_generate_tests(metafunc: pytest.Metafunc) -> None:
57
57
58
58
class_info_set .add (ClassInfo (name = class_name , type = class_value ))
59
59
60
- metafunc .parametrize ("class_info" , sorted (class_info_set ))
60
+ classes = sorted (class_info_set , key = lambda cls : cls .name )
61
+ metafunc .parametrize ("class_info" , classes , ids = [cls .name for cls in classes ])
61
62
62
63
63
64
GET_ID_METHOD_TEMPLATE = """
64
65
def get(
65
- self, id: Union[ str, int] , lazy: bool = False, **kwargs: Any
66
+ self, id: str | int, lazy: bool = False, **kwargs: Any
66
67
) -> {obj_cls.__name__}:
67
68
return cast({obj_cls.__name__}, super().get(id=id, lazy=lazy, **kwargs))
68
69
69
70
You may also need to add the following imports:
70
- from typing import Any, cast, Union "
71
+ from typing import Any, cast"
71
72
"""
72
73
73
74
GET_WITHOUT_ID_METHOD_TEMPLATE = """
@@ -79,58 +80,56 @@ def get(self, **kwargs: Any) -> {obj_cls.__name__}:
79
80
"""
80
81
81
82
82
- class TestTypeHints :
83
- def test_check_get_function_type_hints (self , class_info : ClassInfo ) -> None :
84
- """Ensure classes derived from GetMixin have defined a 'get()' method with
85
- correct type-hints.
86
- """
87
- self .get_check_helper (
88
- base_type = gitlab .mixins .GetMixin ,
89
- class_info = class_info ,
90
- method_template = GET_ID_METHOD_TEMPLATE ,
91
- optional_return = False ,
92
- )
93
-
94
- def test_check_get_without_id_function_type_hints (
95
- self , class_info : ClassInfo
96
- ) -> None :
97
- """Ensure classes derived from GetMixin have defined a 'get()' method with
98
- correct type-hints.
99
- """
100
- self .get_check_helper (
101
- base_type = gitlab .mixins .GetWithoutIdMixin ,
102
- class_info = class_info ,
103
- method_template = GET_WITHOUT_ID_METHOD_TEMPLATE ,
104
- optional_return = False ,
105
- )
106
-
107
- def get_check_helper (
108
- self ,
109
- * ,
110
- base_type : type , # type: ignore[type-arg]
111
- class_info : ClassInfo ,
112
- method_template : str ,
113
- optional_return : bool ,
114
- ) -> None :
115
- if not class_info .name .endswith ("Manager" ):
116
- return
117
- mro = class_info .type .mro ()
118
- # The class needs to be derived from GetMixin or we ignore it
119
- if base_type not in mro :
120
- return
121
-
122
- obj_cls = class_info .type ._obj_cls
123
- signature = inspect .signature (class_info .type .get )
124
- filename = inspect .getfile (class_info .type )
125
-
126
- fail_message = (
127
- f"class definition for { class_info .name !r} in file { filename !r} "
128
- f"must have defined a 'get' method with a return annotation of "
129
- f"{ obj_cls } but found { signature .return_annotation } \n "
130
- f"Recommend adding the following method:\n "
131
- )
132
- fail_message += method_template .format (obj_cls = obj_cls )
133
- check_type = obj_cls
134
- if optional_return :
135
- check_type = Optional [obj_cls ]
136
- assert check_type == signature .return_annotation , fail_message
83
+ def assert_has_correct_return_annotation (
84
+ * ,
85
+ base_type : type , # type: ignore[type-arg]
86
+ class_info : ClassInfo ,
87
+ method_template : str ,
88
+ optional_return : bool ,
89
+ ) -> None :
90
+ if not class_info .name .endswith ("Manager" ):
91
+ return
92
+ mro = class_info .type .mro ()
93
+ # The class needs to be derived from GetMixin or we ignore it
94
+ if base_type not in mro :
95
+ return
96
+
97
+ obj_cls = class_info .type ._obj_cls
98
+ signature = inspect .signature (class_info .type .get )
99
+ filename = inspect .getfile (class_info .type )
100
+
101
+ fail_message = (
102
+ f"class definition for { class_info .name !r} in file { filename !r} "
103
+ f"must have defined a 'get' method with a return annotation of "
104
+ f"{ obj_cls } but found { signature .return_annotation } \n "
105
+ f"Recommend adding the following method:\n "
106
+ )
107
+ fail_message += method_template .format (obj_cls = obj_cls )
108
+ check_type = obj_cls
109
+ if optional_return :
110
+ check_type = Optional [obj_cls ]
111
+ assert check_type == signature .return_annotation , fail_message
112
+
113
+
114
+ def test_get_mixin_return_annotation (class_info : ClassInfo ) -> None :
115
+ """Ensure classes derived from GetMixin have defined a 'get()' method with
116
+ correct type-hints.
117
+ """
118
+ assert_has_correct_return_annotation (
119
+ base_type = gitlab .mixins .GetMixin ,
120
+ class_info = class_info ,
121
+ method_template = GET_ID_METHOD_TEMPLATE ,
122
+ optional_return = False ,
123
+ )
124
+
125
+
126
+ def test_get_without_id_mixin_return_annotation (class_info : ClassInfo ) -> None :
127
+ """Ensure classes derived from GetMixin have defined a 'get()' method with
128
+ correct type-hints.
129
+ """
130
+ assert_has_correct_return_annotation (
131
+ base_type = gitlab .mixins .GetWithoutIdMixin ,
132
+ class_info = class_info ,
133
+ method_template = GET_WITHOUT_ID_METHOD_TEMPLATE ,
134
+ optional_return = False ,
135
+ )
0 commit comments