Registry / database / dsnparse

dsnparse

JSON →
library0.4.0pypypi✓ verified 86d ago

dsnparse is a Python library designed to easily parse Data Source Name (DSN) connection strings, similar to URLs. It's used in projects like 'prom' and 'morp' and aims to provide an interface familiar to users of Python's `urlparse` module. The current version is 0.4.0, and it maintains an infrequent but active release cadence, with the last update in October 2025.

pip install dsnparse
INSTALL
IMPORT
SIG · DSNPARSE
D
dsnparse
databasepythonv0.4.0
Install
1.7s avg
Import
27ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.4.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.029s · 17.8MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 1.7s · import 0.025s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

parse
from dsnparse import parse
Commonly imported directly for DSN string parsing.
parse_environ
from dsnparse import parse_environ
Used for parsing DSNs from environment variables.
ParseResult
from dsnparse import ParseResult
Base class for customizing parsing results.

This quickstart demonstrates basic DSN parsing, extracting information from environment variables, and customizing the parsing result class.

import dsnparse import os # Example 1: Basic DSN parsing dsn = "prom.interface.postgres.Interface://testuser:testpw@localhost:1234/testdb?appname=my_app#frag1" r = dsnparse.parse(dsn) print(f"Scheme: {r.scheme}") # prom.interface.postgres.Interface print(f"Username: {r.username}") # testuser print(f"Password: {r.password}") # testpw print(f"Host: {r.host}") # localhost print(f"Port: {r.port}") # 1234 print(f"Host Location: {r.hostloc}") # localhost:1234 print(f"Paths: {r.paths}") # ['testdb'] print(f"Query: {r.query}") # appname=my_app print(f"Fragment: {r.fragment}") # frag1 print(f"Get Query Param 'appname': {r.get_query('appname')}") # my_app # Example 2: Parsing from environment variable os.environ['MY_DB_DSN'] = os.environ.get('MY_DB_DSN', 'sqlite:///:memory:') env_dsn = dsnparse.parse_environ('MY_DB_DSN') print(f"Env DSN Scheme: {env_dsn.scheme}") # sqlite print(f"Env DSN Path: {env_dsn.path}") # :memory: # Example 3: Custom ParseResult class class MyResult(dsnparse.ParseResult): def configure(self): # Expose 'scheme' as 'interface' for custom logic self.interface = self.scheme custom_dsn = "MyInterface://customuser:custompass@host:5432/path" r_custom = dsnparse.parse(custom_dsn, parse_class=MyResult) print(f"Custom DSN Type: {type(r_custom)}") # <class '__main__.MyResult'> print(f"Custom DSN Interface: {r_custom.interface}") # MyInterface
Debug
Known issues
gotchaAccessing DSN components (e.g., `username`, `password`, `port`, `fragment`, `query`) that are not present in the DSN string will return `None`. Always check for `None` or use default values if these components are optional in your application.
fix
Use conditional checks or provide default values: `user = r.username or 'guest'`
affects: All
gotchaThe `paths` attribute returns a list of path segments, even if there is only one. If you expect a single path, access it as `r.paths[0]` after checking the list is not empty.
fix
Ensure `r.paths` is not empty before accessing `r.paths[0]`, or iterate through `r.paths`.
affects: All
gotchaBe aware of the distinction between `r.host` and `r.hostloc`. `r.host` will provide only the hostname, while `r.hostloc` will include the port if present (e.g., 'localhost' vs 'localhost:1234').
fix
Choose the appropriate attribute based on whether you need the port included with the host.
affects: All
gotchaWhen parsing from environment variables using `dsnparse.parse_environ(KEY)`, the library expects the variable to be set. If the environment variable is not found, it will raise a `KeyError` unless handled by the calling code.
fix
Ensure the environment variable is set or wrap the call in a `try-except KeyError` block, or provide a fallback mechanism for missing variables.
affects: All
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'dsnparse'
The 'dsnparse' library has not been installed in your Python environment.
fix
Install the library using pip: `pip install dsnparse`
ValueError: Invalid port specified
When parsing certain DSNs (like `sqlite:///:memory:` in version 0.4.0), `dsnparse` might incorrectly attempt to interpret a path component as a 'host:port' segment, leading to a port validation error.
fix
For SQLite, use a file-based DSN format like `sqlite:///path/to/database.db` or upgrade `dsnparse` to a version that correctly handles in-memory SQLite DSNs (fixed in 0.4.2+).
AttributeError: 'ParseResult' object has no attribute 'hostname'
The `dsnparse.ParseResult` object uses the attribute `host` to store the hostname, not `hostname` as is common in `urllib.parse.urlparse`'s result.
fix
Access the host using the `host` attribute: `dsn = dsnparse.dsnparse('postgresql://user@localhost:5432/mydb'); print(dsn.host)`
TypeError: dsnparse() missing 1 required positional argument: 'dsn'
The `dsnparse.dsnparse()` function was called without providing the mandatory DSN string argument.
fix
Provide the DSN string to be parsed as the first argument: `dsnparse.dsnparse('postgresql://user:pass@host:port/database')`
Upgrade
Version history
0.4.0latest on PyPI · released Oct 13, 2025
Audit
Dependencies

No dependency data recorded yet.

Agent activity
11 hits · last 30 days
node
8
Resources
dsnparse — pip install dsnparse · libregistry