Allow make_dsn to take no parameter

The behaviour of connect() is unchanged: either dsn or params must be
specified.
This commit is contained in:
Daniele Varrazzo 2016-03-03 16:58:24 +00:00
parent 7aab934ae5
commit c9fd828f8a
4 changed files with 8 additions and 8 deletions

View File

@ -497,11 +497,8 @@ Other functions
Put together the arguments in *kwargs* into a connection string. If *dsn* Put together the arguments in *kwargs* into a connection string. If *dsn*
is specified too, merge the arguments coming from both the sources. If the is specified too, merge the arguments coming from both the sources. If the
same argument is specified in both the sources, the *kwargs* version same argument name is specified in both the sources, the *kwargs* value
overrides the *dsn* version. overrides the *dsn* value.
At least one parameter is required (either *dsn* or any keyword). Note
that the empty string is a valid connection string.
The input arguments are validated: the output should always be a valid The input arguments are validated: the output should always be a valid
connection string (as far as `parse_dsn()` is concerned). If not raise connection string (as far as `parse_dsn()` is concerned). If not raise

View File

@ -116,6 +116,9 @@ def connect(dsn=None, connection_factory=None, cursor_factory=None,
library: the list of supported parameters depends on the library version. library: the list of supported parameters depends on the library version.
""" """
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, async=async) conn = _connect(dsn, connection_factory=connection_factory, async=async)
if cursor_factory is not None: if cursor_factory is not None:

View File

@ -158,7 +158,7 @@ class NoneAdapter(object):
def make_dsn(dsn=None, **kwargs): def make_dsn(dsn=None, **kwargs):
"""Convert a set of keywords into a connection strings.""" """Convert a set of keywords into a connection strings."""
if dsn is None and not kwargs: if dsn is None and not kwargs:
raise TypeError('missing dsn and no parameters') return ''
# If no kwarg is specified don't mung the dsn, but verify it # If no kwarg is specified don't mung the dsn, but verify it
if not kwargs: if not kwargs:

View File

@ -381,8 +381,8 @@ class MakeDsnTestCase(ConnectingTestCase):
def assertDsnEqual(self, dsn1, dsn2): def assertDsnEqual(self, dsn1, dsn2):
self.assertEqual(set(dsn1.split()), set(dsn2.split())) self.assertEqual(set(dsn1.split()), set(dsn2.split()))
def test_there_has_to_be_something(self): def test_empty_arguments(self):
self.assertRaises(TypeError, ext.make_dsn) self.assertEqual(ext.make_dsn(), '')
def test_empty_string(self): def test_empty_string(self):
dsn = ext.make_dsn('') dsn = ext.make_dsn('')