gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / resources-plugin / scripting / lib / pylint / test / functional / string_formatting.py @ 745
History | View | Annotate | Download (6.1 KB)
1 |
"""test for Python 3 string formatting error
|
---|---|
2 |
"""
|
3 |
# pylint: disable=too-few-public-methods, import-error, unused-argument, line-too-long, no-absolute-import
|
4 |
import os |
5 |
from missing import Missing |
6 |
|
7 |
__revision__ = 1
|
8 |
|
9 |
class Custom(object): |
10 |
""" Has a __getattr__ """
|
11 |
def __getattr__(self, _): |
12 |
return self |
13 |
|
14 |
class Test(object): |
15 |
""" test format attribute access """
|
16 |
custom = Custom() |
17 |
ids = [1, 2, 3, [4, 5, 6]] |
18 |
|
19 |
class Getitem(object): |
20 |
""" test custom getitem for lookup access """
|
21 |
def __getitem__(self, index): |
22 |
return 42 |
23 |
|
24 |
class ReturnYes(object): |
25 |
""" can't be properly infered """
|
26 |
missing = Missing() |
27 |
|
28 |
def log(message, message_type="error"): |
29 |
""" Test """
|
30 |
return message
|
31 |
|
32 |
def print_good(): |
33 |
""" Good format strings """
|
34 |
"{0} {1}".format(1, 2) |
35 |
"{0!r:20}".format("Hello") |
36 |
"{!r:20}".format("Hello") |
37 |
"{a!r:20}".format(a="Hello") |
38 |
"{pid}".format(pid=os.getpid())
|
39 |
str("{}").format(2) |
40 |
"{0.missing.length}".format(ReturnYes())
|
41 |
"{1.missing.length}".format(ReturnYes())
|
42 |
"{a.ids[3][1]}".format(a=Test())
|
43 |
"{a[0][0]}".format(a=[[1]]) |
44 |
"{[0][0]}".format({0: {0: 1}}) |
45 |
"{a.test}".format(a=Custom())
|
46 |
"{a.__len__}".format(a=[])
|
47 |
"{a.ids.__len__}".format(a=Test())
|
48 |
"{a[0]}".format(a=Getitem())
|
49 |
"{a[0][0]}".format(a=[Getitem()])
|
50 |
"{[0][0]}".format(["test"]) |
51 |
# these are skipped
|
52 |
"{0} {1}".format(*[1, 2]) |
53 |
"{a} {b}".format(**{'a': 1, 'b': 2}) |
54 |
"{a}".format(a=Missing())
|
55 |
|
56 |
def pprint_bad(): |
57 |
"""Test string format """
|
58 |
"{{}}".format(1) # [too-many-format-args] |
59 |
"{} {".format() # [bad-format-string] |
60 |
"{} }".format() # [bad-format-string] |
61 |
"{0} {}".format(1, 2) # [format-combined-specification] |
62 |
# +1: [missing-format-argument-key, unused-format-string-argument]
|
63 |
"{a} {b}".format(a=1, c=2) |
64 |
"{} {a}".format(1, 2) # [missing-format-argument-key] |
65 |
"{} {}".format(1) # [too-few-format-args] |
66 |
"{} {}".format(1, 2, 3) # [too-many-format-args] |
67 |
# +1: [missing-format-argument-key,missing-format-argument-key,missing-format-argument-key]
|
68 |
"{a} {b} {c}".format()
|
69 |
"{} {}".format(a=1, b=2) # [too-few-format-args] |
70 |
# +1: [missing-format-argument-key, missing-format-argument-key]
|
71 |
"{a} {b}".format(1, 2) |
72 |
"{0} {1} {a}".format(1, 2, 3) # [missing-format-argument-key] |
73 |
# +1: [missing-format-attribute]
|
74 |
"{a.ids.__len__.length}".format(a=Test())
|
75 |
"{a.ids[3][400]}".format(a=Test()) # [invalid-format-index] |
76 |
"{a.ids[3]['string']}".format(a=Test()) # [invalid-format-index] |
77 |
"{[0][1]}".format(["a"]) # [invalid-format-index] |
78 |
"{[0][0]}".format(((1, ))) # [invalid-format-index] |
79 |
# +1: [missing-format-argument-key, unused-format-string-argument]
|
80 |
"{b[0]}".format(a=23) |
81 |
"{a[0]}".format(a=object) # [invalid-format-index] |
82 |
log("{}".format(2, "info")) # [too-many-format-args] |
83 |
"{0.missing}".format(2) # [missing-format-attribute] |
84 |
"{0} {1} {2}".format(1, 2) # [too-few-format-args] |
85 |
"{0} {1}".format(1, 2, 3) # [too-many-format-args] |
86 |
"{0} {a}".format(a=4) # [too-few-format-args] |
87 |
"{[0]} {}".format([4]) # [too-few-format-args] |
88 |
"{[0]} {}".format([4], 5, 6) # [too-many-format-args] |
89 |
|
90 |
def good_issue288(*args, **kwargs): |
91 |
""" Test that using kwargs does not emit a false
|
92 |
positive.
|
93 |
"""
|
94 |
'Hello John Doe {0[0]}'.format(args)
|
95 |
'Hello {0[name]}'.format(kwargs)
|
96 |
|
97 |
def good_issue287(): |
98 |
""" Test that the string format checker skips
|
99 |
format nodes which don't have a string as a parent
|
100 |
(but a subscript, name etc).
|
101 |
"""
|
102 |
name = 'qwerty'
|
103 |
ret = {'comment': ''} |
104 |
ret['comment'] = 'MySQL grant {0} is set to be revoked' |
105 |
ret['comment'] = ret['comment'].format(name) |
106 |
return ret, name
|
107 |
|
108 |
def nested_issue294(): |
109 |
""" Test nested format fields. """
|
110 |
'{0:>{1}}'.format(42, 24) |
111 |
'{0:{a[1]}} {a}'.format(1, a=[1, 2]) |
112 |
'{:>{}}'.format(42, 24) |
113 |
'{0:>{1}}'.format(42) # [too-few-format-args] |
114 |
'{0:>{1}}'.format(42, 24, 54) # [too-many-format-args] |
115 |
'{0:{a[1]}}'.format(1) # [missing-format-argument-key] |
116 |
'{0:{a.x}}'.format(1, a=2) # [missing-format-attribute] |
117 |
|
118 |
def issue310(): |
119 |
""" Test a regression using duplicate manual position arguments. """
|
120 |
'{0} {1} {0}'.format(1, 2) |
121 |
'{0} {1} {0}'.format(1) # [too-few-format-args] |
122 |
|
123 |
def issue322(): |
124 |
""" Test a regression using mixed manual position arguments
|
125 |
and attribute access arguments.
|
126 |
"""
|
127 |
'{0}{1[FOO]}'.format(123, {'FOO': 456}) |
128 |
'{0}{1[FOO]}'.format(123, {'FOO': 456}, 321) # [too-many-format-args] |
129 |
'{0}{1[FOO]}'.format(123) # [too-few-format-args] |
130 |
|
131 |
def issue338(): |
132 |
"""
|
133 |
Check that using a namedtuple subclass doesn't crash when
|
134 |
trying to infer EmptyNodes (resulted after mocking the
|
135 |
members of namedtuples).
|
136 |
"""
|
137 |
from collections import namedtuple |
138 |
|
139 |
class Crash(namedtuple("C", "foo bar")): |
140 |
""" Looking for attributes in __str__ will crash,
|
141 |
because EmptyNodes can't be infered.
|
142 |
"""
|
143 |
def __str__(self): |
144 |
return "{0.foo}: {0.bar}".format(self) |
145 |
return Crash
|
146 |
|
147 |
def issue351(): |
148 |
"""
|
149 |
Check that the format method can be assigned to a variable, ie:
|
150 |
"""
|
151 |
fmt = 'test {} {}'.format
|
152 |
fmt('arg1') # [too-few-format-args] |
153 |
fmt('arg1', 'arg2') |
154 |
fmt('arg1', 'arg2', 'arg3') # [too-many-format-args] |
155 |
|
156 |
def issue373(): |
157 |
"""
|
158 |
Ignore any object coming from an argument.
|
159 |
"""
|
160 |
class SomeClass(object): |
161 |
""" empty docstring. """
|
162 |
def __init__(self, opts=None): |
163 |
self.opts = opts
|
164 |
|
165 |
def dunc(self, arg): |
166 |
"""Don't try to analyze this."""
|
167 |
return "A{0}{1}".format(arg, self.opts) |
168 |
|
169 |
def func(self): |
170 |
"""Don't try to analyze the following string."""
|
171 |
return 'AAA{0[iface]}BBB{0[port]}'.format(self.opts) |
172 |
|
173 |
return SomeClass
|
174 |
|
175 |
def issue_463(): |
176 |
"""
|
177 |
Mix positional arguments, `{0}`, with positional
|
178 |
arguments with attribute access, `{0.__x__}`.
|
179 |
"""
|
180 |
data = "{0.__class__.__name__}: {0}".format(42) |
181 |
data2 = "{0[0]}: {0}".format([1]) |
182 |
return (data, data2)
|
183 |
|