From 74e6efd717d0368a190ac4a7fc3a2a603cc369e9 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Wed, 26 Sep 2012 11:55:21 +0100 Subject: [PATCH] Raise TypeError instead of InterfaceError on bad params on connect() TypeError is the standard Python error raised in this case: $ python -c "(lambda a: None)(b=10)" TypeError: () got an unexpected keyword argument 'b' We only used to raise InterfaceError when connect was used without any parameter at all, so it's hard to think a program depending on that design. Furthermore the function has always raised (and still does) OperationalError too, if the bad argument is detected by the libpq, and that cannot be changed because we can't tell the difference from a normal connection error. --- lib/__init__.py | 4 ++-- tests/test_module.py | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/__init__.py b/lib/__init__.py index e49bcd77..892d8038 100644 --- a/lib/__init__.py +++ b/lib/__init__.py @@ -164,13 +164,13 @@ def connect(dsn=None, items.extend([(k, v) for (k, v) in kwargs.iteritems() if v is not None]) if dsn is not None and items: - raise InterfaceError( + raise TypeError( "'%s' is an invalid keyword argument when the dsn is specified" % items[0][0]) if dsn is None: if not items: - raise InterfaceError('missing dsn and no parameters') + raise TypeError('missing dsn and no parameters') else: dsn = " ".join(["%s=%s" % (k, _param_escape(str(v))) for (k, v) in items]) diff --git a/tests/test_module.py b/tests/test_module.py index c2edeb24..4083c368 100755 --- a/tests/test_module.py +++ b/tests/test_module.py @@ -40,10 +40,10 @@ class ConnectTestCase(unittest.TestCase): psycopg2._connect = self._connect_orig def test_there_has_to_be_something(self): - self.assertRaises(psycopg2.InterfaceError, psycopg2.connect) - self.assertRaises(psycopg2.InterfaceError, psycopg2.connect, + self.assertRaises(TypeError, psycopg2.connect) + self.assertRaises(TypeError, psycopg2.connect, connection_factory=lambda dsn, async=False: None) - self.assertRaises(psycopg2.InterfaceError, psycopg2.connect, + self.assertRaises(TypeError, psycopg2.connect, async=True) def test_no_keywords(self): @@ -128,11 +128,11 @@ class ConnectTestCase(unittest.TestCase): self.assertEqual(self.args[0], r"dbname='\\every thing\''") def test_no_kwargs_swallow(self): - self.assertRaises(psycopg2.InterfaceError, + self.assertRaises(TypeError, psycopg2.connect, 'dbname=foo', database='foo') - self.assertRaises(psycopg2.InterfaceError, + self.assertRaises(TypeError, psycopg2.connect, 'dbname=foo', user='postgres') - self.assertRaises(psycopg2.InterfaceError, + self.assertRaises(TypeError, psycopg2.connect, 'dbname=foo', no_such_param='meh')