Skip to content

Commit b346ed8

Browse files
author
Kareem Zidane
committed
Fix using named param more than once
1 parent a078a44 commit b346ed8

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

src/cs50/_statement.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,18 +197,21 @@ def _substitute_named_or_pyformat_markers(self):
197197
Raises a ``RuntimeError`` if any parameters are missing or unused.
198198
"""
199199

200-
unused_params = set(self._kwargs.keys())
200+
unused_params = {param_name: True for param_name in self._kwargs.keys()}
201201
for token_index, param_name in self._placeholders.items():
202202
if param_name not in self._kwargs:
203203
raise RuntimeError(f"missing value for placeholder ({param_name})")
204204

205205
self._tokens[token_index] = self._kwargs[param_name]
206-
unused_params.remove(param_name)
206+
unused_params[param_name] = False
207207

208-
if len(unused_params) > 0:
209-
joined_unused_params = get_human_readable_list(sorted(unused_params))
208+
sorted_unique_unused_param_names = sorted(set(
209+
param_name for param_name, unused in unused_params.items() if unused))
210+
if len(sorted_unique_unused_param_names) > 0:
211+
joined_unused_params = get_human_readable_list(sorted_unique_unused_param_names)
210212
raise RuntimeError(
211-
f"unused value{'' if len(unused_params) == 1 else 's'} ({joined_unused_params})")
213+
f"unused value{'' if len(sorted_unique_unused_param_names) == 1 else 's'}"
214+
+ " ({joined_unused_params})")
212215

213216
def _escape_verbatim_colons(self):
214217
"""Escapes verbatim colons from string literal and identifier tokens so they aren't treated

tests/sql.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,10 @@ def test_named(self):
223223
self.assertEqual(self.db.execute("SELECT * FROM foo"), [{"firstname": "bar", "lastname": "baz"}])
224224
self.db.execute("DELETE FROM foo")
225225

226+
self.db.execute("INSERT INTO foo VALUES (:baz, :baz)", baz="baz")
227+
self.assertEqual(self.db.execute("SELECT * FROM foo"), [{"firstname": "baz", "lastname": "baz"}])
228+
self.db.execute("DELETE FROM foo")
229+
226230
self.db.execute("CREATE TABLE bar (firstname VARCHAR(255))")
227231
self.db.execute("INSERT INTO bar VALUES (:baz)", baz="baz")
228232
self.assertEqual(self.db.execute("SELECT * FROM bar"), [{"firstname": "baz"}])

0 commit comments

Comments
 (0)