summaryrefslogtreecommitdiff
path: root/pylint/test/functional/unidiomatic_typecheck.py
blob: 008456b1ea68366de41a5525fdf9af61b26ade4a (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
"""Warnings for using type(x) == Y or type(x) is Y instead of isinstance(x, Y)."""
# pylint: disable=missing-docstring,expression-not-assigned,redefined-builtin,invalid-name

def simple_positives():
    type(42) is int # [unidiomatic-typecheck]
    type(42) is not int # [unidiomatic-typecheck]
    type(42) == int # [unidiomatic-typecheck]
    type(42) != int # [unidiomatic-typecheck]
    type(42) in [int] # [unidiomatic-typecheck]
    type(42) not in [int] # [unidiomatic-typecheck]

def simple_inference_positives():
    alias = type
    alias(42) is int # [unidiomatic-typecheck]
    alias(42) is not int # [unidiomatic-typecheck]
    alias(42) == int # [unidiomatic-typecheck]
    alias(42) != int # [unidiomatic-typecheck]
    alias(42) in [int] # [unidiomatic-typecheck]
    alias(42) not in [int] # [unidiomatic-typecheck]

def type_creation_negatives():
    type('Q', (object,), dict(a=1)) is int
    type('Q', (object,), dict(a=1)) is not int
    type('Q', (object,), dict(a=1)) == int
    type('Q', (object,), dict(a=1)) != int
    type('Q', (object,), dict(a=1)) in [int]
    type('Q', (object,), dict(a=1)) not in [int]

def invalid_type_call_negatives(**kwargs):
    type(bad=7) is int
    type(bad=7) is not int
    type(bad=7) == int
    type(bad=7) != int
    type(bad=7) in [int]
    type(bad=7) not in [int]
    type('bad', 7) is int
    type('bad', 7) is not int
    type('bad', 7) == int
    type('bad', 7) != int
    type('bad', 7) in [int]
    type('bad', 7) not in [int]
    type(**kwargs) is int
    type(**kwargs) is not int
    type(**kwargs) == int
    type(**kwargs) != int
    type(**kwargs) in [int]
    type(**kwargs) not in [int]

def local_var_shadowing_inference_negatives():
    type = lambda dummy: 7
    type(42) is int
    type(42) is not int
    type(42) == int
    type(42) != int
    type(42) in [int]
    type(42) not in [int]

def parameter_shadowing_inference_negatives(type):
    type(42) is int
    type(42) is not int
    type(42) == int
    type(42) != int
    type(42) in [int]
    type(42) not in [int]

def deliberate_subclass_check_negatives(b):
    type(42) is type(b)
    type(42) is not type(b)

def type_of_literals_positives(a):
    type(a) is type([]) # [unidiomatic-typecheck]
    type(a) is not type([]) # [unidiomatic-typecheck]
    type(a) is type({}) # [unidiomatic-typecheck]
    type(a) is not type({}) # [unidiomatic-typecheck]
    type(a) is type("") # [unidiomatic-typecheck]
    type(a) is not type("") # [unidiomatic-typecheck]