Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: not enough arguments for format string #1059

Closed
zx80 opened this issue Jul 28, 2022 · 2 comments
Closed

TypeError: not enough arguments for format string #1059

zx80 opened this issue Jul 28, 2022 · 2 comments

Comments

@zx80
Copy link

zx80 commented Jul 28, 2022

In PyMySQL 1.0.2 execute does not like an empty tuple:

import pymysql as db

with db.connect(host="localhost", user="foo", password="bla") as conn:
    with conn.cursor() as cur:
        cur.execute("SELECT DATE_FORMAT(NOW(),'%Y-%m-%d %H:%i:%s');", tuple())
        row = cur.fetchone()
        print(f"row: {row}")

Results in:

Traceback (most recent call last):
  File "./test.py", line 7, in <module>
    cur.execute("SELECT DATE_FORMAT(NOW(),'%Y-%m-%d %H:%i:%s');", tuple())
  File "/.../lib/python3.8/site-packages/pymysql/cursors.py", line 146, in execute
    query = self.mogrify(query, args)
  File "/.../lib/python3.8/site-packages/pymysql/cursors.py", line 125, in mogrify
    query = query % self._escape_args(args, conn)
TypeError: not enough arguments for format string

ISTM should behave as if None was passed:

row: ('2022-07-28 21:21:01',)

This is a pain because otherwise some exception handling is needed when there is no parameters.

@methane
Copy link
Member

methane commented Jul 29, 2022

ISTM should behave as if None was passed:

I don't think so. And changing it is backward incompatible.

This is a pain because otherwise some exception handling is needed when there is no parameters.

You can pass None.

@methane methane closed this as not planned Won't fix, can't repro, duplicate, stale Jul 29, 2022
@zx80
Copy link
Author

zx80 commented Jul 29, 2022

ISTM should behave as if None was passed:

I don't think so.

Why?

changing it is backward incompatible.

If it changes the behavior from error to working, I do not see how this would be a problem to anyone.

This is a pain because otherwise some exception handling is needed when there is no parameters.

You can pass None.

Sure, but I have a tuple of parameters, which may or may not be empty, thus it means having to test for changing the parameter everytime. Needing a special handling for 0 parameters is just a useless pain as it should be managed by the driver, IMHO.

Other drivers do not have such an issue: sqlite3, apsw, aiosqlite, psycopg, psycopg2, pygresql, asyncpg, pg8000, mysql-connector…

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 31, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants