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

Fix DeprecationWarning on 3.12 with a datetime adapter for sqlite3 #14139

Merged
merged 2 commits into from Aug 28, 2023
Merged

Fix DeprecationWarning on 3.12 with a datetime adapter for sqlite3 #14139

merged 2 commits into from Aug 28, 2023

Conversation

skirpichev
Copy link
Contributor

Comment on lines 34 to 39
if sys.version_info >= (3, 12):

def _adapt_datetime(val):
return val.isoformat(" ")

sqlite3.register_adapter(datetime.datetime, _adapt_datetime)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you believe it would be safer to not register adapter and call isoformat(" ") on the actual places where we insert data into sqlite ?

The fact that the upstream issue tell us there are issues with the default adapters is maybe a hint we should not re-register the same as it has global effects.

In the long term, I'm also wondering why isoformat(" "), and not just isoformat(), and if there would be any way we could migrate to just isoformat()...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you believe it would be safer to not register adapter and call isoformat(" ") on the actual places where we insert data into sqlite ?

I'm not sure. I hit this warning while running pytest diofant/tests/interactive for the diofant project and it seems to be related to the statement in the HistoryManager.new_session() method.

But maybe there are more such cases... The default adapter

The fact that the upstream issue tell us there are issues with the default adapters

The default adapters now are explicit rather than implicit. This is the only difference. I don't think this means there is an issue with the notion of default adapters or something like that...

I'm also wondering why isoformat(" "), and not just isoformat(), and if there would be any way we could migrate to just isoformat()

That's a literal copy of the current default adapter from the sqlite3 module. " " is because the default value for sep kwarg is "T", see docs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

he default adapters now are explicit rather than implicit. This is the only difference. I don't think this means there is an issue with the notion of default adapters or something like that...

I was more refering to the text in python/cpython#90016 saying "the default converters in SQLite3 have several bugs"

That's a literal copy of the current default adapter from the sqlite3 module. " " is because the default value for sep kwarg is "T", see docs.

Yes, I got that as well, I'm wondering if we should use the default with T, obviously we want to be careful as we don't want to mess up existing history.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was more refering to the text in python/cpython#90016 saying "the default converters in SQLite3 have several bugs"

The problem is that in the alternative fix you are using same default adapter, but reinvent this code in several places instead. Default adapters are exactly for this type of things...

Yes, I got that as well, I'm wondering if we should use the default with T

Perhaps, but this will change the db format. So, probably this will require a deprecation period, etc.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but I am using the fix only in IPython internal code. If we reinject the same default adapter with sqlite3.register_adapter this mean we affect code that runs in IPython. Code that will work in IPython may not work in pure Python, in particular is code that use sql and datetime. Once CPython remove the default adapter this code will work in IPython but fail in CPython

@Carreau
Copy link
Member

Carreau commented Aug 28, 2023

I'll try an alternative where I call .isoformat(" ") explicitely were necessary and don't change the default adapter.

skirpichev and others added 2 commits August 28, 2023 14:01
I think we need to avoid registering a default adapter, especially if
CPython want to remove them.

So we are going to call .isoformat(" ") explicitly. For backward compat
we'll keep the delimiter as " ", but we might want to consider using the
default of T later on, but still keeping in mind that old history have a
space.
@Carreau Carreau merged commit 46c7ccf into ipython:main Aug 28, 2023
20 of 21 checks passed
@Carreau
Copy link
Member

Carreau commented Aug 28, 2023

Thanks !

@Carreau Carreau added this to the 8.15 milestone Aug 28, 2023
@skirpichev skirpichev deleted the sql3-datetime-adapter branch August 28, 2023 12:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants