forked from tibdex/pydocstyle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.py
921 lines (799 loc) · 26.2 KB
/
parser_test.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
"""Parser tests."""
import io
import sys
import pytest
import textwrap
from pathlib import Path
from pydocstyle.parser import Parser, ParseError
class CodeSnippet(io.StringIO):
"""A code snippet.
Automatically wraps snippet as a file-like object and handles line wraps.
"""
def __init__(self, code_string):
"""Initialize the object."""
io.StringIO.__init__(self, textwrap.dedent(code_string))
def test_function():
"""Test parsing of a simple function."""
parser = Parser()
code = CodeSnippet("""\
def do_something(pos_param0, pos_param1, kw_param0="default"):
\"""Do something.\"""
return None
""")
module = parser.parse(code, 'file_path')
assert module.is_public
assert module.dunder_all is None
function, = module.children
assert function.name == 'do_something'
assert function.decorators == []
assert function.children == []
assert function.docstring == '"""Do something."""'
assert function.docstring.start == 2
assert function.docstring.end == 2
assert function.kind == 'function'
assert function.parent == module
assert function.start == 1
assert function.end == 3
assert function.error_lineno == 2
assert function.source == code.getvalue()
assert function.is_public
assert str(function) == 'in public function `do_something`'
def test_simple_fstring():
"""Test parsing of a function with a simple fstring as a docstring."""
# fstrings are not supported in Python 3.5
if sys.version_info[0:2] == (3, 5):
return
parser = Parser()
code = CodeSnippet("""\
def do_something(pos_param0, pos_param1, kw_param0="default"):
f\"""Do something.\"""
return None
""")
module = parser.parse(code, 'file_path')
assert module.is_public
assert module.dunder_all is None
function, = module.children
assert function.name == 'do_something'
assert function.decorators == []
assert function.children == []
assert function.docstring == 'f"""Do something."""'
assert function.docstring.start == 2
assert function.docstring.end == 2
assert function.kind == 'function'
assert function.parent == module
assert function.start == 1
assert function.end == 3
assert function.error_lineno == 2
assert function.source == code.getvalue()
assert function.is_public
assert str(function) == 'in public function `do_something`'
def test_fstring_with_args():
"""Test parsing of a function with an fstring with args as a docstring."""
# fstrings are not supported in Python 3.5
if sys.version_info[0:2] == (3, 5):
return
parser = Parser()
code = CodeSnippet("""\
foo = "bar"
bar = "baz"
def do_something(pos_param0, pos_param1, kw_param0="default"):
f\"""Do some {foo} and some {bar}.\"""
return None
""")
module = parser.parse(code, 'file_path')
assert module.is_public
assert module.dunder_all is None
function, = module.children
assert function.name == 'do_something'
assert function.decorators == []
assert function.children == []
assert function.docstring == 'f"""Do some {foo} and some {bar}."""'
assert function.docstring.start == 4
assert function.docstring.end == 4
assert function.kind == 'function'
assert function.parent == module
assert function.start == 3
assert function.end == 5
assert function.error_lineno == 4
assert function.source == textwrap.dedent("""\
def do_something(pos_param0, pos_param1, kw_param0="default"):
f\"""Do some {foo} and some {bar}.\"""
return None
""")
assert function.is_public
assert str(function) == 'in public function `do_something`'
def test_decorated_function():
"""Test parsing of a simple function with a decorator."""
parser = Parser()
code = CodeSnippet("""\
@single_decorator
def do_something():
\"""Do something.\"""
return None
""")
module = parser.parse(code, 'file_path')
function, = module.children
assert function.name == 'do_something'
assert len(function.decorators) == 1
assert function.decorators[0].name == 'single_decorator'
assert function.children == []
assert function.docstring == '"""Do something."""'
assert function.kind == 'function'
assert function.parent == module
assert function.start == 2
assert function.end == 4
assert function.source == textwrap.dedent("""\
def do_something():
\"""Do something.\"""
return None
""")
assert function.is_public
assert str(function) == 'in public function `do_something`'
def test_nested_function():
"""Test parsing of a nested function."""
parser = Parser()
code = CodeSnippet("""\
def outer_function():
\"""This is the outer function.\"""
def inner_function():
'''This is the inner function.'''
return None
return None
""")
module = parser.parse(code, 'file_path')
outer_function, = module.children
assert outer_function.name == 'outer_function'
assert outer_function.decorators == []
assert outer_function.docstring == '"""This is the outer function."""'
assert outer_function.kind == 'function'
assert outer_function.parent == module
assert outer_function.start == 1
assert outer_function.end == 6
assert outer_function.error_lineno == 2
assert outer_function.source == code.getvalue()
assert outer_function.is_public
assert str(outer_function) == 'in public function `outer_function`'
inner_function, = outer_function.children
assert inner_function.name == 'inner_function'
assert inner_function.decorators == []
assert inner_function.docstring == "'''This is the inner function.'''"
assert inner_function.kind == 'function'
assert inner_function.parent == outer_function
assert inner_function.start == 3
assert inner_function.end == 5
assert inner_function.error_lineno == 4
assert textwrap.dedent(inner_function.source) == textwrap.dedent("""\
def inner_function():
'''This is the inner function.'''
return None
""")
assert not inner_function.is_public
assert str(inner_function) == 'in private nested function `inner_function`'
def test_conditional_nested_function():
"""Test parsing of a nested function inside a condition."""
parser = Parser()
code = CodeSnippet("""\
def outer_function():
\"""This is the outer function.\"""
if True:
def inner_function():
'''This is the inner function.'''
return None
return None
""")
module = parser.parse(code, 'file_path')
outer_function, = module.children
assert outer_function.name == 'outer_function'
assert outer_function.decorators == []
assert outer_function.docstring == '"""This is the outer function."""'
assert outer_function.kind == 'function'
assert outer_function.parent == module
assert outer_function.start == 1
assert outer_function.end == 7
assert outer_function.source == code.getvalue()
assert outer_function.is_public
assert str(outer_function) == 'in public function `outer_function`'
inner_function, = outer_function.children
assert inner_function.name == 'inner_function'
assert inner_function.decorators == []
assert inner_function.docstring == "'''This is the inner function.'''"
assert inner_function.kind == 'function'
assert inner_function.parent == outer_function
assert inner_function.start == 4
assert inner_function.end == 6
assert textwrap.dedent(inner_function.source) == textwrap.dedent("""\
def inner_function():
'''This is the inner function.'''
return None
""")
assert not inner_function.is_public
assert str(inner_function) == 'in private nested function `inner_function`'
def test_doubly_nested_function():
"""Test parsing of a nested function inside a nested function."""
parser = Parser()
code = CodeSnippet("""\
def outer_function():
\"""This is the outer function.\"""
def middle_function():
def inner_function():
'''This is the inner function.'''
return None
return None
""")
module = parser.parse(code, 'file_path')
outer_function, = module.children
assert outer_function.name == 'outer_function'
assert outer_function.decorators == []
assert outer_function.docstring == '"""This is the outer function."""'
assert outer_function.kind == 'function'
assert outer_function.parent == module
assert outer_function.start == 1
assert outer_function.end == 7
assert outer_function.source == code.getvalue()
assert outer_function.is_public
assert str(outer_function) == 'in public function `outer_function`'
middle_function, = outer_function.children
assert middle_function.name == 'middle_function'
assert middle_function.decorators == []
assert middle_function.docstring is None
assert middle_function.kind == 'function'
assert middle_function.parent == outer_function
assert middle_function.start == 3
assert middle_function.end == 6
assert textwrap.dedent(middle_function.source) == textwrap.dedent("""\
def middle_function():
def inner_function():
'''This is the inner function.'''
return None
""")
assert not middle_function.is_public
assert (str(middle_function) ==
'in private nested function `middle_function`')
inner_function, = middle_function.children
assert inner_function.name == 'inner_function'
assert inner_function.decorators == []
assert inner_function.docstring == "'''This is the inner function.'''"
assert inner_function.kind == 'function'
assert inner_function.parent == middle_function
assert inner_function.start == 4
assert inner_function.end == 6
assert textwrap.dedent(inner_function.source) == textwrap.dedent("""\
def inner_function():
'''This is the inner function.'''
return None
""")
assert not inner_function.is_public
assert str(inner_function) == 'in private nested function `inner_function`'
def test_class():
"""Test parsing of a class."""
parser = Parser()
code = CodeSnippet("""\
class TestedClass(object):
" an ugly docstring "
""")
module = parser.parse(code, 'file_path')
klass, = module.children
assert klass.name == 'TestedClass'
assert klass.decorators == []
assert klass.children == []
assert klass.docstring == '" an ugly docstring "'
assert klass.kind == 'class'
assert klass.parent == module
assert klass.start == 1
assert klass.end == 3
assert klass.error_lineno == 3
assert klass.source == code.getvalue()
assert klass.is_public
assert str(klass) == 'in public class `TestedClass`'
def test_public_method():
"""Test parsing of a public method."""
parser = Parser()
code = CodeSnippet("""\
class TestedClass(object):
def do_it(param):
\"""Do the 'it'\"""
# do nothing
return None
""")
module = parser.parse(code, 'file_path')
klass, = module.children
assert klass.name == 'TestedClass'
assert klass.decorators == []
assert klass.docstring is None
assert klass.kind == 'class'
assert klass.parent == module
assert klass.start == 1
assert klass.end == 5
assert klass.error_lineno == 1
assert klass.source == code.getvalue()
assert klass.is_public
assert str(klass) == 'in public class `TestedClass`'
method, = klass.children
assert method.name == 'do_it'
assert method.decorators == []
assert method.docstring == '''"""Do the 'it'"""'''
assert method.kind == 'method'
assert method.parent == klass
assert method.start == 2
assert method.end == 5
assert method.error_lineno == 3
assert textwrap.dedent(method.source) == textwrap.dedent("""\
def do_it(param):
\"""Do the 'it'\"""
# do nothing
return None
""")
assert method.is_public
assert not method.is_magic
assert str(method) == 'in public method `do_it`'
def test_private_method():
"""Test parsing of a private method."""
parser = Parser()
code = CodeSnippet("""\
class TestedClass(object):
def _do_it(param):
\"""Do the 'it'\"""
# do nothing
return None
""")
module = parser.parse(code, 'file_path')
klass, = module.children
assert klass.name == 'TestedClass'
assert klass.decorators == []
assert klass.docstring is None
assert klass.kind == 'class'
assert klass.parent == module
assert klass.start == 1
assert klass.end == 5
assert klass.error_lineno == 1
assert klass.source == code.getvalue()
assert klass.is_public
assert str(klass) == 'in public class `TestedClass`'
method, = klass.children
assert method.name == '_do_it'
assert method.decorators == []
assert method.docstring == '''"""Do the 'it'"""'''
assert method.kind == 'method'
assert method.parent == klass
assert method.start == 2
assert method.end == 5
assert method.error_lineno == 3
assert textwrap.dedent(method.source) == textwrap.dedent("""\
def _do_it(param):
\"""Do the 'it'\"""
# do nothing
return None
""")
assert not method.is_public
assert not method.is_magic
assert str(method) == 'in private method `_do_it`'
def test_magic_method():
"""Test parsing of a magic method."""
parser = Parser()
code = CodeSnippet("""\
class TestedClass(object):
def __str__(self):
return "me"
""")
module = parser.parse(code, 'file_path')
klass, = module.children
assert klass.name == 'TestedClass'
assert klass.decorators == []
assert klass.docstring is None
assert klass.kind == 'class'
assert klass.parent == module
assert klass.start == 1
assert klass.end == 3
assert klass.error_lineno == 1
assert klass.source == code.getvalue()
assert klass.is_public
assert str(klass) == 'in public class `TestedClass`'
method, = klass.children[0]
assert method.name == '__str__'
assert method.decorators == []
assert method.docstring is None
assert method.kind == 'method'
assert method.parent == klass
assert method.start == 2
assert method.end == 3
assert method.error_lineno == 2
assert textwrap.dedent(method.source) == textwrap.dedent("""\
def __str__(self):
return "me"
""")
assert method.is_public
assert method.is_magic
assert str(method) == 'in public method `__str__`'
def test_nested_class():
"""Test parsing of a class."""
parser = Parser()
code = CodeSnippet("""\
class OuterClass(object):
' an outer docstring'
class InnerClass(object):
"An inner docstring."
""")
module = parser.parse(code, 'file_path')
outer_class, = module.children
assert outer_class.name == 'OuterClass'
assert outer_class.decorators == []
assert outer_class.docstring == "' an outer docstring'"
assert outer_class.kind == 'class'
assert outer_class.parent == module
assert outer_class.start == 1
assert outer_class.end == 4
assert outer_class.error_lineno == 2
assert outer_class.source == code.getvalue()
assert outer_class.is_public
assert str(outer_class) == 'in public class `OuterClass`'
inner_class, = outer_class.children
assert inner_class.name == 'InnerClass'
assert inner_class.decorators == []
assert inner_class.children == []
assert inner_class.docstring == '"An inner docstring."'
assert inner_class.kind == 'class'
assert inner_class.parent == outer_class
assert inner_class.start == 3
assert inner_class.end == 4
assert inner_class.error_lineno == 4
assert textwrap.dedent(inner_class.source) == textwrap.dedent("""\
class InnerClass(object):
"An inner docstring."
""")
assert inner_class.is_public
assert str(inner_class) == 'in public nested class `InnerClass`'
def test_raise_from():
"""Make sure 'raise x from y' doesn't trip the parser."""
parser = Parser()
code = CodeSnippet("raise ValueError() from None")
parser.parse(code, 'file_path')
def test_simple_matrix_multiplication():
"""Make sure 'a @ b' doesn't trip the parser."""
parser = Parser()
code = CodeSnippet("""
def foo():
a @ b
""")
parser.parse(code, 'file_path')
@pytest.mark.parametrize("code", (
CodeSnippet("""
def foo():
a @ b
(a
@b)
@a
def b():
pass
"""),
CodeSnippet("""
def foo():
a @ b
(a
@b)
a\
@b
@a
def b():
pass
"""),
CodeSnippet("""
def foo():
a @ b
(a
# A random comment here
@b)
a\
@b
@a
def b():
pass
"""),
))
def test_matrix_multiplication_with_decorators(code):
"""Make sure 'a @ b' doesn't trip the parser."""
parser = Parser()
module = parser.parse(code, 'file_path')
outer_function, = module.children
assert outer_function.name == 'foo'
inner_function, = outer_function.children
assert len(inner_function.decorators) == 1
assert inner_function.decorators[0].name == 'a'
@pytest.mark.parametrize("parent_path", (
Path("some") / "directory",
Path("")
))
def test_module_publicity(parent_path):
"""Test module publicity.
Modules containing a single leading underscore are private,
and modules within a private package are private.
Private packages contain a single leading underscore.
"""
parser = Parser()
code = CodeSnippet("")
module = parser.parse(code, str(parent_path / "filepath"))
assert module.is_public
module = parser.parse(code, str(parent_path / "_private_pkg" / "filepath"))
assert not module.is_public
module = parser.parse(code, str(
parent_path / "_private_pkg" / "some_pkg" / "filepath"))
assert not module.is_public
module = parser.parse(code, str(parent_path / "_filepath"))
assert not module.is_public
module = parser.parse(code, str(parent_path / "__filepath"))
assert module.is_public
def test_complex_module():
"""Test that a complex module is parsed correctly."""
parser = Parser()
code = CodeSnippet('''\
"""Module."""
__all__ = ('a', 'b'
'c',)
def function():
"Function."
def nested_1():
"""Nested."""
if True:
def nested_2():
pass
class class_(object):
"""Class."""
def method_1(self):
"""Method."""
def method_2(self):
def nested_3(self):
"""Nested."""
''')
module = parser.parse(code, "filepath")
assert list(module)[0] == module
assert len(list(module)) == 8
@pytest.mark.parametrize("code", (
CodeSnippet("""\
__all__ = ['foo', 'bar']
"""),
CodeSnippet("""\
__all__ = ['foo', 'ba'
'r',]
"""),
CodeSnippet("""\
__all__ = ('foo',
'bar'
)
"""),
CodeSnippet("""\
__all__ = ['foo',
# Inconvenient comment
'bar'
]
"""),
CodeSnippet("""\
__all__ = 'foo', 'bar'
"""),
CodeSnippet("""\
__all__ = 'foo', 'bar',
"""),
CodeSnippet(
"""__all__ = 'foo', 'bar'"""
),
CodeSnippet("""\
__all__ = 'foo', \
'bar'
"""),
CodeSnippet("""\
foo = 1
__all__ = 'foo', 'bar'
"""),
CodeSnippet("""\
__all__ = 'foo', 'bar'
foo = 1
"""),
CodeSnippet("""\
__all__ = ['foo', 'bar'] # never freeze
"""),
))
def test_dunder_all(code):
"""Test that __all__ is parsed correctly."""
parser = Parser()
module = parser.parse(code, "filepath")
assert module.dunder_all == ('foo', 'bar')
def test_single_value_dunder_all():
"""Test that single value __all__ is parsed correctly."""
parser = Parser()
code = CodeSnippet("""\
__all__ = 'foo',
""")
module = parser.parse(code, "filepath")
assert module.dunder_all == ('foo', )
code = CodeSnippet("""\
__all__ = 'foo'
""")
module = parser.parse(code, "filepath")
assert module.dunder_all is None
assert module.dunder_all_error
code = CodeSnippet("""\
__all__ = ('foo', )
""")
module = parser.parse(code, "filepath")
assert module.dunder_all == ('foo', )
indeterminable_dunder_all_test_cases = [
CodeSnippet("""\
__all__ = ['foo']
__all__ += ['bar']
"""),
CodeSnippet("""\
__all__ = ['foo'] + ['bar']
"""),
CodeSnippet("""\
__all__ = ['foo']
__all__.insert('bar')
"""),
CodeSnippet("""\
__all__ = foo()
"""),
CodeSnippet("""\
all = ['foo']
__all__ = all
"""),
CodeSnippet("""\
foo = 'foo'
__all__ = [foo]
"""),
CodeSnippet("""\
__all__ = (*foo, 'bar')
"""),
]
@pytest.mark.parametrize("code", indeterminable_dunder_all_test_cases)
def test_indeterminable_dunder_all(code):
"""Test that __all__ is ignored if it can't be statically evaluated."""
parser = Parser()
module = parser.parse(code, "filepath")
assert module.dunder_all is None
assert module.dunder_all_error
@pytest.mark.parametrize("code", (
CodeSnippet("""\
from __future__ import unicode_literals, nested_scopes
"""),
CodeSnippet("""\
from __future__ import unicode_literals, nested_scopes;
"""),
CodeSnippet("""\
from __future__ import unicode_literals
from __future__ import nested_scopes;
"""),
CodeSnippet("""\
from __future__ import unicode_literals
from __future__ import nested_scopes as ns
"""),
CodeSnippet("""\
from __future__ import (unicode_literals as nl,
nested_scopes)
"""),
CodeSnippet("""\
from __future__ import (unicode_literals as nl,)
from __future__ import (nested_scopes)
"""),
CodeSnippet("""\
from __future__ \\
import unicode_literals
from __future__ \\
import nested_scopes
"""),
))
def test_future_import(code):
"""Test that __future__ imports are properly parsed and collected."""
parser = Parser()
module = parser.parse(code, "filepath")
assert module.future_imports == {'unicode_literals', 'nested_scopes'}
def test_noqa_function():
"""Test that "# noqa" comments are correctly collected for definitions."""
code = CodeSnippet("""\
def foo(): # noqa: D100,D101
pass
""")
parser = Parser()
module = parser.parse(code, "filepath")
function, = module.children
assert function.skipped_error_codes == 'D100,D101'
@pytest.mark.parametrize("code", (
CodeSnippet("""\
while True:
try:
pass
"""),
CodeSnippet("[\n"),
# Should result in `SyntaxError: from __future__ imports must occur
# at the beginning of the file`
CodeSnippet("""\
from __future__ import unicode_literals; import string; from \
__future__ import nested_scopes
"""),
))
def test_invalid_syntax(code):
"""Test invalid code input to the parser."""
parser = Parser()
with pytest.raises(ParseError):
module = parser.parse(code, "filepath")
@pytest.mark.parametrize("code", (
CodeSnippet("""\
'''Test this'''
@property
def test():
pass
"""),
CodeSnippet("""\
'''Test this'''
@property
def test():
pass
"""),
CodeSnippet("""\
'''Test this'''
@property
def test():
pass
"""),
CodeSnippet("""\
'''Test this'''
@property
def test():
pass
"""),
CodeSnippet("""\
'''Test this'''
# A random comment in the middle to break things
@property
def test():
pass
"""),
))
def test_parsing_function_decorators(code):
"""Test to ensure we are correctly parsing function decorators."""
parser = Parser()
module = parser.parse(code, "filename")
function, = module.children
decorator_names = {dec.name for dec in function.decorators}
assert "property" in decorator_names
@pytest.mark.parametrize("code", (
CodeSnippet("""\
class Test:
@property
def test(self):
pass
"""),
CodeSnippet("""\
class Test:
@property
def test(self):
pass
"""),
CodeSnippet("""\
class Test:
# Random comment to trip decorator parsing
@property
def test(self):
pass
"""),
CodeSnippet("""\
class Test:
# Random comment to trip decorator parsing
A = 1
@property
def test(self):
pass
"""),
CodeSnippet("""\
class Test:
# Random comment to trip decorator parsing
A = 1
'''Another random comment'''
@property
def test(self):
pass
"""),
))
def test_parsing_method_decorators(code):
"""Test to ensure we are correctly parsing method decorators."""
parser = Parser()
module = parser.parse(code, "filename")
function, = module.children[0].children
decorator_names = {dec.name for dec in function.decorators}
assert "property" in decorator_names