-
-
Notifications
You must be signed in to change notification settings - Fork 216
Description
Hi all,
This is my first issue here so apologies if things aren't formatted properly.
I'm currently using psycopg 3.1.17 on Python 3.11 on Ubuntu 20.04.
My current direct usecase for this library involves sanitising and building text SQL queries, below is a mock example:
>>> example_query = sql.SQL("SELECT * FROM foo_table WHERE example_col = {bar_value}")
>>> example_query
SQL('SELECT * FROM foo_table WHERE example_col = {bar_value}')
>>> example_query.format(bar_value="formatted_bar_value")
Composed([SQL('SELECT * FROM foo_table WHERE example_col = '), Literal('formatted_bar_value')])
>>> example_query.format(bar_value="formatted_bar_value").as_string()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Composable.as_string() missing 1 required positional argument: 'context'
>>> example_query.format(bar_value="formatted_bar_value").as_string(None)
"SELECT * FROM foo_table WHERE example_col = 'formatted_bar_value'"I would have expected Composable.as_string called without arguments to default to context=None as context is defined as Optional in signatures and documentation, instead it errors unless None is provided for the context argument. This same behaviour exists with Composed objects as they inherit from Composable.
Assuming this would align with the style and intention of the library's text SQL API design, I believe this is a simple case of changing the to_string method context argument from:
... context: Optional[AdaptContext] ... to ... context: Optional[AdaptContext] = None ....
Many thanks for maintaining this library, it's incredibly useful.