transformers
98 строк · 4.9 Кб
1# Copyright 2023 The HuggingFace Team. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import inspect16import os17import sys18import unittest19
20
21git_repo_path = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))22sys.path.append(os.path.join(git_repo_path, "utils"))23
24from check_docstrings import get_default_description, replace_default_in_arg_description # noqa: E40225
26
27class CheckDostringsTested(unittest.TestCase):28def test_replace_default_in_arg_description(self):29# Standard docstring with default.30desc_with_default = "`float`, *optional*, defaults to 2.0"31self.assertEqual(32replace_default_in_arg_description(desc_with_default, 2.0), "`float`, *optional*, defaults to 2.0"33)34self.assertEqual(35replace_default_in_arg_description(desc_with_default, 1.0), "`float`, *optional*, defaults to 1.0"36)37self.assertEqual(replace_default_in_arg_description(desc_with_default, inspect._empty), "`float`")38
39# Standard docstring with default but optional is not using the stars.40desc_with_default_typo = "`float`, `optional`, defaults to 2.0"41self.assertEqual(42replace_default_in_arg_description(desc_with_default_typo, 2.0), "`float`, *optional*, defaults to 2.0"43)44self.assertEqual(45replace_default_in_arg_description(desc_with_default_typo, 1.0), "`float`, *optional*, defaults to 1.0"46)47
48# If the default is None we do not erase the value in the docstring.49self.assertEqual(50replace_default_in_arg_description(desc_with_default, None), "`float`, *optional*, defaults to 2.0"51)52# If the default is None (and set as such in the docstring), we do not include it.53desc_with_default = "`float`, *optional*, defaults to None"54self.assertEqual(replace_default_in_arg_description(desc_with_default, None), "`float`, *optional*")55desc_with_default = "`float`, *optional*, defaults to `None`"56self.assertEqual(replace_default_in_arg_description(desc_with_default, None), "`float`, *optional*")57
58# Operations are not replaced, but put in backtiks.59desc_with_default = "`float`, *optional*, defaults to 1/255"60self.assertEqual(61replace_default_in_arg_description(desc_with_default, 1 / 255), "`float`, *optional*, defaults to `1/255`"62)63desc_with_default = "`float`, *optional*, defaults to `1/255`"64self.assertEqual(65replace_default_in_arg_description(desc_with_default, 1 / 255), "`float`, *optional*, defaults to `1/255`"66)67
68desc_with_optional = "`float`, *optional*"69self.assertEqual(70replace_default_in_arg_description(desc_with_optional, 2.0), "`float`, *optional*, defaults to 2.0"71)72self.assertEqual(73replace_default_in_arg_description(desc_with_optional, 1.0), "`float`, *optional*, defaults to 1.0"74)75self.assertEqual(replace_default_in_arg_description(desc_with_optional, None), "`float`, *optional*")76self.assertEqual(replace_default_in_arg_description(desc_with_optional, inspect._empty), "`float`")77
78desc_with_no_optional = "`float`"79self.assertEqual(80replace_default_in_arg_description(desc_with_no_optional, 2.0), "`float`, *optional*, defaults to 2.0"81)82self.assertEqual(83replace_default_in_arg_description(desc_with_no_optional, 1.0), "`float`, *optional*, defaults to 1.0"84)85self.assertEqual(replace_default_in_arg_description(desc_with_no_optional, None), "`float`, *optional*")86self.assertEqual(replace_default_in_arg_description(desc_with_no_optional, inspect._empty), "`float`")87
88def test_get_default_description(self):89# Fake function to have arguments to test.90def _fake_function(a, b: int, c=1, d: float = 2.0, e: str = "blob"):91pass92
93params = inspect.signature(_fake_function).parameters94assert get_default_description(params["a"]) == "`<fill_type>`"95assert get_default_description(params["b"]) == "`int`"96assert get_default_description(params["c"]) == "`<fill_type>`, *optional*, defaults to 1"97assert get_default_description(params["d"]) == "`float`, *optional*, defaults to 2.0"98assert get_default_description(params["e"]) == '`str`, *optional*, defaults to `"blob"`'99