transformers

Форк
0
/
test_check_docstrings.py 
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

15
import inspect
16
import os
17
import sys
18
import unittest
19

20

21
git_repo_path = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
22
sys.path.append(os.path.join(git_repo_path, "utils"))
23

24
from check_docstrings import get_default_description, replace_default_in_arg_description  # noqa: E402
25

26

27
class CheckDostringsTested(unittest.TestCase):
28
    def test_replace_default_in_arg_description(self):
29
        # Standard docstring with default.
30
        desc_with_default = "`float`, *optional*, defaults to 2.0"
31
        self.assertEqual(
32
            replace_default_in_arg_description(desc_with_default, 2.0), "`float`, *optional*, defaults to 2.0"
33
        )
34
        self.assertEqual(
35
            replace_default_in_arg_description(desc_with_default, 1.0), "`float`, *optional*, defaults to 1.0"
36
        )
37
        self.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.
40
        desc_with_default_typo = "`float`, `optional`, defaults to 2.0"
41
        self.assertEqual(
42
            replace_default_in_arg_description(desc_with_default_typo, 2.0), "`float`, *optional*, defaults to 2.0"
43
        )
44
        self.assertEqual(
45
            replace_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.
49
        self.assertEqual(
50
            replace_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.
53
        desc_with_default = "`float`, *optional*, defaults to None"
54
        self.assertEqual(replace_default_in_arg_description(desc_with_default, None), "`float`, *optional*")
55
        desc_with_default = "`float`, *optional*, defaults to `None`"
56
        self.assertEqual(replace_default_in_arg_description(desc_with_default, None), "`float`, *optional*")
57

58
        # Operations are not replaced, but put in backtiks.
59
        desc_with_default = "`float`, *optional*, defaults to 1/255"
60
        self.assertEqual(
61
            replace_default_in_arg_description(desc_with_default, 1 / 255), "`float`, *optional*, defaults to `1/255`"
62
        )
63
        desc_with_default = "`float`, *optional*, defaults to `1/255`"
64
        self.assertEqual(
65
            replace_default_in_arg_description(desc_with_default, 1 / 255), "`float`, *optional*, defaults to `1/255`"
66
        )
67

68
        desc_with_optional = "`float`, *optional*"
69
        self.assertEqual(
70
            replace_default_in_arg_description(desc_with_optional, 2.0), "`float`, *optional*, defaults to 2.0"
71
        )
72
        self.assertEqual(
73
            replace_default_in_arg_description(desc_with_optional, 1.0), "`float`, *optional*, defaults to 1.0"
74
        )
75
        self.assertEqual(replace_default_in_arg_description(desc_with_optional, None), "`float`, *optional*")
76
        self.assertEqual(replace_default_in_arg_description(desc_with_optional, inspect._empty), "`float`")
77

78
        desc_with_no_optional = "`float`"
79
        self.assertEqual(
80
            replace_default_in_arg_description(desc_with_no_optional, 2.0), "`float`, *optional*, defaults to 2.0"
81
        )
82
        self.assertEqual(
83
            replace_default_in_arg_description(desc_with_no_optional, 1.0), "`float`, *optional*, defaults to 1.0"
84
        )
85
        self.assertEqual(replace_default_in_arg_description(desc_with_no_optional, None), "`float`, *optional*")
86
        self.assertEqual(replace_default_in_arg_description(desc_with_no_optional, inspect._empty), "`float`")
87

88
    def test_get_default_description(self):
89
        # Fake function to have arguments to test.
90
        def _fake_function(a, b: int, c=1, d: float = 2.0, e: str = "blob"):
91
            pass
92

93
        params = inspect.signature(_fake_function).parameters
94
        assert get_default_description(params["a"]) == "`<fill_type>`"
95
        assert get_default_description(params["b"]) == "`int`"
96
        assert get_default_description(params["c"]) == "`<fill_type>`, *optional*, defaults to 1"
97
        assert get_default_description(params["d"]) == "`float`, *optional*, defaults to 2.0"
98
        assert get_default_description(params["e"]) == '`str`, *optional*, defaults to `"blob"`'
99

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.