summaryrefslogtreecommitdiff
path: root/tests/checkers/unittest_python3.py
blob: 70456b7561649865cacb381817d83afffdfad004 (plain)
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
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
# Copyright (c) 2014-2020 Claudiu Popa <pcmanticore@gmail.com>
# Copyright (c) 2014-2015 Brett Cannon <brett@python.org>
# Copyright (c) 2015 Ionel Cristian Maries <contact@ionelmc.ro>
# Copyright (c) 2015 Cosmin Poieana <cmin@ropython.org>
# Copyright (c) 2015 Viorel Stirbu <viorels@gmail.com>
# Copyright (c) 2016-2017 Roy Williams <roy.williams.iii@gmail.com>
# Copyright (c) 2016 Roy Williams <rwilliams@lyft.com>
# Copyright (c) 2016 Derek Gustafson <degustaf@gmail.com>
# Copyright (c) 2017 Daniel Miller <millerdev@gmail.com>
# Copyright (c) 2018-2020 Anthony Sottile <asottile@umich.edu>
# Copyright (c) 2018 sbagan <pnlbagan@gmail.com>
# Copyright (c) 2018 Aivar Annamaa <aivarannamaa@users.noreply.github.com>
# Copyright (c) 2018 Ville Skyttä <ville.skytta@iki.fi>
# Copyright (c) 2019-2020 Pierre Sassoulas <pierre.sassoulas@gmail.com>
# Copyright (c) 2019 Gabriel R Sezefredo <gabriel@sezefredo.com.br>
# Copyright (c) 2019 Hugo van Kemenade <hugovk@users.noreply.github.com>
# Copyright (c) 2019 Ashley Whetter <ashley@awhetter.co.uk>
# Copyright (c) 2020 Federico Bond <federicobond@gmail.com>
# Copyright (c) 2020 Athos Ribeiro <athoscr@fedoraproject.org>

# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
# For details: https://github.com/PyCQA/pylint/blob/master/COPYING

"""Tests for the python3 checkers."""

# pylint: disable=too-many-public-methods,redefined-outer-name

import astroid

from pylint import testutils
from pylint.checkers import python3 as checker
from pylint.interfaces import INFERENCE, INFERENCE_FAILURE

# TODO(cpopa): Port these to the functional test framework instead. pylint: disable=fixme


class TestPython3Checker(testutils.CheckerTestCase):

    CHECKER_CLASS = checker.Python3Checker

    def check_bad_builtin(self, builtin_name):
        node = astroid.extract_node(builtin_name + "  #@")
        message = builtin_name.lower() + "-builtin"
        with self.assertAddsMessages(testutils.Message(message, node=node)):
            self.checker.visit_name(node)

    def test_bad_builtins(self):
        builtins = [
            "apply",
            "buffer",
            "cmp",
            "coerce",
            "execfile",
            "file",
            "input",
            "intern",
            "long",
            "raw_input",
            "round",
            "reduce",
            "StandardError",
            "unichr",
            "unicode",
            "xrange",
            "reload",
        ]
        for builtin in builtins:
            self.check_bad_builtin(builtin)

    def as_iterable_in_for_loop_test(self, fxn):
        code = f"for x in {fxn}(): pass"
        module = astroid.parse(code)
        with self.assertNoMessages():
            self.walk(module)

    def as_used_by_iterable_in_for_loop_test(self, fxn):
        checker = f"{fxn}-builtin-not-iterating"
        node = astroid.extract_node(
            f"""
        for x in (whatever(
            {fxn}() #@
        )):
            pass
        """
        )
        message = testutils.Message(checker, node=node)
        with self.assertAddsMessages(message):
            self.checker.visit_call(node)

    def as_iterable_in_genexp_test(self, fxn):
        code = f"x = (x for x in {fxn}())"
        module = astroid.parse(code)
        with self.assertNoMessages():
            self.walk(module)

    def as_iterable_in_starred_context(self, fxn):
        code = f"x = test(*{fxn}())"
        module = astroid.parse(code)
        with self.assertNoMessages():
            self.walk(module)

    def as_iterable_in_listcomp_test(self, fxn):
        code = f"x = [x for x in {fxn}(None, [1])]"
        module = astroid.parse(code)
        with self.assertNoMessages():
            self.walk(module)

    def as_iterable_in_yield_from(self, fxn):
        code = f"yield from {fxn}()"
        module = astroid.parse(code)
        with self.assertNoMessages():
            self.walk(module)

    def as_used_in_variant_in_genexp_test(self, fxn):
        checker = f"{fxn}-builtin-not-iterating"
        node = astroid.extract_node(
            f"""
        list(
            __({fxn}(x))
            for x in [1]
        )
        """
        )
        message = testutils.Message(checker, node=node)
        with self.assertAddsMessages(message):
            self.checker.visit_call(node)

    def as_used_in_variant_in_listcomp_test(self, fxn):
        checker = f"{fxn}-builtin-not-iterating"
        node = astroid.extract_node(
            f"""
        [
            __({fxn}(None, x))
        for x in [[1]]]
        """
        )
        message = testutils.Message(checker, node=node)
        with self.assertAddsMessages(message):
            self.checker.visit_call(node)

    def as_argument_to_callable_constructor_test(self, fxn, callable_fn):
        module = astroid.parse(f"x = {callable_fn}({fxn}())")
        with self.assertNoMessages():
            self.walk(module)

    def as_argument_to_materialized_filter(self, callable_fn):
        module = astroid.parse(f"list(filter(None, {callable_fn}()))")
        with self.assertNoMessages():
            self.walk(module)

    def as_argument_to_random_fxn_test(self, fxn):
        checker = f"{fxn}-builtin-not-iterating"
        node = astroid.extract_node(
            f"""
        y(
            {fxn}() #@
        )
        """
        )
        message = testutils.Message(checker, node=node)
        with self.assertAddsMessages(message):
            self.checker.visit_call(node)

    def as_argument_to_str_join_test(self, fxn):
        code = f"x = ''.join({fxn}())"
        module = astroid.parse(code)
        with self.assertNoMessages():
            self.walk(module)

    def as_argument_to_itertools_functions(self, fxn):
        code = f"""
        from __future__ import absolute_import
        import itertools
        from itertools import product
        for i,j in product({fxn}(), repeat=2):
            pass
        for i,j in itertools.product({fxn}(), repeat=2):
            pass
        """
        module = astroid.parse(code)
        with self.assertNoMessages():
            self.walk(module)

    def as_argument_to_zip_test(self, fxn):
        module = astroid.parse(f"list(zip({fxn}))")
        with self.assertNoMessages():
            self.walk(module)

    def as_argument_to_map_test(self, fxn):
        module = astroid.parse(f"list(map(__, {fxn}()))")
        with self.assertNoMessages():
            self.walk(module)

    def as_iterable_in_unpacking(self, fxn):
        node = astroid.extract_node(
            f"""
        a, b = __({fxn}())
        """
        )
        with self.assertNoMessages():
            self.checker.visit_call(node)

    def as_assignment(self, fxn):
        checker = f"{fxn}-builtin-not-iterating"
        node = astroid.extract_node(
            f"""
        a = __({fxn}())
        """
        )
        message = testutils.Message(checker, node=node)
        with self.assertAddsMessages(message):
            self.checker.visit_call(node)

    def iterating_context_tests(self, fxn):
        """Helper for verifying a function isn't used as an iterator."""
        self.as_iterable_in_for_loop_test(fxn)
        self.as_used_by_iterable_in_for_loop_test(fxn)
        self.as_iterable_in_genexp_test(fxn)
        self.as_iterable_in_listcomp_test(fxn)
        self.as_used_in_variant_in_genexp_test(fxn)
        self.as_used_in_variant_in_listcomp_test(fxn)
        self.as_argument_to_random_fxn_test(fxn)
        self.as_argument_to_str_join_test(fxn)
        self.as_iterable_in_unpacking(fxn)
        self.as_assignment(fxn)
        self.as_argument_to_materialized_filter(fxn)
        self.as_iterable_in_yield_from(fxn)
        self.as_iterable_in_starred_context(fxn)
        self.as_argument_to_itertools_functions(fxn)
        self.as_argument_to_zip_test(fxn)
        self.as_argument_to_map_test(fxn)
        for func in (
            "iter",
            "list",
            "tuple",
            "sorted",
            "set",
            "sum",
            "any",
            "all",
            "enumerate",
            "dict",
            "OrderedDict",
        ):
            self.as_argument_to_callable_constructor_test(fxn, func)

    def test_dict_subclasses_methods_in_iterating_context(self):
        iterating, not_iterating = astroid.extract_node(
            """
        from __future__ import absolute_import
        from collections import defaultdict
        d = defaultdict(list)
        a, b = d.keys() #@
        x = d.keys() #@
        """
        )

        with self.assertNoMessages():
            self.checker.visit_call(iterating.value)

        message = testutils.Message("dict-keys-not-iterating", node=not_iterating.value)
        with self.assertAddsMessages(message):
            self.checker.visit_call(not_iterating.value)

    def test_dict_methods_in_iterating_context(self):
        iterating_code = [
            "for x in {}(): pass",
            "(x for x in {}())",
            "[x for x in {}()]",
            "iter({}())",
            "a, b = {}()",
            "max({}())",
            "min({}())",
            "3 in {}()",
            "3 not in {}()",
            "set().update({}())",
            "[].extend({}())",
            "{{}}.update({}())",
            """
            from __future__ import absolute_import
            from itertools import chain
            chain.from_iterable({}())
            """,
        ]
        non_iterating_code = ["x = __({}())", "__({}())[0]"]

        for method in ("keys", "items", "values"):
            dict_method = "{{}}.{}".format(method)

            for code in iterating_code:
                with_value = code.format(dict_method)
                module = astroid.parse(with_value)
                with self.assertNoMessages():
                    self.walk(module)

            for code in non_iterating_code:
                with_value = code.format(dict_method)
                node = astroid.extract_node(with_value)

                checker = f"dict-{method}-not-iterating"
                message = testutils.Message(checker, node=node)
                with self.assertAddsMessages(message):
                    self.checker.visit_call(node)

    def test_map_in_iterating_context(self):
        self.iterating_context_tests("map")

    def test_zip_in_iterating_context(self):
        self.iterating_context_tests("zip")

    def test_range_in_iterating_context(self):
        self.iterating_context_tests("range")

    def test_filter_in_iterating_context(self):
        self.iterating_context_tests("filter")

    def defined_method_test(self, method, warning):
        """Helper for verifying that a certain method is not defined."""
        node = astroid.extract_node(
            f"""
            class Foo(object):
                def __{method}__(self, other):  #@
                    pass"""
        )
        message = testutils.Message(warning, node=node)
        with self.assertAddsMessages(message):
            self.checker.visit_functiondef(node)

    def test_delslice_method(self):
        self.defined_method_test("delslice", "delslice-method")

    def test_getslice_method(self):
        self.defined_method_test("getslice", "getslice-method")

    def test_setslice_method(self):
        self.defined_method_test("setslice", "setslice-method")

    def test_coerce_method(self):
        self.defined_method_test("coerce", "coerce-method")

    def test_oct_method(self):
        self.defined_method_test("oct", "oct-method")

    def test_hex_method(self):
        self.defined_method_test("hex", "hex-method")

    def test_nonzero_method(self):
        self.defined_method_test("nonzero", "nonzero-method")

    def test_cmp_method(self):
        self.defined_method_test("cmp", "cmp-method")

    def test_div_method(self):
        self.defined_method_test("div", "div-method")

    def test_idiv_method(self):
        self.defined_method_test("idiv", "idiv-method")

    def test_rdiv_method(self):
        self.defined_method_test("rdiv", "rdiv-method")

    def test_eq_and_hash_method(self):
        """Helper for verifying that a certain method is not defined."""
        node = astroid.extract_node(
            """
            class Foo(object):  #@
                def __eq__(self, other):
                    pass
                def __hash__(self):
                    pass"""
        )
        with self.assertNoMessages():
            self.checker.visit_classdef(node)

    def test_eq_and_hash_is_none(self):
        """Helper for verifying that a certain method is not defined."""
        node = astroid.extract_node(
            """
            class Foo(object):  #@
                def __eq__(self, other):
                    pass
                __hash__ = None"""
        )
        with self.assertNoMessages():
            self.checker.visit_classdef(node)

    def test_eq_without_hash_method(self):
        """Helper for verifying that a certain method is not defined."""
        node = astroid.extract_node(
            """
            class Foo(object):  #@
                def __eq__(self, other):
                    pass"""
        )
        message = testutils.Message("eq-without-hash", node=node)
        with self.assertAddsMessages(message):
            self.checker.visit_classdef(node)

    def test_relative_import(self):
        node = astroid.extract_node("import string  #@")
        message = testutils.Message("no-absolute-import", node=node)
        with self.assertAddsMessages(message):
            self.checker.visit_import(node)
        with self.assertNoMessages():
            # message should only be added once
            self.checker.visit_import(node)

    def test_relative_from_import(self):
        node = astroid.extract_node("from os import path  #@")
        message = testutils.Message("no-absolute-import", node=node)
        with self.assertAddsMessages(message):
            self.checker.visit_importfrom(node)
        with self.assertNoMessages():
            # message should only be added once
            self.checker.visit_importfrom(node)

    def test_absolute_import(self):
        module_import = astroid.parse(
            "from __future__ import absolute_import; import os"
        )
        module_from = astroid.parse(
            "from __future__ import absolute_import; from os import path"
        )
        with self.assertNoMessages():
            for module in (module_import, module_from):
                self.walk(module)

    def test_import_star_module_level(self):
        node = astroid.extract_node(
            """
        def test():
            from lala import * #@
        """
        )
        absolute = testutils.Message("no-absolute-import", node=node)
        star = testutils.Message("import-star-module-level", node=node)
        with self.assertAddsMessages(absolute, star):
            self.checker.visit_importfrom(node)

    def test_division(self):
        nodes = astroid.extract_node(
            """\
            from _unknown import a, b
            3 / 2  #@
            3 / int(a) #@
            int(a) / 3 #@
            a / b #@
            """
        )
        for node in nodes:
            message = testutils.Message("old-division", node=node)
            with self.assertAddsMessages(message):
                self.checker.visit_binop(node)

    def test_division_with_future_statement(self):
        module = astroid.parse("from __future__ import division; 3 / 2")
        with self.assertNoMessages():
            self.walk(module)

    def test_floor_division(self):
        node = astroid.extract_node(" 3 // 2  #@")
        with self.assertNoMessages():
            self.checker.visit_binop(node)

    def test_division_by_float(self):
        nodes = astroid.extract_node(
            """\
            3.0 / 2  #@
            3 / 2.0  #@
            3 / float(a)  #@
            float(a) / 3  #@
            """
        )
        with self.assertNoMessages():
            for node in nodes:
                self.checker.visit_binop(node)

    def test_division_different_types(self):
        nodes = astroid.extract_node(
            """
        class A:
            pass
        A / 2 #@
        A() / 2 #@
        "a" / "a" #@
        class Path:
            def __div__(self, other):
                return 42
        Path() / 24 #@
        """
        )
        for node in nodes:
            with self.assertNoMessages():
                self.checker.visit_binop(node)

    def test_dict_iter_method(self):
        for meth in ("keys", "values", "items"):
            node = astroid.extract_node("x.iter%s()  #@" % meth)
            message = testutils.Message("dict-iter-method", node=node)
            with self.assertAddsMessages(message):
                self.checker.visit_call(node)

    def test_dict_iter_method_on_dict(self):
        nodes = astroid.extract_node(
            """
        from collections import defaultdict
        {}.iterkeys() #@
        defaultdict(list).iterkeys() #@
        class Someclass(dict):
            pass
        Someclass().iterkeys() #@

        # Emits even though we are not sure they are dicts
        x.iterkeys() #@

        def func(x):
            x.iterkeys() #@
        """
        )
        for node in nodes:
            message = testutils.Message("dict-iter-method", node=node)
            with self.assertAddsMessages(message):
                self.checker.visit_call(node)

    def test_dict_not_iter_method(self):
        arg_node = astroid.extract_node("x.iterkeys(x)  #@")
        stararg_node = astroid.extract_node("x.iterkeys(*x)  #@")
        kwarg_node = astroid.extract_node("x.iterkeys(y=x)  #@")
        with self.assertNoMessages():
            for node in (arg_node, stararg_node, kwarg_node):
                self.checker.visit_call(node)

    def test_dict_view_method(self):
        for meth in ("keys", "values", "items"):
            node = astroid.extract_node("x.view%s()  #@" % meth)
            message = testutils.Message("dict-view-method", node=node)
            with self.assertAddsMessages(message):
                self.checker.visit_call(node)

    def test_dict_viewkeys(self):
        nodes = astroid.extract_node(
            """
        from collections import defaultdict
        {}.viewkeys() #@
        defaultdict(list).viewkeys() #@
        class Someclass(dict):
            pass
        Someclass().viewkeys() #@

        # Emits even though they might not be dicts
        x.viewkeys() #@

        def func(x):
            x.viewkeys() #@
        """
        )
        for node in nodes:
            message = testutils.Message("dict-view-method", node=node)
            with self.assertAddsMessages(message):
                self.checker.visit_call(node)

    def test_next_method(self):
        node = astroid.extract_node("x.next()  #@")
        message = testutils.Message("next-method-called", node=node)
        with self.assertAddsMessages(message):
            self.checker.visit_call(node)

    def test_not_next_method(self):
        arg_node = astroid.extract_node("x.next(x)  #@")
        stararg_node = astroid.extract_node("x.next(*x)  #@")
        kwarg_node = astroid.extract_node("x.next(y=x)  #@")
        with self.assertNoMessages():
            for node in (arg_node, stararg_node, kwarg_node):
                self.checker.visit_call(node)

    def test_metaclass_assignment(self):
        node = astroid.extract_node(
            """
            class Foo(object):  #@
                __metaclass__ = type"""
        )
        message = testutils.Message("metaclass-assignment", node=node)
        with self.assertAddsMessages(message):
            self.checker.visit_classdef(node)

    def test_metaclass_global_assignment(self):
        module = astroid.parse("__metaclass__ = type")
        with self.assertNoMessages():
            self.walk(module)

    def test_xreadlines_attribute(self):
        node = astroid.extract_node(
            """
        f.xreadlines #@
        """
        )
        message = testutils.Message("xreadlines-attribute", node=node)
        with self.assertAddsMessages(message):
            self.checker.visit_attribute(node)

    def test_exception_message_attribute(self):
        node = astroid.extract_node(
            """
        try:
            raise Exception("test")
        except Exception as e:
            e.message #@
        """
        )
        message = testutils.Message("exception-message-attribute", node=node)
        with self.assertAddsMessages(message):
            self.checker.visit_attribute(node)

    def test_normal_message_attribute(self):
        node = astroid.extract_node(
            """
        e.message #@
        """
        )
        with self.assertNoMessages():
            self.checker.visit_attribute(node)

    def test_invalid_codec(self):
        node = astroid.extract_node('foobar.encode("hex") #@')
        message = testutils.Message("invalid-str-codec", node=node)
        with self.assertAddsMessages(message):
            self.checker.visit_call(node)

    def test_valid_codec(self):
        node = astroid.extract_node('foobar.encode("ascii", "ignore")  #@')
        with self.assertNoMessages():
            self.checker.visit_call(node)

    def test_visit_call_with_kwarg(self):
        node = astroid.extract_node('foobar.raz(encoding="hex")  #@')
        with self.assertNoMessages():
            self.checker.visit_call(node)

    def test_invalid_open_codec(self):
        node = astroid.extract_node('open(foobar, encoding="hex") #@')
        message = testutils.Message("invalid-str-codec", node=node)
        with self.assertAddsMessages(message):
            self.checker.visit_call(node)

    def test_valid_open_codec(self):
        node = astroid.extract_node('open(foobar, encoding="palmos") #@')
        with self.assertNoMessages():
            self.checker.visit_call(node)

    def test_using_cmp_argument(self):
        nodes = astroid.extract_node(
            """
        [].sort(cmp=lambda x: x) #@
        a = list(range(x))
        a.sort(cmp=lambda x: x) #@

        sorted([], cmp=lambda x: x) #@
        """
        )
        for node in nodes:
            message = testutils.Message("using-cmp-argument", node=node)
            with self.assertAddsMessages(message):
                self.checker.visit_call(node)

    def test_sys_maxint(self):
        node = astroid.extract_node(
            """
        import sys
        sys.maxint #@
        """
        )
        message = testutils.Message("sys-max-int", node=node)
        with self.assertAddsMessages(message):
            self.checker.visit_attribute(node)

    def test_itertools_izip(self):
        node = astroid.extract_node(
            """
        from itertools import izip #@
        """
        )
        absolute_import_message = testutils.Message("no-absolute-import", node=node)
        message = testutils.Message("deprecated-itertools-function", node=node)
        with self.assertAddsMessages(absolute_import_message, message):
            self.checker.visit_importfrom(node)

    def test_deprecated_types_fields(self):
        node = astroid.extract_node(
            """
        from types import StringType #@
        """
        )
        absolute_import_message = testutils.Message("no-absolute-import", node=node)
        message = testutils.Message("deprecated-types-field", node=node)
        with self.assertAddsMessages(absolute_import_message, message):
            self.checker.visit_importfrom(node)

    def test_sys_maxint_imort_from(self):
        node = astroid.extract_node(
            """
        from sys import maxint #@
        """
        )
        absolute_import_message = testutils.Message("no-absolute-import", node=node)
        message = testutils.Message("sys-max-int", node=node)
        with self.assertAddsMessages(absolute_import_message, message):
            self.checker.visit_importfrom(node)

    def test_object_maxint(self):
        node = astroid.extract_node(
            """
        sys = object()
        sys.maxint #@
        """
        )
        with self.assertNoMessages():
            self.checker.visit_attribute(node)

    def test_bad_import(self):
        node = astroid.extract_node(
            """
        import urllib2, sys #@
        """
        )
        absolute_import_message = testutils.Message("no-absolute-import", node=node)
        message = testutils.Message("bad-python3-import", node=node)
        with self.assertAddsMessages(absolute_import_message, message):
            self.checker.visit_import(node)

    def test_bad_import_turtle(self):
        node = astroid.extract_node(
            """
        import turtle #@
        turtle.Turtle()
        """
        )
        absolute_import_message = testutils.Message("no-absolute-import", node=node)
        with self.assertAddsMessages(absolute_import_message):
            self.checker.visit_import(node)

    def test_bad_import_dbm(self):
        node = astroid.extract_node(
            """
        from dbm import open as open_ #@
        open_("dummy.db")
        """
        )
        absolute_import_message = testutils.Message("no-absolute-import", node=node)
        with self.assertAddsMessages(absolute_import_message):
            self.checker.visit_importfrom(node)

    def test_bad_import_conditional(self):
        node = astroid.extract_node(
            """
        import six
        if six.PY2:
            import urllib2 #@
        """
        )
        absolute_import_message = testutils.Message("no-absolute-import", node=node)
        with self.assertAddsMessages(absolute_import_message):
            self.checker.visit_import(node)

    def test_bad_import_try_except_handler(self):
        node = astroid.extract_node(
            """
        try:
            from hashlib import sha
        except:
            import sha #@
        """
        )
        absolute_import_message = testutils.Message("no-absolute-import", node=node)
        with self.assertAddsMessages(absolute_import_message):
            self.checker.visit_import(node)

    def test_bad_import_try(self):
        node = astroid.extract_node(
            """
        try:
            import md5  #@
        except:
            from hashlib import md5
        finally:
            pass
        """
        )
        absolute_import_message = testutils.Message("no-absolute-import", node=node)
        with self.assertAddsMessages(absolute_import_message):
            self.checker.visit_import(node)

    def test_bad_import_try_finally(self):
        node = astroid.extract_node(
            """
        try:
            import Queue  #@
        finally:
            import queue
        """
        )
        absolute_import_message = testutils.Message("no-absolute-import", node=node)
        message = testutils.Message("bad-python3-import", node=node)
        with self.assertAddsMessages(absolute_import_message, message):
            self.checker.visit_import(node)

    def test_bad_import_from(self):
        node = astroid.extract_node(
            """
        from cStringIO import StringIO #@
        """
        )
        absolute_import_message = testutils.Message("no-absolute-import", node=node)
        message = testutils.Message("bad-python3-import", node=node)
        with self.assertAddsMessages(absolute_import_message, message):
            self.checker.visit_importfrom(node)

    def test_bad_string_attribute(self):
        node = astroid.extract_node(
            """
        import string
        string.maketrans #@
        """
        )
        message = testutils.Message("deprecated-string-function", node=node)
        with self.assertAddsMessages(message):
            self.checker.visit_attribute(node)

    def test_bad_operator_attribute(self):
        node = astroid.extract_node(
            """
        import operator
        operator.div #@
        """
        )
        message = testutils.Message("deprecated-operator-function", node=node)
        with self.assertAddsMessages(message):
            self.checker.visit_attribute(node)

    def test_comprehension_escape(self):
        assign, escaped_node = astroid.extract_node(
            """
        a = [i for i in range(10)] #@
        i #@
        """
        )
        good_module = astroid.parse(
            """
        {c for c in range(10)} #@
        {j:j for j in range(10)} #@
        [image_child] = [x for x in range(10)]
        thumbnail = func(__(image_child))
        """
        )
        message = testutils.Message("comprehension-escape", node=escaped_node)
        with self.assertAddsMessages(message):
            self.checker.visit_listcomp(assign.value)

        with self.assertNoMessages():
            self.walk(good_module)

    def test_comprehension_escape_newly_introduced(self):
        node = astroid.extract_node(
            """
        [i for i in range(3)]
        for i in range(3):
            i
        """
        )
        with self.assertNoMessages():
            self.walk(node)

    def test_exception_escape(self):
        module = astroid.parse(
            """
        try: 1/0
        except ValueError as exc:
            pass
        exc #@
        try:
           2/0
        except (ValueError, TypeError) as exc:
           exc = 2
        exc #@
        try:
           2/0
        except (ValueError, TypeError): #@
           exc = 2
        exc #@
        try:
           1/0
        except (ValueError, TypeError) as exc:
           foo(bar for bar in exc.bar)
        """
        )
        message = testutils.Message("exception-escape", node=module.body[1].value)
        with self.assertAddsMessages(message):
            self.checker.visit_excepthandler(module.body[0].handlers[0])
        with self.assertNoMessages():
            self.checker.visit_excepthandler(module.body[2].handlers[0])
            self.checker.visit_excepthandler(module.body[4].handlers[0])
            self.checker.visit_excepthandler(module.body[6].handlers[0])

    def test_bad_sys_attribute(self):
        node = astroid.extract_node(
            """
        import sys
        sys.exc_clear #@
        """
        )
        message = testutils.Message("deprecated-sys-function", node=node)
        with self.assertAddsMessages(message):
            self.checker.visit_attribute(node)

    def test_bad_urllib_attribute(self):
        nodes = astroid.extract_node(
            """
        import urllib
        urllib.addbase #@
        urllib.splithost #@
        urllib.urlretrieve #@
        urllib.urlopen #@
        urllib.urlencode #@
        """
        )
        for node in nodes:
            message = testutils.Message("deprecated-urllib-function", node=node)
            with self.assertAddsMessages(message):
                self.checker.visit_attribute(node)

    def test_ok_string_attribute(self):
        node = astroid.extract_node(
            """
        import string
        string.ascii_letters #@
        """
        )
        with self.assertNoMessages():
            self.checker.visit_attribute(node)

    def test_bad_string_call(self):
        node = astroid.extract_node(
            """
        import string
        string.upper("hello world") #@
        """
        )
        message = testutils.Message("deprecated-string-function", node=node)
        with self.assertAddsMessages(message):
            self.checker.visit_call(node)

    def test_ok_shadowed_call(self):
        node = astroid.extract_node(
            """
        import six.moves.configparser
        six.moves.configparser.ConfigParser() #@
        """
        )
        with self.assertNoMessages():
            self.checker.visit_call(node)

    def test_ok_string_call(self):
        node = astroid.extract_node(
            """
        import string
        string.Foramtter() #@
        """
        )
        with self.assertNoMessages():
            self.checker.visit_call(node)

    def test_bad_string_import_from(self):
        node = astroid.extract_node(
            """
         from string import atoi #@
         """
        )
        absolute_import_message = testutils.Message("no-absolute-import", node=node)
        message = testutils.Message("deprecated-string-function", node=node)
        with self.assertAddsMessages(absolute_import_message, message):
            self.checker.visit_importfrom(node)

    def test_ok_string_import_from(self):
        node = astroid.extract_node(
            """
         from string import digits #@
         """
        )
        absolute_import_message = testutils.Message("no-absolute-import", node=node)
        with self.assertAddsMessages(absolute_import_message):
            self.checker.visit_importfrom(node)

    def test_bad_str_translate_call_string_literal(self):
        node = astroid.extract_node(
            """
         foobar.translate(None, 'abc123') #@
         """
        )
        message = testutils.Message(
            "deprecated-str-translate-call", node=node, confidence=INFERENCE_FAILURE
        )
        with self.assertAddsMessages(message):
            self.checker.visit_call(node)

    def test_bad_str_translate_call_variable(self):
        node = astroid.extract_node(
            """
         def raz(foobar):
           foobar.translate(None, 'hello') #@
         """
        )
        message = testutils.Message(
            "deprecated-str-translate-call", node=node, confidence=INFERENCE_FAILURE
        )
        with self.assertAddsMessages(message):
            self.checker.visit_call(node)

    def test_bad_str_translate_call_infer_str(self):
        node = astroid.extract_node(
            """
         foobar = "hello world"
         foobar.translate(None, foobar) #@
         """
        )
        message = testutils.Message(
            "deprecated-str-translate-call", node=node, confidence=INFERENCE
        )
        with self.assertAddsMessages(message):
            self.checker.visit_call(node)

    def test_ok_str_translate_call_integer(self):
        node = astroid.extract_node(
            """
         foobar.translate(None, 33) #@
         """
        )
        with self.assertNoMessages():
            self.checker.visit_call(node)

    def test_ok_str_translate_call_keyword(self):
        node = astroid.extract_node(
            """
         foobar.translate(None, 'foobar', raz=33) #@
         """
        )
        with self.assertNoMessages():
            self.checker.visit_call(node)

    def test_ok_str_translate_call_not_str(self):
        node = astroid.extract_node(
            """
         foobar = {}
         foobar.translate(None, 'foobar') #@
         """
        )
        with self.assertNoMessages():
            self.checker.visit_call(node)

    def test_non_py2_conditional(self):
        code = """
        from __future__ import absolute_import
        import sys
        x = {}
        if sys.maxsize:
            x.iterkeys()  #@
        """
        node = astroid.extract_node(code)
        module = node.parent.parent
        message = testutils.Message("dict-iter-method", node=node)
        with self.assertAddsMessages(message):
            self.walk(module)

    def test_six_conditional(self):
        code = """
        from __future__ import absolute_import
        import six
        x = {}
        if six.PY2:
            x.iterkeys()
        """
        module = astroid.parse(code)
        with self.assertNoMessages():
            self.walk(module)

    def test_versioninfo_conditional(self):
        code = """
        from __future__ import absolute_import
        import sys
        x = {}
        if sys.version_info[0] == 2:
            x.iterkeys()
        """
        module = astroid.parse(code)
        with self.assertNoMessages():
            self.walk(module)

    def test_versioninfo_tuple_conditional(self):
        code = """
        from __future__ import absolute_import
        import sys
        x = {}
        if sys.version_info == (2, 7):
            x.iterkeys()
        """
        module = astroid.parse(code)
        with self.assertNoMessages():
            self.walk(module)

    def test_six_ifexp_conditional(self):
        code = """
        from __future__ import absolute_import
        import six
        import string
        string.translate if six.PY2 else None
        """
        module = astroid.parse(code)
        with self.assertNoMessages():
            self.walk(module)

    def test_next_defined(self):
        node = astroid.extract_node(
            """
            class Foo(object):
                def next(self):  #@
                    pass"""
        )
        message = testutils.Message("next-method-defined", node=node)
        with self.assertAddsMessages(message):
            self.checker.visit_functiondef(node)

    def test_next_defined_too_many_args(self):
        node = astroid.extract_node(
            """
            class Foo(object):
                def next(self, foo=None):  #@
                    pass"""
        )
        with self.assertNoMessages():
            self.checker.visit_functiondef(node)

    def test_next_defined_static_method_too_many_args(self):
        node = astroid.extract_node(
            """
            class Foo(object):
                @staticmethod
                def next(self):  #@
                    pass"""
        )
        with self.assertNoMessages():
            self.checker.visit_functiondef(node)

    def test_next_defined_static_method(self):
        node = astroid.extract_node(
            """
            class Foo(object):
                @staticmethod
                def next():  #@
                    pass"""
        )
        message = testutils.Message("next-method-defined", node=node)
        with self.assertAddsMessages(message):
            self.checker.visit_functiondef(node)

    def test_next_defined_class_method(self):
        node = astroid.extract_node(
            """
            class Foo(object):
                @classmethod
                def next(cls):  #@
                    pass"""
        )
        message = testutils.Message("next-method-defined", node=node)
        with self.assertAddsMessages(message):
            self.checker.visit_functiondef(node)