-
Notifications
You must be signed in to change notification settings - Fork 53
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
NXDRIVE-2967: Upgrade the deprecated datetime adapter #5313
base: wip-NXDRIVE-2929-upgrade-python-from-3.9.5-to-3.12.3
Are you sure you want to change the base?
Changes from 1 commit
aa11d4e
853392b
88f8a6f
62a40e6
1fc22e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,24 @@ def execute(self, sql: str, parameters: Iterable[Any] = ()) -> Cursor: | |
while True: | ||
count += 1 | ||
try: | ||
return super().execute(sql, parameters) | ||
if parameters: | ||
import datetime | ||
import sqlite3 | ||
new_param = [] | ||
for param in parameters: | ||
if type(param) == datetime.datetime: | ||
def adapt_datetime_iso(val): | ||
return datetime.datetime.fromtimestamp(str(val), datetime.UTC) | ||
mtime = sqlite3.register_adapter(param, adapt_datetime_iso) | ||
new_param.append(mtime) | ||
else: | ||
new_param.append(param) | ||
|
||
new_param = tuple(new_param) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (performance): Optimize parameter processing Instead of creating a list and then converting it to a tuple, consider using a generator expression for better performance. For example: new_param = tuple(adapt_datetime_iso(param) if isinstance(param, datetime.datetime) else param for param in parameters)
|
||
res = super().execute(sql, new_param) | ||
else: | ||
res = super().execute(sql, parameters) | ||
return res | ||
except OperationalError as exc: | ||
log.info( | ||
f"Retry locked database #{count}, {sql=}, {parameters=}", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -95,10 +95,6 @@ def no_warnings(recwarn): | |
continue | ||
elif "Cryptography will be significantly faster" in message: | ||
continue | ||
elif "The default datetime adapter is deprecated as of Python 3.12" in message: | ||
continue | ||
Comment on lines
-98
to
-99
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (testing): Removal of warning suppression for deprecated datetime adapter The removal of this warning suppression suggests that the issue with the deprecated datetime adapter has been addressed. However, it would be beneficial to add a test that verifies the new datetime handling in the
|
||
elif "datetime.datetime" in message: | ||
continue | ||
|
||
warn = f"{warning.filename}:{warning.lineno} {message}" | ||
print(warn, file=sys.stderr) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue: Improve datetime adaptation for SQLite
The current implementation of adapt_datetime_iso is incorrect and the adapter registration is inefficient. Consider using isoformat() for datetime conversion and register the adapter once outside the loop. For example:
def adapt_datetime_iso(val):
return val.isoformat()
sqlite3.register_adapter(datetime.datetime, adapt_datetime_iso)
In the loop
if isinstance(param, datetime.datetime):
new_param.append(param) # SQLite will use the registered adapter