Skip to content

grass.pygrass: Add the psycopg2 hint as an exception note - #7690

Merged
wenzeslaus merged 1 commit into
OSGeo:mainfrom
wenzeslaus:pygrass-psycopg2-import-note
Jul 14, 2026
Merged

wenzeslaus merged 1 commit into
OSGeo:mainfrom
wenzeslaus:pygrass-psycopg2-import-note

Conversation

@wenzeslaus

@wenzeslaus wenzeslaus commented Jul 14, 2026 •

Copy link
Copy Markdown
Member

Now, the table connection raises a new ImportError with the hint as its message, so a missing psycopg2 ends in a pair of chained exceptions ("During handling of the above exception, another exception occurred").

Add the hint as a note to the original error instead. From the Python documentation of BaseException.add_note(): "Add the string note to the exception's notes which appear in the standard traceback after the exception string."

Connecting to a PostgreSQL table without psycopg2 then gives a single exception with the actual error as its message and the hint after it:

ModuleNotFoundError: No module named 'psycopg2'
You need to install psycopg2 to connect with this table.

The error is not caught anywhere, so it reaches a traceback, which is where a note is shown. It would not be shown if a caller printed str(exception).

This is the case discussed in #4032 (table.py:865), which was waiting for Python 3.11 as the minimum version. It touches the same lines as #4032, so whichever is merged second needs a trivial conflict resolution.

The table connection raised a new ImportError with the hint as its message,
so the message of the original error, which says which module is missing,
was replaced by the hint.

Add the hint as a note to the original error instead. From the Python
documentation of BaseException.add_note(): "Add the string note to the
exception's notes which appear in the standard traceback after the exception
string." The traceback then shows both the missing module and the hint.

Exception notes need Python 3.11, which is now the minimum version.
@github-actions github-actions Bot added Python Related code is in Python libraries labels Jul 14, 2026
@wenzeslaus
wenzeslaus enabled auto-merge (squash) July 14, 2026 07:10

@echoix echoix left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Perfect! Needed Python 3.11, now it is there, and follows the Python docs.

@wenzeslaus
wenzeslaus merged commit e0672ab into OSGeo:main Jul 14, 2026
27 checks passed
@wenzeslaus
wenzeslaus deleted the pygrass-psycopg2-import-note branch July 14, 2026 08:22
@github-actions github-actions Bot added this to the 8.6.0 milestone Jul 14, 2026
echoix pushed a commit to wenzeslaus/grass that referenced this pull request Jul 14, 2026
The table connection raised a new ImportError with the hint as its message,
so the message of the original error, which says which module is missing,
was replaced by the hint.

Add the hint as a note to the original error instead. From the Python
documentation of BaseException.add_note(): Add the string note to the
exception's notes which appear in the standard traceback after the exception
string. The traceback then shows both the missing module and the hint.

Exception notes need Python 3.11, which is now the minimum version.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

libraries Python Related code is in Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants