Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitdcc7def

Browse files
committed
chore(meta): adapt type hint tests (wip)
1 parentabc2b9d commitdcc7def

File tree

1 file changed

+57
-58
lines changed

1 file changed

+57
-58
lines changed

‎tests/unit/meta/test_ensure_type_hints.py

Lines changed: 57 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,18 @@ def pytest_generate_tests(metafunc: pytest.Metafunc) -> None:
5757

5858
class_info_set.add(ClassInfo(name=class_name,type=class_value))
5959

60-
metafunc.parametrize("class_info",sorted(class_info_set))
60+
classes=sorted(class_info_set,key=lambdacls:cls.name)
61+
metafunc.parametrize("class_info",classes,ids=[cls.nameforclsinclasses])
6162

6263

6364
GET_ID_METHOD_TEMPLATE="""
6465
def get(
65-
self, id:Union[str,int], lazy: bool = False, **kwargs: Any
66+
self, id: str |int, lazy: bool = False, **kwargs: Any
6667
) -> {obj_cls.__name__}:
6768
return cast({obj_cls.__name__}, super().get(id=id, lazy=lazy, **kwargs))
6869
6970
You may also need to add the following imports:
70-
from typing import Any, cast, Union"
71+
from typing import Any, cast"
7172
"""
7273

7374
GET_WITHOUT_ID_METHOD_TEMPLATE="""
@@ -79,58 +80,56 @@ def get(self, **kwargs: Any) -> {obj_cls.__name__}:
7980
"""
8081

8182

82-
classTestTypeHints:
83-
deftest_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-
deftest_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-
defget_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-
ifnotclass_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-
ifbase_typenotinmro:
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-
ifoptional_return:
135-
check_type=Optional[obj_cls]
136-
assertcheck_type==signature.return_annotation,fail_message
83+
defassert_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+
ifnotclass_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+
ifbase_typenotinmro:
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+
ifoptional_return:
110+
check_type=Optional[obj_cls]
111+
assertcheck_type==signature.return_annotation,fail_message
112+
113+
114+
deftest_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+
deftest_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

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp