From 391386cfb9efb277813bb6b53d727497b642dd97 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Thu, 20 May 2021 12:36:40 +0200 Subject: [PATCH] Accept no param in connect() More friendly towards ``connect(**parse_dsn())``, and what psycopg3 does. Close #1250 --- NEWS | 6 ++++++ lib/__init__.py | 3 --- tests/test_module.py | 20 +++++++++++++++----- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/NEWS b/NEWS index 5d26d711..4e4b68fe 100644 --- a/NEWS +++ b/NEWS @@ -10,6 +10,12 @@ What's new in psycopg 2.9 `~psycopg2.errors.DatabaseError`) (:ticket:`#1148`). +What's new in psycopg 2.8.7 +^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Accept empty params as `~psycopg2.connect()` (:ticket:`#1250). + + What's new in psycopg 2.8.6 ^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/lib/__init__.py b/lib/__init__.py index 3cd46404..52c82bc0 100644 --- a/lib/__init__.py +++ b/lib/__init__.py @@ -120,9 +120,6 @@ def connect(dsn=None, connection_factory=None, cursor_factory=None, **kwargs): if 'async_' in kwargs: kwasync['async_'] = kwargs.pop('async_') - if dsn is None and not kwargs: - raise TypeError('missing dsn and no parameters') - dsn = _ext.make_dsn(dsn, **kwargs) conn = _connect(dsn, connection_factory=connection_factory, **kwasync) if cursor_factory is not None: diff --git a/tests/test_module.py b/tests/test_module.py index 24b072c4..c492bbdf 100755 --- a/tests/test_module.py +++ b/tests/test_module.py @@ -50,12 +50,22 @@ class ConnectTestCase(unittest.TestCase): def tearDown(self): psycopg2._connect = self._connect_orig - def test_there_has_to_be_something(self): - self.assertRaises(TypeError, psycopg2.connect) - self.assertRaises(TypeError, psycopg2.connect, + def test_there_might_be_nothing(self): + psycopg2.connect() + self.assertEqual(self.args[0], '') + self.assertEqual(self.args[1], None) + self.assertEqual(self.args[2], False) + + psycopg2.connect( connection_factory=lambda dsn, async_=False: None) - self.assertRaises(TypeError, psycopg2.connect, - async_=True) + self.assertEqual(self.args[0], '') + self.assertNotEqual(self.args[1], None) + self.assertEqual(self.args[2], False) + + psycopg2.connect(async_=True) + self.assertEqual(self.args[0], '') + self.assertEqual(self.args[1], None) + self.assertEqual(self.args[2], True) def test_no_keywords(self): psycopg2.connect('')