From 05f9e231a0aafed57b1f38ad6a1e6a5838b05cc5 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Tue, 23 Oct 2018 00:39:14 +0100 Subject: [PATCH] Full flake8 3.5 cleanup --- lib/__init__.py | 4 ++-- lib/_range.py | 2 +- lib/extras.py | 7 ++----- lib/pool.py | 2 +- lib/tz.py | 1 + tests/test_async.py | 1 + tests/test_async_keyword.py | 1 + tests/test_bugX000.py | 1 + tests/test_cancel.py | 1 + tests/test_connection.py | 1 + tests/test_copy.py | 10 +++++++--- tests/test_cursor.py | 11 +++++++++-- tests/test_dates.py | 1 + tests/test_errcodes.py | 1 + tests/test_errors.py | 1 + tests/test_extras_dictcursor.py | 1 + tests/test_fast_executemany.py | 1 + tests/test_green.py | 1 + tests/test_ipaddress.py | 1 + tests/test_lobject.py | 6 ++++++ tests/test_module.py | 1 + tests/test_psycopg2_dbapi20.py | 20 ++++++++++---------- tests/test_quote.py | 7 +++++-- tests/test_sql.py | 1 + tests/test_transaction.py | 1 + tests/test_types_basic.py | 10 ++++++---- tests/test_types_extras.py | 5 ++++- tests/test_with.py | 3 ++- tests/testutils.py | 6 ++++-- tox.ini | 4 ++-- 30 files changed, 77 insertions(+), 36 deletions(-) diff --git a/lib/__init__.py b/lib/__init__.py index b88d59c7..1c1a9e02 100644 --- a/lib/__init__.py +++ b/lib/__init__.py @@ -72,8 +72,8 @@ _ext.register_adapter(type(None), _ext.NoneAdapter) # Register the Decimal adapter here instead of in the C layer. # This way a new class is registered for each sub-interpreter. # See ticket #52 -from decimal import Decimal -from psycopg2._psycopg import Decimal as Adapter +from decimal import Decimal # noqa +from psycopg2._psycopg import Decimal as Adapter # noqa _ext.register_adapter(Decimal, Adapter) del Decimal, Adapter diff --git a/lib/_range.py b/lib/_range.py index 1747b201..309b2745 100644 --- a/lib/_range.py +++ b/lib/_range.py @@ -506,10 +506,10 @@ class NumberRangeAdapter(RangeAdapter): return ("'%s%s,%s%s'" % ( r._bounds[0], lower, upper, r._bounds[1])).encode('ascii') + # TODO: probably won't work with infs, nans and other tricky cases. register_adapter(NumericRange, NumberRangeAdapter) - # Register globally typecasters and adapters for builtin range types. # note: the adapter is registered more than once, but this is harmless. diff --git a/lib/extras.py b/lib/extras.py index 4dea3f93..ade0869b 100644 --- a/lib/extras.py +++ b/lib/extras.py @@ -31,10 +31,7 @@ import time as _time import re as _re from collections import namedtuple, OrderedDict -try: - import logging as _logging -except: - _logging = None +import logging as _logging import psycopg2 from psycopg2 import extensions as _ext @@ -189,7 +186,7 @@ class DictRow(list): def get(self, x, default=None): try: return self[x] - except: + except Exception: return default def copy(self): diff --git a/lib/pool.py b/lib/pool.py index 734c136d..075e29df 100644 --- a/lib/pool.py +++ b/lib/pool.py @@ -138,7 +138,7 @@ class AbstractConnectionPool(object): for conn in self._pool + list(self._used.values()): try: conn.close() - except: + except Exception: pass self.closed = True diff --git a/lib/tz.py b/lib/tz.py index 97f3857c..02dc12aa 100644 --- a/lib/tz.py +++ b/lib/tz.py @@ -132,6 +132,7 @@ class LocalTimezone(datetime.tzinfo): tt = time.localtime(stamp) return tt.tm_isdst > 0 + LOCAL = LocalTimezone() # TODO: pre-generate some interesting time zones? diff --git a/tests/test_async.py b/tests/test_async.py index 066e352a..5e70337a 100755 --- a/tests/test_async.py +++ b/tests/test_async.py @@ -460,5 +460,6 @@ class AsyncTests(ConnectingTestCase): def test_suite(): return unittest.TestLoader().loadTestsFromName(__name__) + if __name__ == "__main__": unittest.main() diff --git a/tests/test_async_keyword.py b/tests/test_async_keyword.py index fa37500c..86a4ad21 100755 --- a/tests/test_async_keyword.py +++ b/tests/test_async_keyword.py @@ -218,5 +218,6 @@ class AsyncReplicationTest(ReplicationTestCase): def test_suite(): return unittest.TestLoader().loadTestsFromName(__name__) + if __name__ == "__main__": unittest.main() diff --git a/tests/test_bugX000.py b/tests/test_bugX000.py index fbd2a9f6..1ab3cfdc 100755 --- a/tests/test_bugX000.py +++ b/tests/test_bugX000.py @@ -42,5 +42,6 @@ class DateTimeAllocationBugTestCase(unittest.TestCase): def test_suite(): return unittest.TestLoader().loadTestsFromName(__name__) + if __name__ == "__main__": unittest.main() diff --git a/tests/test_cancel.py b/tests/test_cancel.py index 5e550aa6..d257f3d7 100755 --- a/tests/test_cancel.py +++ b/tests/test_cancel.py @@ -114,5 +114,6 @@ class CancelTests(ConnectingTestCase): def test_suite(): return unittest.TestLoader().loadTestsFromName(__name__) + if __name__ == "__main__": unittest.main() diff --git a/tests/test_connection.py b/tests/test_connection.py index 596ca8bc..ffc9887f 100755 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -1845,5 +1845,6 @@ class TestConnectionInfo(ConnectingTestCase): def test_suite(): return unittest.TestLoader().loadTestsFromName(__name__) + if __name__ == "__main__": unittest.main() diff --git a/tests/test_copy.py b/tests/test_copy.py index b16e1ddc..1eb10900 100755 --- a/tests/test_copy.py +++ b/tests/test_copy.py @@ -140,7 +140,8 @@ class CopyTests(ConnectingTestCase): about = abin.decode('latin1').replace('\\', '\\\\') 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('\\', '\\\\') curs = self.conn.cursor() @@ -161,7 +162,8 @@ class CopyTests(ConnectingTestCase): abin = ''.join(map(chr, range(32, 127) + range(160, 255))) about = abin.replace('\\', '\\\\') 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') curs = self.conn.cursor() @@ -184,7 +186,8 @@ class CopyTests(ConnectingTestCase): about = abin.replace('\\', '\\\\') 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('\\', '\\\\') import io @@ -381,5 +384,6 @@ decorate_all_tests(CopyTests, skip_copy_if_green) def test_suite(): return unittest.TestLoader().loadTestsFromName(__name__) + if __name__ == "__main__": unittest.main() diff --git a/tests/test_cursor.py b/tests/test_cursor.py index d91a7fc2..bee469e4 100755 --- a/tests/test_cursor.py +++ b/tests/test_cursor.py @@ -589,7 +589,10 @@ class CursorTests(ConnectingTestCase): # psycopg2 noticing. control_conn = self.conn connect_func = self.connect - wait_func = lambda conn: None + + def wait_func(conn): + pass + self._test_external_close(control_conn, connect_func, wait_func) @skip_if_no_superuser @@ -599,7 +602,10 @@ class CursorTests(ConnectingTestCase): # Issue #443 is in the async code too. Since the fix is duplicated, # so is the test. 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 self._test_external_close(control_conn, connect_func, wait_func) @@ -648,5 +654,6 @@ class CursorTests(ConnectingTestCase): def test_suite(): return unittest.TestLoader().loadTestsFromName(__name__) + if __name__ == "__main__": unittest.main() diff --git a/tests/test_dates.py b/tests/test_dates.py index bb5aee30..4c2ba841 100755 --- a/tests/test_dates.py +++ b/tests/test_dates.py @@ -713,5 +713,6 @@ class FixedOffsetTimezoneTests(unittest.TestCase): def test_suite(): return unittest.TestLoader().loadTestsFromName(__name__) + if __name__ == "__main__": unittest.main() diff --git a/tests/test_errcodes.py b/tests/test_errcodes.py index 5ce2cef4..34d214b6 100755 --- a/tests/test_errcodes.py +++ b/tests/test_errcodes.py @@ -68,5 +68,6 @@ class ErrocodeTests(ConnectingTestCase): def test_suite(): return unittest.TestLoader().loadTestsFromName(__name__) + if __name__ == "__main__": unittest.main() diff --git a/tests/test_errors.py b/tests/test_errors.py index cb680a22..f67bb721 100755 --- a/tests/test_errors.py +++ b/tests/test_errors.py @@ -66,5 +66,6 @@ class ErrorsTests(ConnectingTestCase): def test_suite(): return unittest.TestLoader().loadTestsFromName(__name__) + if __name__ == "__main__": unittest.main() diff --git a/tests/test_extras_dictcursor.py b/tests/test_extras_dictcursor.py index 1d5dfd03..38f484d4 100755 --- a/tests/test_extras_dictcursor.py +++ b/tests/test_extras_dictcursor.py @@ -582,5 +582,6 @@ class NamedTupleCursorTest(ConnectingTestCase): def test_suite(): return unittest.TestLoader().loadTestsFromName(__name__) + if __name__ == "__main__": unittest.main() diff --git a/tests/test_fast_executemany.py b/tests/test_fast_executemany.py index ab0a129a..3eb19fac 100755 --- a/tests/test_fast_executemany.py +++ b/tests/test_fast_executemany.py @@ -238,5 +238,6 @@ testutils.decorate_all_tests(TestExecuteValues, def test_suite(): return unittest.TestLoader().loadTestsFromName(__name__) + if __name__ == "__main__": unittest.main() diff --git a/tests/test_green.py b/tests/test_green.py index b726c46f..491a720e 100755 --- a/tests/test_green.py +++ b/tests/test_green.py @@ -206,5 +206,6 @@ class CallbackErrorTestCase(ConnectingTestCase): def test_suite(): return unittest.TestLoader().loadTestsFromName(__name__) + if __name__ == "__main__": unittest.main() diff --git a/tests/test_ipaddress.py b/tests/test_ipaddress.py index 2b4ff6af..0db27240 100755 --- a/tests/test_ipaddress.py +++ b/tests/test_ipaddress.py @@ -125,6 +125,7 @@ class NetworkingTestCase(testutils.ConnectingTestCase): cur.execute("select %s", [ip.ip_network('::ffff:102:300/128')]) self.assertEquals(cur.fetchone()[0], '::ffff:102:300/128') + testutils.decorate_all_tests(NetworkingTestCase, skip_if_no_ipaddress) diff --git a/tests/test_lobject.py b/tests/test_lobject.py index 54d676c6..e32673c5 100755 --- a/tests/test_lobject.py +++ b/tests/test_lobject.py @@ -44,6 +44,7 @@ def skip_if_no_lo(f): return skip_if_no_lo_ + 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) self.assert_(isinstance(lo, lobject_subclass)) + 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) + decorate_all_tests(LargeObjectTruncateTests, 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.tell(), length) + decorate_all_tests(LargeObject64Tests, 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), lo.truncate, length) + decorate_all_tests(LargeObjectNot64Tests, 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(): return unittest.TestLoader().loadTestsFromName(__name__) + if __name__ == "__main__": unittest.main() diff --git a/tests/test_module.py b/tests/test_module.py index cf3eb020..b5cbb7b8 100755 --- a/tests/test_module.py +++ b/tests/test_module.py @@ -348,5 +348,6 @@ class TestVersionDiscovery(unittest.TestCase): def test_suite(): return unittest.TestLoader().loadTestsFromName(__name__) + if __name__ == "__main__": unittest.main() diff --git a/tests/test_psycopg2_dbapi20.py b/tests/test_psycopg2_dbapi20.py index d69c6073..5bcb9e35 100755 --- a/tests/test_psycopg2_dbapi20.py +++ b/tests/test_psycopg2_dbapi20.py @@ -37,7 +37,7 @@ class Psycopg2Tests(dbapi20.DatabaseAPI20Test): connect_args = () connect_kw_args = {'dsn': dsn} - lower_func = 'lower' # For stored procedure test + lower_func = 'lower' # For stored procedure test def test_callproc(self): # Until DBAPI 2.0 compliance, callproc should return None or it's just @@ -50,16 +50,14 @@ class Psycopg2Tests(dbapi20.DatabaseAPI20Test): con = self._connect() try: cur = con.cursor() - if self.lower_func and hasattr(cur,'callproc'): - cur.callproc(self.lower_func,('FOO',)) + if self.lower_func and hasattr(cur, 'callproc'): + cur.callproc(self.lower_func, ('FOO',)) r = cur.fetchall() - self.assertEqual(len(r),1,'callproc produced no result set') - self.assertEqual(len(r[0]),1, - 'callproc produced invalid result set' - ) - self.assertEqual(r[0][0],'foo', - 'callproc produced invalid results' - ) + self.assertEqual(len(r), 1, 'callproc produced no result set') + self.assertEqual(len(r[0]), 1, + 'callproc produced invalid result set') + self.assertEqual(r[0][0], 'foo', + 'callproc produced invalid results') finally: con.close() @@ -78,11 +76,13 @@ class Psycopg2TPCTests(dbapi20_tpc.TwoPhaseCommitTests, unittest.TestCase): def connect(self): return psycopg2.connect(dsn=dsn) + decorate_all_tests(Psycopg2TPCTests, skip_if_tpc_disabled) def test_suite(): return unittest.TestLoader().loadTestsFromName(__name__) + if __name__ == '__main__': unittest.main() diff --git a/tests/test_quote.py b/tests/test_quote.py index c14d80cc..33371ee7 100755 --- a/tests/test_quote.py +++ b/tests/test_quote.py @@ -126,7 +126,8 @@ class QuotingTestCase(ConnectingTestCase): if sys.version_info[0] < 3: data = ''.join(map(chr, range(32, 127) + range(160, 256))) 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 curs.execute("SELECT %s::text;", (data,)) @@ -150,7 +151,8 @@ class QuotingTestCase(ConnectingTestCase): if sys.version_info[0] < 3: data = ''.join(map(chr, range(32, 127) + range(128, 256))) 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 curs.execute("SELECT %s::text;", (data,)) @@ -252,5 +254,6 @@ class TestStringAdapter(ConnectingTestCase): def test_suite(): return unittest.TestLoader().loadTestsFromName(__name__) + if __name__ == "__main__": unittest.main() diff --git a/tests/test_sql.py b/tests/test_sql.py index cc9bba2b..6d684a99 100755 --- a/tests/test_sql.py +++ b/tests/test_sql.py @@ -412,5 +412,6 @@ class ValuesTest(ConnectingTestCase): def test_suite(): return unittest.TestLoader().loadTestsFromName(__name__) + if __name__ == "__main__": unittest.main() diff --git a/tests/test_transaction.py b/tests/test_transaction.py index 060c8d41..b3648fe8 100755 --- a/tests/test_transaction.py +++ b/tests/test_transaction.py @@ -249,5 +249,6 @@ class QueryCancellationTests(ConnectingTestCase): def test_suite(): return unittest.TestLoader().loadTestsFromName(__name__) + if __name__ == "__main__": unittest.main() diff --git a/tests/test_types_basic.py b/tests/test_types_basic.py index 510bdca6..774f7ad8 100755 --- a/tests/test_types_basic.py +++ b/tests/test_types_basic.py @@ -168,8 +168,8 @@ class TypesBasicTests(ConnectingTestCase): self.assertEqual(curs.fetchone()[0], []) # issue #788 (test commented out until issue fixed) - #curs.execute("select null = any(%s)", ([[]], )) - #self.assertFalse(curs.fetchone()[0]) + # curs.execute("select null = any(%s)", ([[]], )) + # self.assertFalse(curs.fetchone()[0]) def testEmptyArrayNoCast(self): s = self.execute("SELECT '{}' AS foo") @@ -232,9 +232,9 @@ class TypesBasicTests(ConnectingTestCase): curs.execute("insert into na (textaa) values (%s)", ([['a', None]],)) curs.execute("insert into na (textaa) values (%s)", ([[None, None]],)) - curs.execute("insert into na (intaa) values (%s)", ([[None]],)) + curs.execute("insert into na (intaa) values (%s)", ([[None]],)) curs.execute("insert into na (intaa) values (%s)", ([[42, None]],)) - curs.execute("insert into na (intaa) values (%s)", ([[None, None]],)) + curs.execute("insert into na (intaa) values (%s)", ([[None, None]],)) curs.execute("insert into na (boolaa) values (%s)", ([[None]],)) curs.execute("insert into na (boolaa) values (%s)", ([[True, None]],)) @@ -554,11 +554,13 @@ def skip_if_cant_cast(f): return skip_if_cant_cast_ + decorate_all_tests(ByteaParserTest, skip_if_cant_cast) def test_suite(): return unittest.TestLoader().loadTestsFromName(__name__) + if __name__ == "__main__": unittest.main() diff --git a/tests/test_types_extras.py b/tests/test_types_extras.py index 18ac6eaa..1aa2ecf5 100755 --- a/tests/test_types_extras.py +++ b/tests/test_types_extras.py @@ -123,6 +123,7 @@ class TypesExtrasTests(ConnectingTestCase): s = self.execute("""SELECT '{"(1,2)","(3,4)"}' AS foo""") self.failUnless(s == """{"(1,2)","(3,4)"}""") + def skip_if_no_hstore(f): @wraps(f) def skip_if_no_hstore_(self): @@ -1137,6 +1138,7 @@ class JsonbTestCase(ConnectingTestCase): curs.execute("""select NULL::jsonb[]""") self.assertEqual(curs.fetchone()[0], None) + decorate_all_tests(JsonbTestCase, skip_if_no_jsonb_type) @@ -1425,7 +1427,7 @@ class RangeTestCase(unittest.TestCase): from datetime import datetime from psycopg2.tz import FixedOffsetTimezone converter = unicode if sys.version_info < (3, 0) else str - tz = FixedOffsetTimezone(-5*60, "EST") + tz = FixedOffsetTimezone(-5 * 60, "EST") r = DateTimeTZRange(datetime(2010, 1, 1, tzinfo=tz), datetime(2011, 1, 1, tzinfo=tz)) expected = u'[2010-01-01 00:00:00-05:00, 2011-01-01 00:00:00-05:00)' @@ -1763,6 +1765,7 @@ class RangeCasterTestCase(ConnectingTestCase): for r in [ra1, ra2, rars2, rars3]: del ext.adapters[r.range, ext.ISQLQuote] + decorate_all_tests(RangeCasterTestCase, skip_if_no_range) diff --git a/tests/test_with.py b/tests/test_with.py index 1e4c5181..ced425d7 100755 --- a/tests/test_with.py +++ b/tests/test_with.py @@ -217,12 +217,13 @@ class WithCursorTestCase(WithTestCase): @skip_before_postgres(8, 2) def test_named_with_noop(self): - with self.conn.cursor('named') as cur: + with self.conn.cursor('named'): pass def test_suite(): return unittest.TestLoader().loadTestsFromName(__name__) + if __name__ == "__main__": unittest.main() diff --git a/tests/testutils.py b/tests/testutils.py index 98753819..7fd11f9d 100644 --- a/tests/testutils.py +++ b/tests/testutils.py @@ -42,8 +42,8 @@ if sys.version_info[0] == 2: unichr = unichr else: # Python 3 - from io import StringIO - from importlib import reload + from io import StringIO # noqa + from importlib import reload # noqa long = int unichr = chr @@ -64,6 +64,7 @@ def assertDsnEqual(self, dsn1, dsn2, msg=None): """Check that two conninfo string have the same content""" self.assertEqual(set(dsn1.split()), set(dsn2.split()), msg) + unittest.TestCase.assertDsnEqual = assertDsnEqual @@ -363,6 +364,7 @@ def skip_if_green(reason): return skip_if_green__ return skip_if_green_ + skip_copy_if_green = skip_if_green("copy in async mode currently not supported") diff --git a/tox.ini b/tox.ini index df6b5b9e..e504fdcb 100644 --- a/tox.ini +++ b/tox.ini @@ -7,5 +7,5 @@ whitelist_externals = make [flake8] max-line-length = 85 -ignore = E128, W503 -exclude = build, doc, sandbox, examples, tests/dbapi20.py +ignore = E128, W503, E741 +exclude = build, doc, examples, tests/dbapi20.py