Skip to content

LCORE-1272: explicit datetime adapter#1102

Merged
tisnik merged 1 commit intolightspeed-core:mainfrom
tisnik:lcore-1272
Feb 4, 2026
Merged

LCORE-1272: explicit datetime adapter#1102
tisnik merged 1 commit intolightspeed-core:mainfrom
tisnik:lcore-1272

Conversation

@tisnik
Copy link
Contributor

@tisnik tisnik commented Feb 4, 2026

Description

LCORE-1272: explicit datetime adapter

Type of change

  • Refactor
  • New feature
  • Bug fix
  • CVE fix
  • Optimization
  • Documentation Update
  • Configuration Update
  • Bump-up service version
  • Bump-up dependent library
  • Bump-up library or tool used for development (does not change the final image)
  • CI configuration change
  • Konflux configuration change
  • Unit tests improvement
  • Integration tests improvement
  • End to end tests improvement

Tools used to create PR

  • Assisted-by: N/A
  • Generated by: N/A

Related Tickets & Documents

  • Related Issue #LCORE-1272

Summary by CodeRabbit

  • Bug Fixes
    • Improved date/time data handling to ensure compatibility with Python 3.12 and prevent deprecated functionality warnings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 4, 2026

Walkthrough

A custom datetime adapter is registered for SQLite connections in quota_limiter.py to serialize datetime objects as ISO format strings. This change accommodates Python 3.12's deprecation of default adapters and converters, occurring after connection establishment but before table initialization.

Changes

Cohort / File(s) Summary
SQLite Datetime Adapter
src/quota/quota_limiter.py
Adds datetime import and registers custom adapter to serialize datetime objects as ISO format strings for SQLite, ensuring Python 3.12 compatibility.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~2 minutes

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'LCORE-1272: explicit datetime adapter' is fully related to the changeset, clearly summarizing the main change: adding an explicit datetime adapter for SQLite.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@src/quota/quota_limiter.py`:
- Around line 152-157: The SQLite adapter registered via
sqlite3.register_adapter for datetime.datetime currently strips tzinfo with
val.replace(tzinfo=None); remove that tzinfo stripping and change the adapter
lambda to return val.isoformat() so timezone information is preserved (i.e.,
update the lambda in the sqlite3.register_adapter call to call val.isoformat()
directly).

Comment on lines +152 to +157
# the default adapters and converters are deprecated as of Python
# 3.12. Instead, we use the Adapter and converter recipes and
# tailor them to our needs.
sqlite3.register_adapter(
datetime.datetime, lambda val: val.replace(tzinfo=None).isoformat()
)
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

# First, let's check the file exists and read the relevant section
cat -n src/quota/quota_limiter.py | sed -n '140,170p'

Repository: lightspeed-core/lightspeed-stack

Length of output: 1658


🏁 Script executed:

# Let's also look at the broader context of this file to understand imports and usage
head -50 src/quota/quota_limiter.py

Repository: lightspeed-core/lightspeed-stack

Length of output: 2020


🏁 Script executed:

# Check if there are any other references to this adapter or datetime handling in the quota module
rg -n "datetime\|timezone\|tzinfo" src/quota/quota_limiter.py -A 2 -B 2

Repository: lightspeed-core/lightspeed-stack

Length of output: 58


🏁 Script executed:

# Look for usages of this quota_limiter module to understand how datetimes are passed
rg -r "from.*quota_limiter|import.*quota_limiter" --type py -l

Repository: lightspeed-core/lightspeed-stack

Length of output: 472


🏁 Script executed:

# Check if datetime values are being stored with timezone info
rg -n "datetime.datetime" src/quota/ -A 2 -B 2

Repository: lightspeed-core/lightspeed-stack

Length of output: 407


🏁 Script executed:

# Let's look for where datetimes are actually created/passed in quota module
rg "datetime\.datetime" src/quota/ -A 3 -B 3

Repository: lightspeed-core/lightspeed-stack

Length of output: 528


🏁 Script executed:

# Check for any timezone-aware datetime creation
rg -n "datetime\.now\|datetime\.utcnow\|timezone\|tzinfo" src/quota/ -A 2 -B 2

Repository: lightspeed-core/lightspeed-stack

Length of output: 58


🏁 Script executed:

# Look for INSERT/UPDATE statements to see what values are being stored
rg -n "INSERT\|UPDATE\|VALUES" src/quota/quota_limiter.py -A 3 -B 3

Repository: lightspeed-core/lightspeed-stack

Length of output: 58


🏁 Script executed:

# Check if there are any timestamp/datetime columns defined in table creation
rg -n "_initialize_tables\|CREATE TABLE\|TIMESTAMP\|datetime" src/quota/quota_limiter.py -A 5 -B 2

Repository: lightspeed-core/lightspeed-stack

Length of output: 58


🏁 Script executed:

# Look at the entire _initialize_tables method to understand table schema
rg -n "def _initialize_tables" src/quota/quota_limiter.py -A 50

Repository: lightspeed-core/lightspeed-stack

Length of output: 2444


🏁 Script executed:

# Look for concrete implementations of QuotaLimiter to see actual table creation
find src/quota -name "*.py" -type f | head -20

Repository: lightspeed-core/lightspeed-stack

Length of output: 397


🏁 Script executed:

# Check concrete quota limiter implementations
ls -la src/quota/

Repository: lightspeed-core/lightspeed-stack

Length of output: 1025


🏁 Script executed:

# Look for any class that inherits from QuotaLimiter to see actual usage
rg "class.*QuotaLimiter" src/quota/ -A 20

Repository: lightspeed-core/lightspeed-stack

Length of output: 7660


🏁 Script executed:

# Search for INSERT statements and datetime usage in the entire quota directory
rg -n "INSERT INTO\|datetime\|now()" src/quota/ --type py

Repository: lightspeed-core/lightspeed-stack

Length of output: 58


🏁 Script executed:

# Check for any test files that might show how datetimes are used
fd "test.*quota\|quota.*test" --type f src/

Repository: lightspeed-core/lightspeed-stack

Length of output: 58


🏁 Script executed:

# Check revokable_quota_limiter.py to see actual table creation and datetime usage
cat -n src/quota/revokable_quota_limiter.py | head -100

Repository: lightspeed-core/lightspeed-stack

Length of output: 4498


🏁 Script executed:

# Look for _initialize_tables implementation
rg -n "def _initialize_tables" src/quota/ -A 30

Repository: lightspeed-core/lightspeed-stack

Length of output: 6561


🏁 Script executed:

# Look for actual datetime usage with insert/update operations
cat -n src/quota/revokable_quota_limiter.py | tail -200

Repository: lightspeed-core/lightspeed-stack

Length of output: 8898


🏁 Script executed:

# Check token_usage_history.py for datetime handling
cat -n src/quota/token_usage_history.py

Repository: lightspeed-core/lightspeed-stack

Length of output: 8751


🏁 Script executed:

# Look for how datetime objects are created and used
rg -n "datetime\.now\|datetime\.datetime\|utcnow" src/quota/ -B 3 -A 3

Repository: lightspeed-core/lightspeed-stack

Length of output: 58


🏁 Script executed:

# Check SQL module to see the table schema and how timestamps are used
cat -n src/quota/sql.py

Repository: lightspeed-core/lightspeed-stack

Length of output: 5477


🏁 Script executed:

# Verify the import statement for datetime
rg -n "from datetime import|import datetime" src/quota/ -A 0

Repository: lightspeed-core/lightspeed-stack

Length of output: 255


🏁 Script executed:

# Check if datetime.now with timezone is used anywhere in the codebase
rg -n "datetime\.now.*tz\|timezone\.utc\|timezone\." src/ --type py

Repository: lightspeed-core/lightspeed-stack

Length of output: 58


🏁 Script executed:

# Look for any timezone-aware datetime creation
rg -n "timezone\|tzinfo\|astimezone\|replace.*tzinfo" src/ --type py

Repository: lightspeed-core/lightspeed-stack

Length of output: 58


Remove redundant tzinfo stripping in SQLite adapter.

replace(tzinfo=None) is unnecessary since datetime.now() already returns naive datetimes. Removing it makes the intent clearer and would preserve timezone information if aware datetimes are ever passed in the future.

Suggested fix
            sqlite3.register_adapter(
                datetime.datetime, lambda val: val.isoformat()
            )
🤖 Prompt for AI Agents
In `@src/quota/quota_limiter.py` around lines 152 - 157, The SQLite adapter
registered via sqlite3.register_adapter for datetime.datetime currently strips
tzinfo with val.replace(tzinfo=None); remove that tzinfo stripping and change
the adapter lambda to return val.isoformat() so timezone information is
preserved (i.e., update the lambda in the sqlite3.register_adapter call to call
val.isoformat() directly).

@tisnik tisnik merged commit 0e5455a into lightspeed-core:main Feb 4, 2026
21 of 22 checks passed
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