summaryrefslogtreecommitdiff
path: root/test/input/func_string_format_py27.py
blob: e2bfe1263cc1db926cb797cf48cf33239cd7ae31 (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
"""test for Python 3 string formatting error
"""
# pylint: disable=too-few-public-methods, import-error, unused-argument, star-args
import os
from missing import Missing

__revision__ = 1

class Custom(object):
    """ Has a __getattr__ """
    def __getattr__(self):
        return self

class Test(object):
    """ test format attribute access """
    custom = Custom()
    ids = [1, 2, 3, [4, 5, 6]]

class Getitem(object):
    """ test custom getitem for lookup access """
    def __getitem__(self, index):
        return 42

class ReturnYes(object):
    """ can't be properly infered """
    missing = Missing()

def log(message, message_type="error"):
    """ Test """
    return message

def print_good():
    """ Good format strings """
    print "{0} {1}".format(1, 2)
    print "{0!r:20}".format("Hello")
    print "{!r:20}".format("Hello")
    print "{a!r:20}".format(a="Hello")
    print "{pid}".format(pid=os.getpid())
    print str("{}").format(2)
    print "{0.missing.length}".format(ReturnYes())
    print "{1.missing.length}".format(ReturnYes())
    print "{a.ids[3][1]}".format(a=Test())
    print "{a[0][0]}".format(a=[[1]])
    print "{[0][0]}".format({0: {0: 1}})
    print "{a.test}".format(a=Custom())
    print "{a.__len__}".format(a=[])
    print "{a.ids.__len__}".format(a=Test())
    print "{a[0]}".format(a=Getitem())
    print "{a[0][0]}".format(a=[Getitem()])
    print "{[0][0]}".format(["test"])
    # these are skipped
    print "{0} {1}".format(*[1, 2])
    print "{a} {b}".format(**{'a': 1, 'b': 2})
    print "{a}".format(a=Missing())

def pprint_bad():
    """Test string format """
    print "{{}}".format(1)
    print "{} {".format()
    print "{} }".format()
    print "{0} {}".format(1, 2)
    print "{a} {b}".format(a=1, c=2)
    print "{} {a}".format(1, 2)
    print "{} {}".format(1)
    print "{} {}".format(1, 2, 3)
    print "{a} {b} {c}".format()
    print "{} {}".format(a=1, b=2)
    print "{a} {b}".format(1, 2)
    print "{0} {1} {a}".format(1, 2, 3)
    print "{a.ids.__len__.length}".format(a=Test())
    print "{a.ids[3][400]}".format(a=Test())
    print "{a.ids[3]['string']}".format(a=Test())
    print "{[0][1]}".format(["a"])
    print "{[0][0]}".format(((1, )))
    print "{b[0]}".format(a=23)
    print "{a[0]}".format(a=object)
    print log("{}".format(2, "info"))
    print "{0.missing}".format(2)
    print "{0} {1} {2}".format(1, 2)
    print "{0} {1}".format(1, 2, 3)

def good_issue288(*args, **kwargs):
    """ Test that using kwargs does not emit a false
    positive.
    """
    data = 'Hello John Doe {0[0]}'.format(args)
    print data
    return 'Hello {0[name]}'.format(kwargs)