Full flake8 3.5 cleanup

This commit is contained in:
Daniele Varrazzo 2018-10-23 00:39:14 +01:00
parent 1bb3d5cfe2
commit 05f9e231a0
30 changed files with 77 additions and 36 deletions

View File

@ -72,8 +72,8 @@ _ext.register_adapter(type(None), _ext.NoneAdapter)
# Register the Decimal adapter here instead of in the C layer. # Register the Decimal adapter here instead of in the C layer.
# This way a new class is registered for each sub-interpreter. # This way a new class is registered for each sub-interpreter.
# See ticket #52 # See ticket #52
from decimal import Decimal from decimal import Decimal # noqa
from psycopg2._psycopg import Decimal as Adapter from psycopg2._psycopg import Decimal as Adapter # noqa
_ext.register_adapter(Decimal, Adapter) _ext.register_adapter(Decimal, Adapter)
del Decimal, Adapter del Decimal, Adapter

View File

@ -506,10 +506,10 @@ class NumberRangeAdapter(RangeAdapter):
return ("'%s%s,%s%s'" % ( return ("'%s%s,%s%s'" % (
r._bounds[0], lower, upper, r._bounds[1])).encode('ascii') r._bounds[0], lower, upper, r._bounds[1])).encode('ascii')
# TODO: probably won't work with infs, nans and other tricky cases. # TODO: probably won't work with infs, nans and other tricky cases.
register_adapter(NumericRange, NumberRangeAdapter) register_adapter(NumericRange, NumberRangeAdapter)
# Register globally typecasters and adapters for builtin range types. # Register globally typecasters and adapters for builtin range types.
# note: the adapter is registered more than once, but this is harmless. # note: the adapter is registered more than once, but this is harmless.

View File

@ -31,10 +31,7 @@ import time as _time
import re as _re import re as _re
from collections import namedtuple, OrderedDict from collections import namedtuple, OrderedDict
try:
import logging as _logging import logging as _logging
except:
_logging = None
import psycopg2 import psycopg2
from psycopg2 import extensions as _ext from psycopg2 import extensions as _ext
@ -189,7 +186,7 @@ class DictRow(list):
def get(self, x, default=None): def get(self, x, default=None):
try: try:
return self[x] return self[x]
except: except Exception:
return default return default
def copy(self): def copy(self):

View File

@ -138,7 +138,7 @@ class AbstractConnectionPool(object):
for conn in self._pool + list(self._used.values()): for conn in self._pool + list(self._used.values()):
try: try:
conn.close() conn.close()
except: except Exception:
pass pass
self.closed = True self.closed = True

View File

@ -132,6 +132,7 @@ class LocalTimezone(datetime.tzinfo):
tt = time.localtime(stamp) tt = time.localtime(stamp)
return tt.tm_isdst > 0 return tt.tm_isdst > 0
LOCAL = LocalTimezone() LOCAL = LocalTimezone()
# TODO: pre-generate some interesting time zones? # TODO: pre-generate some interesting time zones?

View File

@ -460,5 +460,6 @@ class AsyncTests(ConnectingTestCase):
def test_suite(): def test_suite():
return unittest.TestLoader().loadTestsFromName(__name__) return unittest.TestLoader().loadTestsFromName(__name__)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()

View File

@ -218,5 +218,6 @@ class AsyncReplicationTest(ReplicationTestCase):
def test_suite(): def test_suite():
return unittest.TestLoader().loadTestsFromName(__name__) return unittest.TestLoader().loadTestsFromName(__name__)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()

View File

@ -42,5 +42,6 @@ class DateTimeAllocationBugTestCase(unittest.TestCase):
def test_suite(): def test_suite():
return unittest.TestLoader().loadTestsFromName(__name__) return unittest.TestLoader().loadTestsFromName(__name__)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()

View File

@ -114,5 +114,6 @@ class CancelTests(ConnectingTestCase):
def test_suite(): def test_suite():
return unittest.TestLoader().loadTestsFromName(__name__) return unittest.TestLoader().loadTestsFromName(__name__)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()

View File

@ -1845,5 +1845,6 @@ class TestConnectionInfo(ConnectingTestCase):
def test_suite(): def test_suite():
return unittest.TestLoader().loadTestsFromName(__name__) return unittest.TestLoader().loadTestsFromName(__name__)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()

View File

@ -140,7 +140,8 @@ class CopyTests(ConnectingTestCase):
about = abin.decode('latin1').replace('\\', '\\\\') about = abin.decode('latin1').replace('\\', '\\\\')
else: else:
abin = bytes(list(range(32, 127)) + list(range(160, 256))).decode('latin1') abin = bytes(list(range(32, 127))
+ list(range(160, 256))).decode('latin1')
about = abin.replace('\\', '\\\\') about = abin.replace('\\', '\\\\')
curs = self.conn.cursor() curs = self.conn.cursor()
@ -161,7 +162,8 @@ class CopyTests(ConnectingTestCase):
abin = ''.join(map(chr, range(32, 127) + range(160, 255))) abin = ''.join(map(chr, range(32, 127) + range(160, 255)))
about = abin.replace('\\', '\\\\') about = abin.replace('\\', '\\\\')
else: else:
abin = bytes(list(range(32, 127)) + list(range(160, 255))).decode('latin1') abin = bytes(list(range(32, 127))
+ list(range(160, 255))).decode('latin1')
about = abin.replace('\\', '\\\\').encode('latin1') about = abin.replace('\\', '\\\\').encode('latin1')
curs = self.conn.cursor() curs = self.conn.cursor()
@ -184,7 +186,8 @@ class CopyTests(ConnectingTestCase):
about = abin.replace('\\', '\\\\') about = abin.replace('\\', '\\\\')
else: else:
abin = bytes(list(range(32, 127)) + list(range(160, 256))).decode('latin1') abin = bytes(list(range(32, 127))
+ list(range(160, 256))).decode('latin1')
about = abin.replace('\\', '\\\\') about = abin.replace('\\', '\\\\')
import io import io
@ -381,5 +384,6 @@ decorate_all_tests(CopyTests, skip_copy_if_green)
def test_suite(): def test_suite():
return unittest.TestLoader().loadTestsFromName(__name__) return unittest.TestLoader().loadTestsFromName(__name__)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()

View File

@ -589,7 +589,10 @@ class CursorTests(ConnectingTestCase):
# psycopg2 noticing. # psycopg2 noticing.
control_conn = self.conn control_conn = self.conn
connect_func = self.connect connect_func = self.connect
wait_func = lambda conn: None
def wait_func(conn):
pass
self._test_external_close(control_conn, connect_func, wait_func) self._test_external_close(control_conn, connect_func, wait_func)
@skip_if_no_superuser @skip_if_no_superuser
@ -599,7 +602,10 @@ class CursorTests(ConnectingTestCase):
# Issue #443 is in the async code too. Since the fix is duplicated, # Issue #443 is in the async code too. Since the fix is duplicated,
# so is the test. # so is the test.
control_conn = self.conn control_conn = self.conn
connect_func = lambda: self.connect(async_=True)
def connect_func():
return self.connect(async_=True)
wait_func = psycopg2.extras.wait_select wait_func = psycopg2.extras.wait_select
self._test_external_close(control_conn, connect_func, wait_func) self._test_external_close(control_conn, connect_func, wait_func)
@ -648,5 +654,6 @@ class CursorTests(ConnectingTestCase):
def test_suite(): def test_suite():
return unittest.TestLoader().loadTestsFromName(__name__) return unittest.TestLoader().loadTestsFromName(__name__)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()

View File

@ -713,5 +713,6 @@ class FixedOffsetTimezoneTests(unittest.TestCase):
def test_suite(): def test_suite():
return unittest.TestLoader().loadTestsFromName(__name__) return unittest.TestLoader().loadTestsFromName(__name__)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()

View File

@ -68,5 +68,6 @@ class ErrocodeTests(ConnectingTestCase):
def test_suite(): def test_suite():
return unittest.TestLoader().loadTestsFromName(__name__) return unittest.TestLoader().loadTestsFromName(__name__)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()

View File

@ -66,5 +66,6 @@ class ErrorsTests(ConnectingTestCase):
def test_suite(): def test_suite():
return unittest.TestLoader().loadTestsFromName(__name__) return unittest.TestLoader().loadTestsFromName(__name__)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()

View File

@ -582,5 +582,6 @@ class NamedTupleCursorTest(ConnectingTestCase):
def test_suite(): def test_suite():
return unittest.TestLoader().loadTestsFromName(__name__) return unittest.TestLoader().loadTestsFromName(__name__)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()

View File

@ -238,5 +238,6 @@ testutils.decorate_all_tests(TestExecuteValues,
def test_suite(): def test_suite():
return unittest.TestLoader().loadTestsFromName(__name__) return unittest.TestLoader().loadTestsFromName(__name__)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()

View File

@ -206,5 +206,6 @@ class CallbackErrorTestCase(ConnectingTestCase):
def test_suite(): def test_suite():
return unittest.TestLoader().loadTestsFromName(__name__) return unittest.TestLoader().loadTestsFromName(__name__)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()

View File

@ -125,6 +125,7 @@ class NetworkingTestCase(testutils.ConnectingTestCase):
cur.execute("select %s", [ip.ip_network('::ffff:102:300/128')]) cur.execute("select %s", [ip.ip_network('::ffff:102:300/128')])
self.assertEquals(cur.fetchone()[0], '::ffff:102:300/128') self.assertEquals(cur.fetchone()[0], '::ffff:102:300/128')
testutils.decorate_all_tests(NetworkingTestCase, skip_if_no_ipaddress) testutils.decorate_all_tests(NetworkingTestCase, skip_if_no_ipaddress)

View File

@ -44,6 +44,7 @@ def skip_if_no_lo(f):
return skip_if_no_lo_ return skip_if_no_lo_
skip_lo_if_green = skip_if_green("libpq doesn't support LO in async mode") skip_lo_if_green = skip_if_green("libpq doesn't support LO in async mode")
@ -397,6 +398,7 @@ class LargeObjectTests(LargeObjectTestCase):
lo = self.conn.lobject(lobject_factory=lobject_subclass) lo = self.conn.lobject(lobject_factory=lobject_subclass)
self.assert_(isinstance(lo, lobject_subclass)) self.assert_(isinstance(lo, lobject_subclass))
decorate_all_tests(LargeObjectTests, skip_if_no_lo, skip_lo_if_green) decorate_all_tests(LargeObjectTests, skip_if_no_lo, skip_lo_if_green)
@ -453,6 +455,7 @@ class LargeObjectTruncateTests(LargeObjectTestCase):
self.assertRaises(psycopg2.ProgrammingError, lo.truncate) self.assertRaises(psycopg2.ProgrammingError, lo.truncate)
decorate_all_tests(LargeObjectTruncateTests, decorate_all_tests(LargeObjectTruncateTests,
skip_if_no_lo, skip_lo_if_green, skip_if_no_truncate) skip_if_no_lo, skip_lo_if_green, skip_if_no_truncate)
@ -491,6 +494,7 @@ class LargeObject64Tests(LargeObjectTestCase):
self.assertEqual(lo.seek(length, 0), length) self.assertEqual(lo.seek(length, 0), length)
self.assertEqual(lo.tell(), length) self.assertEqual(lo.tell(), length)
decorate_all_tests(LargeObject64Tests, decorate_all_tests(LargeObject64Tests,
skip_if_no_lo, skip_lo_if_green, skip_if_no_truncate, skip_if_no_lo64) skip_if_no_lo, skip_lo_if_green, skip_if_no_truncate, skip_if_no_lo64)
@ -522,6 +526,7 @@ class LargeObjectNot64Tests(LargeObjectTestCase):
(OverflowError, psycopg2.InterfaceError, psycopg2.NotSupportedError), (OverflowError, psycopg2.InterfaceError, psycopg2.NotSupportedError),
lo.truncate, length) lo.truncate, length)
decorate_all_tests(LargeObjectNot64Tests, decorate_all_tests(LargeObjectNot64Tests,
skip_if_no_lo, skip_lo_if_green, skip_if_no_truncate, skip_if_lo64) skip_if_no_lo, skip_lo_if_green, skip_if_no_truncate, skip_if_lo64)
@ -529,5 +534,6 @@ decorate_all_tests(LargeObjectNot64Tests,
def test_suite(): def test_suite():
return unittest.TestLoader().loadTestsFromName(__name__) return unittest.TestLoader().loadTestsFromName(__name__)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()

View File

@ -348,5 +348,6 @@ class TestVersionDiscovery(unittest.TestCase):
def test_suite(): def test_suite():
return unittest.TestLoader().loadTestsFromName(__name__) return unittest.TestLoader().loadTestsFromName(__name__)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()

View File

@ -55,11 +55,9 @@ class Psycopg2Tests(dbapi20.DatabaseAPI20Test):
r = cur.fetchall() r = cur.fetchall()
self.assertEqual(len(r), 1, 'callproc produced no result set') self.assertEqual(len(r), 1, 'callproc produced no result set')
self.assertEqual(len(r[0]), 1, self.assertEqual(len(r[0]), 1,
'callproc produced invalid result set' 'callproc produced invalid result set')
)
self.assertEqual(r[0][0], 'foo', self.assertEqual(r[0][0], 'foo',
'callproc produced invalid results' 'callproc produced invalid results')
)
finally: finally:
con.close() con.close()
@ -78,11 +76,13 @@ class Psycopg2TPCTests(dbapi20_tpc.TwoPhaseCommitTests, unittest.TestCase):
def connect(self): def connect(self):
return psycopg2.connect(dsn=dsn) return psycopg2.connect(dsn=dsn)
decorate_all_tests(Psycopg2TPCTests, skip_if_tpc_disabled) decorate_all_tests(Psycopg2TPCTests, skip_if_tpc_disabled)
def test_suite(): def test_suite():
return unittest.TestLoader().loadTestsFromName(__name__) return unittest.TestLoader().loadTestsFromName(__name__)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View File

@ -126,7 +126,8 @@ class QuotingTestCase(ConnectingTestCase):
if sys.version_info[0] < 3: if sys.version_info[0] < 3:
data = ''.join(map(chr, range(32, 127) + range(160, 256))) data = ''.join(map(chr, range(32, 127) + range(160, 256)))
else: else:
data = bytes(list(range(32, 127)) + list(range(160, 256))).decode('latin1') data = bytes(list(range(32, 127))
+ list(range(160, 256))).decode('latin1')
# as string # as string
curs.execute("SELECT %s::text;", (data,)) curs.execute("SELECT %s::text;", (data,))
@ -150,7 +151,8 @@ class QuotingTestCase(ConnectingTestCase):
if sys.version_info[0] < 3: if sys.version_info[0] < 3:
data = ''.join(map(chr, range(32, 127) + range(128, 256))) data = ''.join(map(chr, range(32, 127) + range(128, 256)))
else: else:
data = bytes(list(range(32, 127)) + list(range(128, 256))).decode('koi8_r') data = bytes(list(range(32, 127))
+ list(range(128, 256))).decode('koi8_r')
# as string # as string
curs.execute("SELECT %s::text;", (data,)) curs.execute("SELECT %s::text;", (data,))
@ -252,5 +254,6 @@ class TestStringAdapter(ConnectingTestCase):
def test_suite(): def test_suite():
return unittest.TestLoader().loadTestsFromName(__name__) return unittest.TestLoader().loadTestsFromName(__name__)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()

View File

@ -412,5 +412,6 @@ class ValuesTest(ConnectingTestCase):
def test_suite(): def test_suite():
return unittest.TestLoader().loadTestsFromName(__name__) return unittest.TestLoader().loadTestsFromName(__name__)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()

View File

@ -249,5 +249,6 @@ class QueryCancellationTests(ConnectingTestCase):
def test_suite(): def test_suite():
return unittest.TestLoader().loadTestsFromName(__name__) return unittest.TestLoader().loadTestsFromName(__name__)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()

View File

@ -554,11 +554,13 @@ def skip_if_cant_cast(f):
return skip_if_cant_cast_ return skip_if_cant_cast_
decorate_all_tests(ByteaParserTest, skip_if_cant_cast) decorate_all_tests(ByteaParserTest, skip_if_cant_cast)
def test_suite(): def test_suite():
return unittest.TestLoader().loadTestsFromName(__name__) return unittest.TestLoader().loadTestsFromName(__name__)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()

View File

@ -123,6 +123,7 @@ class TypesExtrasTests(ConnectingTestCase):
s = self.execute("""SELECT '{"(1,2)","(3,4)"}' AS foo""") s = self.execute("""SELECT '{"(1,2)","(3,4)"}' AS foo""")
self.failUnless(s == """{"(1,2)","(3,4)"}""") self.failUnless(s == """{"(1,2)","(3,4)"}""")
def skip_if_no_hstore(f): def skip_if_no_hstore(f):
@wraps(f) @wraps(f)
def skip_if_no_hstore_(self): def skip_if_no_hstore_(self):
@ -1137,6 +1138,7 @@ class JsonbTestCase(ConnectingTestCase):
curs.execute("""select NULL::jsonb[]""") curs.execute("""select NULL::jsonb[]""")
self.assertEqual(curs.fetchone()[0], None) self.assertEqual(curs.fetchone()[0], None)
decorate_all_tests(JsonbTestCase, skip_if_no_jsonb_type) decorate_all_tests(JsonbTestCase, skip_if_no_jsonb_type)
@ -1763,6 +1765,7 @@ class RangeCasterTestCase(ConnectingTestCase):
for r in [ra1, ra2, rars2, rars3]: for r in [ra1, ra2, rars2, rars3]:
del ext.adapters[r.range, ext.ISQLQuote] del ext.adapters[r.range, ext.ISQLQuote]
decorate_all_tests(RangeCasterTestCase, skip_if_no_range) decorate_all_tests(RangeCasterTestCase, skip_if_no_range)

View File

@ -217,12 +217,13 @@ class WithCursorTestCase(WithTestCase):
@skip_before_postgres(8, 2) @skip_before_postgres(8, 2)
def test_named_with_noop(self): def test_named_with_noop(self):
with self.conn.cursor('named') as cur: with self.conn.cursor('named'):
pass pass
def test_suite(): def test_suite():
return unittest.TestLoader().loadTestsFromName(__name__) return unittest.TestLoader().loadTestsFromName(__name__)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()

View File

@ -42,8 +42,8 @@ if sys.version_info[0] == 2:
unichr = unichr unichr = unichr
else: else:
# Python 3 # Python 3
from io import StringIO from io import StringIO # noqa
from importlib import reload from importlib import reload # noqa
long = int long = int
unichr = chr unichr = chr
@ -64,6 +64,7 @@ def assertDsnEqual(self, dsn1, dsn2, msg=None):
"""Check that two conninfo string have the same content""" """Check that two conninfo string have the same content"""
self.assertEqual(set(dsn1.split()), set(dsn2.split()), msg) self.assertEqual(set(dsn1.split()), set(dsn2.split()), msg)
unittest.TestCase.assertDsnEqual = assertDsnEqual unittest.TestCase.assertDsnEqual = assertDsnEqual
@ -363,6 +364,7 @@ def skip_if_green(reason):
return skip_if_green__ return skip_if_green__
return skip_if_green_ return skip_if_green_
skip_copy_if_green = skip_if_green("copy in async mode currently not supported") skip_copy_if_green = skip_if_green("copy in async mode currently not supported")

View File

@ -7,5 +7,5 @@ whitelist_externals = make
[flake8] [flake8]
max-line-length = 85 max-line-length = 85
ignore = E128, W503 ignore = E128, W503, E741
exclude = build, doc, sandbox, examples, tests/dbapi20.py exclude = build, doc, examples, tests/dbapi20.py