PEP 675: Clarify Motivation query examples are abridged (#2854)

Co-authored-by: C.A.M. Gerlach <CAM.Gerlach@Gerlach.CAM>
This commit is contained in:
Eclips4 2022-10-30 16:29:39 +03:00 committed by GitHub
parent a3310aeeff
commit dc44249a14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 3 deletions

View File

@ -42,6 +42,7 @@ insert it into a predefined SQL query:
def query_user(conn: Connection, user_id: str) -> User: def query_user(conn: Connection, user_id: str) -> User:
query = f"SELECT * FROM data WHERE user_id = {user_id}" query = f"SELECT * FROM data WHERE user_id = {user_id}"
conn.execute(query) conn.execute(query)
... # Transform data to a User object and return it
query_user(conn, "user123") # OK. query_user(conn, "user123") # OK.
@ -69,7 +70,7 @@ original function would be written safely as a query with parameters:
def query_user(conn: Connection, user_id: str) -> User: def query_user(conn: Connection, user_id: str) -> User:
query = "SELECT * FROM data WHERE user_id = ?" query = "SELECT * FROM data WHERE user_id = ?"
conn.execute(query, (user_id,)) conn.execute(query, (user_id,))
...
The problem is that there is no way to enforce this The problem is that there is no way to enforce this
discipline. sqlite3's own `documentation discipline. sqlite3's own `documentation
@ -140,8 +141,8 @@ from a format string using ``user_id``, and cannot be passed to
def query_user(conn: Connection, user_id: str) -> User: def query_user(conn: Connection, user_id: str) -> User:
query = f"SELECT * FROM data WHERE user_id = {user_id}" query = f"SELECT * FROM data WHERE user_id = {user_id}"
conn.execute(query) conn.execute(query) # Error: Expected LiteralString, got str.
# Error: Expected LiteralString, got str. ...
The method remains flexible enough to allow our more complicated The method remains flexible enough to allow our more complicated
example: example: