6
6
ClassDefData ,
7
7
DefinitionFinder ,
8
8
FunctionDefData ,
9
+ ImportFromData ,
9
10
)
10
11
from nb_autodoc .analyzers .utils import ast_parse
11
12
12
13
13
14
def get_analyzer_data (filename : str ) -> str :
14
- return open (Path (__file__ ).parent / "analyzerdata" / filename ).read ()
15
+ return open (Path (__file__ ).parent . parent / "analyzerdata" / filename ).read ()
15
16
16
17
17
18
class TestDefinitionFinder :
18
19
def test_simple_data (self ):
19
20
code = get_analyzer_data ("simple-definition-ast.py" )
20
21
module = ast_parse (code )
21
- visitor = DefinitionFinder ("mypkg.pkg" , "mypkg.pkg.pkg" )
22
+ visitor = DefinitionFinder (package = "mypkg.pkg.pkg" )
22
23
visitor .visit (module )
23
24
del code , module
24
25
module_freevars = visitor .freevars
25
26
# duplicated from repr(module_freevars)
26
27
# notice that AssignData.annotation is uncomparable `ast.Expression`
27
28
# so until unparser implement (maybe py3.8), annotation test is unavailable
28
29
assert module_freevars == {
30
+ "Path_rename" : ImportFromData (
31
+ order = 0 , name = "Path_rename" , module = "pathlib" , orig_name = "Path"
32
+ ),
33
+ "ext_A_rename" : ImportFromData (
34
+ order = 1 , name = "ext_A_rename" , module = "mypkg" , orig_name = "ext_A"
35
+ ),
36
+ "ext_fa" : ImportFromData (
37
+ order = 2 , name = "ext_fa" , module = "mypkg" , orig_name = "ext_fa"
38
+ ),
39
+ "ext_B" : ImportFromData (
40
+ order = 3 , name = "ext_B" , module = "mypkg.pkg" , orig_name = "ext_B"
41
+ ),
42
+ "ext_fb" : ImportFromData (
43
+ order = 4 , name = "ext_fb" , module = "mypkg.pkg" , orig_name = "ext_fb"
44
+ ),
45
+ "ext_fc" : ImportFromData (
46
+ order = 5 , name = "ext_fc" , module = "mypkg.pkg.pkg.util" , orig_name = "ext_fc"
47
+ ),
29
48
"a" : AssignData (
30
- order = 0 , name = "a" , type_comment = "int" , docstring = "a docstring"
49
+ order = 6 , name = "a" , type_comment = "int" , docstring = "a docstring"
31
50
),
32
- "a2" : AssignData (order = 1 , name = "a2" , type_comment = "int" , docstring = None ),
33
- "a3" : AssignData (order = 2 , name = "a3" , type_comment = None , docstring = None ),
51
+ "a2" : AssignData (order = 7 , name = "a2" , type_comment = "int" , docstring = None ),
52
+ "a3" : AssignData (order = 8 , name = "a3" , type_comment = None , docstring = None ),
34
53
"b" : AssignData (
35
- order = 3 , name = "b" , type_comment = "int" , docstring = "b docstring"
54
+ order = 9 , name = "b" , type_comment = "int" , docstring = "b docstring"
36
55
),
37
- "fa" : FunctionDefData (order = 4 , name = "fa" ),
56
+ "fa" : FunctionDefData (order = 10 , name = "fa" ),
38
57
"c" : AssignData (
39
- order = 5 , name = "c" , type_comment = None , docstring = "c and d docstring"
58
+ order = 11 , name = "c" , type_comment = None , docstring = "c and d docstring"
40
59
),
41
60
"d" : AssignData (
42
- order = 6 , name = "d" , type_comment = None , docstring = "c and d docstring"
61
+ order = 12 , name = "d" , type_comment = None , docstring = "c and d docstring"
43
62
),
44
63
"a1" : AssignData (
45
- order = 7 , name = "a1" , type_comment = None , docstring = "abcde11111 docstring"
64
+ order = 13 , name = "a1" , type_comment = None , docstring = "abcde11111 docstring"
46
65
),
47
66
"b1" : AssignData (
48
- order = 8 , name = "b1" , type_comment = None , docstring = "abcde11111 docstring"
67
+ order = 14 , name = "b1" , type_comment = None , docstring = "abcde11111 docstring"
49
68
),
50
69
"c1" : AssignData (
51
- order = 9 , name = "c1" , type_comment = None , docstring = "abcde11111 docstring"
70
+ order = 15 , name = "c1" , type_comment = None , docstring = "abcde11111 docstring"
52
71
),
53
72
"d1" : AssignData (
54
- order = 10 , name = "d1" , type_comment = None , docstring = "abcde11111 docstring"
73
+ order = 16 , name = "d1" , type_comment = None , docstring = "abcde11111 docstring"
55
74
),
56
75
"e1" : AssignData (
57
- order = 11 , name = "e1" , type_comment = None , docstring = "abcde11111 docstring"
76
+ order = 17 , name = "e1" , type_comment = None , docstring = "abcde11111 docstring"
58
77
),
59
78
"A" : ClassDefData (
60
- order = 12 ,
79
+ order = 18 ,
61
80
name = "A" ,
62
81
freevars = {
63
82
"a" : AssignData (
64
- order = 13 , name = "a" , type_comment = None , docstring = None
83
+ order = 19 , name = "a" , type_comment = None , docstring = None
65
84
)
66
85
},
67
86
instance_vars = {},
68
87
methods = {},
69
88
),
70
89
"B" : ClassDefData (
71
- order = 14 ,
90
+ order = 20 ,
72
91
name = "B" ,
73
92
freevars = {
74
93
"a" : AssignData (
75
- order = 15 , name = "a" , type_comment = None , docstring = "B.a docstring"
94
+ order = 21 , name = "a" , type_comment = None , docstring = "B.a docstring"
76
95
)
77
96
},
78
97
instance_vars = {},
79
98
methods = {},
80
99
),
81
100
"B1" : ClassDefData (
82
- order = 16 ,
101
+ order = 22 ,
83
102
name = "B1" ,
84
103
freevars = {
85
104
"a" : AssignData (
86
- order = 17 , name = "a" , type_comment = None , docstring = None
105
+ order = 23 , name = "a" , type_comment = None , docstring = None
87
106
),
88
- "__init__" : FunctionDefData (order = 18 , name = "__init__" ),
89
- "b" : FunctionDefData (order = 19 , name = "b" ),
107
+ "__init__" : FunctionDefData (order = 24 , name = "__init__" ),
108
+ "b" : FunctionDefData (order = 25 , name = "b" ),
90
109
},
91
110
instance_vars = {},
92
111
methods = {},
93
112
),
94
113
"C" : ClassDefData (
95
- order = 20 ,
114
+ order = 26 ,
96
115
name = "C" ,
97
116
freevars = {
98
117
"a" : AssignData (
99
- order = 21 ,
118
+ order = 27 ,
100
119
name = "a" ,
101
120
type_comment = None ,
102
121
docstring = "C.a classvar docstring" ,
103
122
),
104
- "__init__" : FunctionDefData (order = 22 , name = "__init__" ),
123
+ "__init__" : FunctionDefData (order = 28 , name = "__init__" ),
105
124
"_A" : ClassDefData (
106
- order = 27 ,
125
+ order = 33 ,
107
126
name = "_A" ,
108
127
freevars = {
109
128
"_a" : AssignData (
110
- order = 28 ,
129
+ order = 34 ,
111
130
name = "_a" ,
112
131
type_comment = None ,
113
132
docstring = "nested OK" ,
@@ -119,22 +138,22 @@ def test_simple_data(self):
119
138
},
120
139
instance_vars = {
121
140
"a" : AssignData (
122
- order = 23 ,
141
+ order = 29 ,
123
142
name = "a" ,
124
143
type_comment = "int | None" ,
125
144
docstring = "C instance var a/b docstring" ,
126
145
),
127
146
"b" : AssignData (
128
- order = 24 ,
147
+ order = 30 ,
129
148
name = "b" ,
130
149
type_comment = "int | None" ,
131
150
docstring = "C instance var a/b docstring" ,
132
151
),
133
152
"c" : AssignData (
134
- order = 25 , name = "c" , type_comment = None , docstring = None
153
+ order = 31 , name = "c" , type_comment = None , docstring = None
135
154
),
136
155
"d" : AssignData (
137
- order = 26 ,
156
+ order = 32 ,
138
157
name = "d" ,
139
158
type_comment = None ,
140
159
docstring = "C instance var d docstring" ,
@@ -147,7 +166,7 @@ def test_simple_data(self):
147
166
def test_assigndata_override (self ):
148
167
code = get_analyzer_data ("assigndata-override-ast.py" )
149
168
module = ast_parse (code )
150
- visitor = DefinitionFinder ("<test>" , "<test>" )
169
+ visitor = DefinitionFinder (package = "<test>" )
151
170
visitor .visit (module )
152
171
module_freevars = visitor .freevars
153
172
assert module_freevars == {
0 commit comments