forked from PaddlePaddle/Paddle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_check_api_compatible.py
149 lines (119 loc) · 8.74 KB
/
test_check_api_compatible.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#! /usr/bin/env python
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
TestCases for check_api_compatible.py
"""
import unittest
import sys
import os
import tempfile
import inspect
from check_api_compatible import read_argspec_from_file
from check_api_compatible import check_compatible
from check_api_compatible import check_compatible_str
class Test_check_compatible(unittest.TestCase):
def setUp(self) -> None:
self.fullargspec_prefix = 'inspect.Full'
self.argspec_str_o = self.fullargspec_prefix + '''ArgSpec(args=['shape', 'dtype', 'name'], varargs=None, varkw=None, defaults=(None, None), kwonlyargs=[], kwonlydefaults=None, annotations={})'''
return super().setUp()
def test_normal_not_changed(self):
argspec_o = eval(self.argspec_str_o)
argspec_n = eval(self.argspec_str_o)
self.assertTrue(check_compatible(argspec_o, argspec_n))
def test_args_added(self):
argspec_str_n = '''ArgSpec(args=['shape', 'dtype', 'name', 'arg4'], varargs=None, varkw=None, defaults=(None, None), kwonlyargs=[], kwonlydefaults=None, annotations={})'''
argspec_o = eval(self.argspec_str_o)
argspec_n = eval(self.fullargspec_prefix + argspec_str_n)
self.assertFalse(check_compatible(argspec_o, argspec_n))
argspec_str_n = '''ArgSpec(args=['shape', 'dtype', 'name', 'arg4'], varargs=None, varkw=None, defaults=(None, None, 1), kwonlyargs=[], kwonlydefaults=None, annotations={})'''
argspec_n = eval(self.fullargspec_prefix + argspec_str_n)
self.assertTrue(check_compatible(argspec_o, argspec_n))
argspec_str_n = '''ArgSpec(args=['shape', 'dtype', 'name', 'arg4'], varargs=None, varkw=None, defaults=(None, None, 1, True), kwonlyargs=[], kwonlydefaults=None, annotations={})'''
argspec_n = eval(self.fullargspec_prefix + argspec_str_n)
self.assertFalse(check_compatible(argspec_o, argspec_n))
argspec_str_n = '''ArgSpec(args=['shape', 'dtype', 'name', 'arg4'], varargs=None, varkw=None, defaults=(True, None, None, 1), kwonlyargs=[], kwonlydefaults=None, annotations={})'''
argspec_n = eval(self.fullargspec_prefix + argspec_str_n)
self.assertTrue(check_compatible(argspec_o, argspec_n))
def test_args_places_exchanged(self):
argspec_str_n = '''ArgSpec(args=['shape', 'name', 'dtype'], varargs=None, varkw=None, defaults=(None, None), kwonlyargs=[], kwonlydefaults=None, annotations={})'''
argspec_o = eval(self.argspec_str_o)
argspec_n = eval(self.fullargspec_prefix + argspec_str_n)
self.assertFalse(check_compatible(argspec_o, argspec_n))
def test_args_reduced(self):
argspec_str_n = '''ArgSpec(args=['shape', 'name'], varargs=None, varkw=None, defaults=(None,), kwonlyargs=[], kwonlydefaults=None, annotations={})'''
argspec_o = eval(self.argspec_str_o)
argspec_n = eval(self.fullargspec_prefix + argspec_str_n)
self.assertFalse(check_compatible(argspec_o, argspec_n))
class Test_check_compatible_str(unittest.TestCase):
def setUp(self) -> None:
self.fullargspec_prefix = 'inspect.Full'
# paddle.fluid.layer_helper_base.LayerHelperBase.create_parameter
self.argspec_str_o = self.fullargspec_prefix + """ArgSpec(args=['self', 'attr', 'shape', 'dtype', 'is_bias', 'default_initializer', 'stop_gradient', 'type'], varargs=None, varkw=None, defaults=(None, False, None, False, VarType.LOD_TENSOR), kwonlyargs=[], kwonlydefaults=None, annotations={})"""
return super().setUp()
def test_normal_not_changed(self):
argspec_o = self.argspec_str_o
argspec_n = self.argspec_str_o
self.assertTrue(check_compatible_str(argspec_o, argspec_n))
def test_args_added(self):
argspec_n = self.fullargspec_prefix + """ArgSpec(args=['self', 'attr', 'shape', 'dtype', 'is_bias', 'default_initializer', 'stop_gradient', 'type', 'argadded'], varargs=None, varkw=None, defaults=(None, False, None, False, VarType.LOD_TENSOR), kwonlyargs=[], kwonlydefaults=None, annotations={})"""
argspec_o = self.argspec_str_o
self.assertFalse(check_compatible_str(argspec_o, argspec_n))
argspec_n = self.fullargspec_prefix + """ArgSpec(args=['self', 'attr', 'shape', 'dtype', 'is_bias', 'default_initializer', 'stop_gradient', 'type', 'argadded'], varargs=None, varkw=None, defaults=(None, False, None, False, VarType.LOD_TENSOR, argadded), kwonlyargs=[], kwonlydefaults=None, annotations={})"""
self.assertTrue(check_compatible_str(argspec_o, argspec_n))
argspec_n = self.fullargspec_prefix + """ArgSpec(args=['self', 'attr', 'shape', 'dtype', 'is_bias', 'default_initializer', 'stop_gradient', 'type', 'argadded'], varargs=None, varkw=None, defaults=(None, False, None, False, VarType.LOD_TENSOR, argadded, 1), kwonlyargs=[], kwonlydefaults=None, annotations={})"""
self.assertFalse(check_compatible_str(argspec_o, argspec_n))
argspec_n = self.fullargspec_prefix + """ArgSpec(args=['self', 'attr', 'shape', 'dtype', 'is_bias', 'default_initializer', 'stop_gradient', 'type', 'argadded'], varargs=None, varkw=None, defaults=(1, None, False, None, False, VarType.LOD_TENSOR, argadded), kwonlyargs=[], kwonlydefaults=None, annotations={})"""
self.assertTrue(check_compatible_str(argspec_o, argspec_n))
def test_args_places_exchanged(self):
argspec_n = self.fullargspec_prefix + """ArgSpec(args=['self', 'attr', 'shape', 'dtype', 'is_bias', 'default_initializer', 'type', 'stop_gradient'], varargs=None, varkw=None, defaults=(None, False, None, False, VarType.LOD_TENSOR), kwonlyargs=[], kwonlydefaults=None, annotations={})"""
argspec_o = self.argspec_str_o
self.assertFalse(check_compatible_str(argspec_o, argspec_n))
def test_args_reduced(self):
argspec_n = self.fullargspec_prefix + """ArgSpec(args=['self', 'attr', 'shape', 'dtype', 'is_bias', 'default_initializer', 'stop_gradient'], varargs=None, varkw=None, defaults=(None, False, None, False, VarType.LOD_TENSOR), kwonlyargs=[], kwonlydefaults=None, annotations={})"""
argspec_o = self.argspec_str_o
self.assertFalse(check_compatible_str(argspec_o, argspec_n))
def test_args_defaults_None(self):
argspec_o = """inspect.FullArgSpec(args=['filename'], varargs=None, varkw=None, defaults=None, kwonlyargs=[], kwonlydefaults=None, annotations={'filename': <class 'str'>})"""
argspec_n = """inspect.FullArgSpec(args=['filename'], varargs=None, varkw=None, defaults=None, kwonlyargs=[], kwonlydefaults=None, annotations={'filename': <class 'str'>})"""
self.assertTrue(check_compatible_str(argspec_o, argspec_n))
class Test_read_argspec_from_file(unittest.TestCase):
def setUp(self) -> None:
self.fullargspec_prefix = 'inspect.Full'
self.argspec_str_o = self.fullargspec_prefix + '''ArgSpec(args=['shape', 'dtype', 'name'], varargs=None, varkw=None, defaults=(None, None), kwonlyargs=[], kwonlydefaults=None, annotations={})'''
self.api_spec_file = tempfile.TemporaryFile('w+t')
if self.api_spec_file:
self.api_spec_file.write("\n".join([
"""paddle.ones (ArgSpec(args=['shape', 'dtype', 'name'], varargs=None, varkw=None, defaults=(None, None), kwonlyargs=[], kwonlydefaults=None, annotations={}), ('document', '50a3b3a77fa13bb2ae4337d8f9d091b7'))""",
# """paddle.four_plus_four (paddle.four_plus_four, ('document', 'ff0f188c95030158cc6398d2a6c5four'))""",
"""paddle.five_plus_five (ArgSpec(), ('document', 'ff0f188c95030158cc6398d2a6c5five'))""",
]))
self.api_spec_file.seek(0)
return super().setUp()
def tearDown(self):
if self.api_spec_file:
self.api_spec_file.close()
def test_case_normal(self):
if self.api_spec_file:
api_argspec_dict = read_argspec_from_file(self.api_spec_file)
argspec = eval(self.argspec_str_o)
self.assertEqual(
api_argspec_dict.get('paddle.ones').args, argspec.args)
self.assertEqual(
api_argspec_dict.get('paddle.ones').defaults, argspec.defaults)
self.assertIsNone(api_argspec_dict.get('paddle.five_plus_five'))
else:
self.fail('api_spec_file error')
if __name__ == '__main__':
unittest.main()