Skip to content

Fix bug in set_generated_identity#38

Merged
timb07 merged 4 commits into
mainfrom
fix-bug-in-set_generated_identity
Jun 5, 2025
Merged

Fix bug in set_generated_identity#38
timb07 merged 4 commits into
mainfrom
fix-bug-in-set_generated_identity

Conversation

@timb07
Copy link
Copy Markdown
Contributor

@timb07 timb07 commented Jun 4, 2025

When using Psycopack with Django, we give it a native psycopg2 cursor. That cursor class's execute method names its first parameter as query. But in one place, Psycopack calls the cursor.execute with a named argument sql. That works with the LoggedCursor type used in Psycopack's tests, but throws an exception with psycopg2's native cursor type.

This PR adds a test to illustrate the problem, and changes the problematic method call to use a positional argument. It also changes LoggedCursor.execute to only accept positional arguments, so that it's not possible for this issue to happen again.

The PR also updates the .gitignore file to include files belonging to common IDEs.

@timb07 timb07 changed the title Fix bug in set generated identity Fix bug in set_generated_identity Jun 4, 2025
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 4, 2025

Coverage Report Results

Name Stmts Miss Branch BrPart Cover
src/psycopack/__init__.py 6 0 0 0 100%
src/psycopack/_commands.py 109 0 8 0 100%
src/psycopack/_conn.py 5 0 0 0 100%
src/psycopack/_const.py 3 0 0 0 100%
src/psycopack/_cur.py 21 0 0 0 100%
src/psycopack/_identifiers.py 12 0 2 0 100%
src/psycopack/_introspect.py 157 0 14 0 100%
src/psycopack/_logging.py 4 0 0 0 100%
src/psycopack/_psycopg.py 5 0 0 0 100%
src/psycopack/_registry.py 58 0 6 0 100%
src/psycopack/_repack.py 349 0 116 0 100%
src/psycopack/_tracker.py 165 0 36 0 100%
tests/conftest.py 19 0 0 0 100%
tests/factories.py 33 0 4 0 100%
tests/test_cur.py 20 0 2 0 100%
tests/test_fixtures.py 5 0 0 0 100%
tests/test_package.py 3 0 0 0 100%
tests/test_repack.py 693 0 0 0 100%
TOTAL 1667 0 188 0 100%

1 empty file skipped.

@timb07 timb07 force-pushed the fix-bug-in-set_generated_identity branch from 30addbe to 71517cf Compare June 4, 2025 23:50
Comment on lines 446 to +447
self.cur.execute(
sql=psycopg.sql.SQL(
psycopg.sql.SQL(
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Could you also change the signature of LoggedCursor's execute to take sql as a positional-only argument? That way, it's impossible to use it incorrectly

class LoggedCursor:
    # ...
    def execute(self, sql: str, /) -> None: ...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, that's probably a better fix.

timb07 added 4 commits June 5, 2025 12:47
LoggedCursor.execute has a parameter "sql", but the native Psycopg "cursor"
type's execute method has the parameter named "query" instead.

This test shows that calling execute with a named argument "sql" throws an
exception when used with a native cursor.
This change prevents the kind of issue fixed in the previous commit from
ever happening again by making cur.execute(sql="") calls throw a TypeError.
@timb07 timb07 force-pushed the fix-bug-in-set_generated_identity branch from 71517cf to 7ac2c24 Compare June 5, 2025 02:54
Copy link
Copy Markdown

@felcury felcury left a comment

Choose a reason for hiding this comment

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

🧑‍🍳

@timb07 timb07 merged commit 4c3db1b into main Jun 5, 2025
9 checks passed
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.

2 participants