Accept no param in connect()

More friendly towards ``connect(**parse_dsn())``, and what psycopg3 does.

Close #1250
This commit is contained in:
Daniele Varrazzo 2021-05-20 12:36:40 +02:00
parent ba637a5e0c
commit 56dab8398d
3 changed files with 21 additions and 8 deletions

6
NEWS
View File

@ -1,6 +1,12 @@
Current release Current release
--------------- ---------------
What's new in psycopg 2.8.7
^^^^^^^^^^^^^^^^^^^^^^^^^^^
Accept empty params as `~psycopg2.connect()` (:ticket:`#1250).
What's new in psycopg 2.8.6 What's new in psycopg 2.8.6
^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^

View File

@ -120,9 +120,6 @@ def connect(dsn=None, connection_factory=None, cursor_factory=None, **kwargs):
if 'async_' in kwargs: if 'async_' in kwargs:
kwasync['async_'] = kwargs.pop('async_') 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) dsn = _ext.make_dsn(dsn, **kwargs)
conn = _connect(dsn, connection_factory=connection_factory, **kwasync) conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
if cursor_factory is not None: if cursor_factory is not None:

View File

@ -50,12 +50,22 @@ class ConnectTestCase(unittest.TestCase):
def tearDown(self): def tearDown(self):
psycopg2._connect = self._connect_orig psycopg2._connect = self._connect_orig
def test_there_has_to_be_something(self): def test_there_might_be_nothing(self):
self.assertRaises(TypeError, psycopg2.connect) psycopg2.connect()
self.assertRaises(TypeError, 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) connection_factory=lambda dsn, async_=False: None)
self.assertRaises(TypeError, psycopg2.connect, self.assertEqual(self.args[0], '')
async_=True) 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): def test_no_keywords(self):
psycopg2.connect('') psycopg2.connect('')