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

NXDRIVE-2967: Upgrade the deprecated datetime adapter #5313

Open
wants to merge 5 commits into
base: wip-NXDRIVE-2929-upgrade-python-from-3.9.5-to-3.12.3
Choose a base branch
from

Conversation

gitofanindya
Copy link
Collaborator

@gitofanindya gitofanindya commented Oct 7, 2024

Summary by Sourcery

Upgrade the handling of datetime objects in SQL execution to address deprecation warnings by implementing a custom adapter, and update test configurations to reflect this change.

Bug Fixes:

  • Fix the handling of datetime parameters in SQL execution by registering a custom adapter for datetime objects.

Tests:

  • Remove suppression of warnings related to the deprecated datetime adapter in test configuration.

Copy link
Contributor

sourcery-ai bot commented Oct 7, 2024

Reviewer's Guide by Sourcery

This pull request upgrades the deprecated datetime adapter in the nxdrive project. The main changes involve modifying the execute method in the base.py file to handle datetime objects differently when passing parameters to SQL queries. Additionally, some warning suppressions related to the deprecated datetime adapter have been removed from the conftest.py file.

No diagrams generated as the changes look simple and do not need a visual representation.

File-Level Changes

Change Details Files
Modify the execute method to handle datetime objects
  • Add a check for parameters in the execute method
  • Import datetime and sqlite3 modules
  • Iterate through parameters to identify datetime objects
  • Register a custom adapter for datetime objects
  • Create a new parameter list with adapted datetime values
  • Execute the SQL query with the new parameter list
nxdrive/dao/base.py
Remove warning suppressions for deprecated datetime adapter
  • Remove suppression for 'The default datetime adapter is deprecated as of Python 3.12' warning
  • Remove suppression for 'datetime.datetime' warning
tests/conftest.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

codecov bot commented Oct 7, 2024

Codecov Report

Attention: Patch coverage is 68.75000% with 5 lines in your changes missing coverage. Please review.

Project coverage is 47.80%. Comparing base (462f0a0) to head (1fc22e7).

Files with missing lines Patch % Lines
nxdrive/client/local/base.py 50.00% 4 Missing ⚠️
nxdrive/dao/base.py 87.50% 1 Missing ⚠️
Additional details and impacted files
@@                                   Coverage Diff                                    @@
##           wip-NXDRIVE-2929-upgrade-python-from-3.9.5-to-3.12.3    #5313      +/-   ##
========================================================================================
- Coverage                                                 50.58%   47.80%   -2.78%     
========================================================================================
  Files                                                        96       96              
  Lines                                                     16109    16121      +12     
========================================================================================
- Hits                                                       8148     7706     -442     
- Misses                                                     7961     8415     +454     
Flag Coverage Δ
functional 37.85% <68.75%> (-4.99%) ⬇️
integration 2.00% <0.00%> (-0.01%) ⬇️
unit 40.30% <56.25%> (+1.64%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @gitofanindya - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider moving the datetime handling logic into a separate method to improve readability and maintainability of the execute() function.
  • Avoid importing modules (datetime, sqlite3) inside the execute() method. Move these imports to the top of the file to prevent potential performance issues.
Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🟢 Security: all looks good
  • 🟡 Testing: 1 issue found
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines 34 to 36
def adapt_datetime_iso(val):
return datetime.datetime.fromtimestamp(str(val), datetime.UTC)
mtime = sqlite3.register_adapter(param, adapt_datetime_iso)
Copy link
Contributor

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

Comment on lines 31 to 41
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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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)

def adapt_datetime_iso(val):
    return datetime.datetime.fromtimestamp(str(val), datetime.UTC)

sqlite3.register_adapter(datetime.datetime, adapt_datetime_iso)

new_param = tuple(
    adapt_datetime_iso(param) if isinstance(param, datetime.datetime) else param
    for param in parameters
)

Comment on lines -98 to -99
elif "The default datetime adapter is deprecated as of Python 3.12" in message:
continue
Copy link
Contributor

Choose a reason for hiding this comment

The 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 execute method of nxdrive/dao/base.py.

@pytest.fixture(autouse=True)
def ignore_warnings(request):
    warnings.filterwarnings("ignore", category=DeprecationWarning)
    warnings.filterwarnings("ignore", category=ResourceWarning)
    yield
    warnings.resetwarnings()

def test_datetime_handling():
    from nxdrive.dao.base import execute
    # Add test logic here to verify datetime handling

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.

1 participant