Skip to content

Report invalid source schemes as OperationalError - #296

Closed
ryanduguid wants to merge 4 commits into
beancount:masterfrom
ryanduguid:fix/invalid-source-dsn
Closed

ryanduguid wants to merge 4 commits into
beancount:masterfrom
ryanduguid:fix/invalid-source-dsn

Conversation

@ryanduguid

@ryanduguid ryanduguid commented Oct 2, 2026 •

Copy link
Copy Markdown

Summary

Report a missing or unknown source scheme as OperationalError, following the connection error contract in PEP 249. For example, connect('unknown:ledger') reports unknown source scheme: 'unknown'. Messages contain only the scheme.

The import handler recognises a missing source module or a missing parent of a dotted source scheme. Genuine dependency errors propagate, and source attachment runs outside that handler. A short comment and a local name explain the parent-module comparison.

Fixes #247.

Evidence

  • Before the connection fix, four new regression methods failed with 11 ModuleNotFoundError subcase errors. The final clarification passes all 303 tests and Ruff on Linux Python 3.11.16 / Beancount 3.2.3; all ten connection tests also pass on Windows Python 3.11.
  • Tests cover missing and unknown schemes, dotted schemes, message privacy, dependency and attachment failures, argument forwarding, empty connections and an existing source.
  • Before the comment/local-name clarification, the same exception predicate and tests passed on Python 3.8, 3.11 and 3.14. Parser and renderer coverage gates passed at 100%. Current source distribution and wheel builds pass.
  • Five scoped Ruff annotations preserve the inherited renderer and command signatures under Ruff 0.16. Their runtime code remains unchanged.
  • Fork CI completed all 24 checks at 36f2dff, with deployment skipped and no failures.

Merge danger

Door: two-way. Blast radius: source connections.

Callers catching ModuleNotFoundError for invalid DSNs will need to catch OperationalError.

Unverified

Upstream checks and maintainer review remain outstanding. The full Windows baseline has temporary-file handle errors and a CSV newline failure. The current Aikido rescan timed out connecting and supplies no fresh scan result.

Comment thread beanquery/__init__.py
Comment on lines +55 to +59
missing_source = (
exc.name == source_name or
(exc.name is not None and exc.name.startswith('beanquery.sources.') and
source_name.startswith(f'{exc.name}.'))
)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain what this code is doing? I cannot understand why it is needed in the form it is written.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python can name a missing parent package instead of the requested module. For foo.bar:ledger, the import requests beanquery.sources.foo.bar, but exc.name can be beanquery.sources.foo.

The predicate converts a missing source module or one of its parents below beanquery.sources to OperationalError. It re-raises unrelated dependency errors, and excludes beanquery.sources itself. source.attach() runs outside the handler so its failures also propagate.

I've named exc.name locally and added a comment explaining the dotted-parent case. The real dotted-scheme and dependency tests pass, along with all 303 Linux tests, Ruff, ten Windows connection tests and both package builds.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems still very much LLM crap. I warned you. Goodbye

@dnicolodi dnicolodi closed this Oct 2, 2026
@ryanduguid
ryanduguid deleted the fix/invalid-source-dsn branch October 22, 2026 03:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve exception message when attempting to connect to an invalid DSN

2 participants