Skip to content

Commit e556879

Browse files
committed
Add a challenge for LiteralString
1 parent 63efa32 commit e556879

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
TODO:
3+
4+
You're writing a web backend.
5+
Annotate a function `execute_query` which runs SQL, but also can prevent SQL injection attacks.
6+
"""
7+
8+
from typing import Iterable
9+
10+
11+
def execute_query(sql, parameters: Iterable[str] = ...):
12+
"""No need to implement it"""
13+
14+
15+
## End of your code ##
16+
def query_user(user_id: str):
17+
query = f"SELECT * FROM data WHERE user_id = {user_id}"
18+
execute_query(query) # expect-type-error
19+
20+
21+
def query_data(user_id: str, limit: bool) -> None:
22+
query = """
23+
SELECT
24+
user.name,
25+
user.age
26+
FROM data
27+
WHERE user_id = ?
28+
"""
29+
30+
if limit:
31+
query += " LIMIT 1"
32+
33+
execute_query(query, (user_id,))
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
TODO:
3+
4+
You're writing a web backend.
5+
Annotate a function `execute_query` which runs SQL, but also can prevent SQL injection attacks.
6+
"""
7+
8+
from typing import LiteralString, Iterable
9+
10+
11+
def execute_query(sql: LiteralString, parameters: Iterable[str] = ...):
12+
...
13+
14+
15+
## End of your code ##
16+
def query_user(user_id: str):
17+
query = f"SELECT * FROM data WHERE user_id = {user_id}"
18+
execute_query(query) # expect-type-error
19+
20+
21+
def query_data(user_id: str, limit: bool) -> None:
22+
query = """
23+
SELECT
24+
user.name,
25+
user.age
26+
FROM data
27+
WHERE user_id = ?
28+
"""
29+
30+
if limit:
31+
query += " LIMIT 1"
32+
33+
execute_query(query, (user_id,))

0 commit comments

Comments
 (0)