LCORE-1272: explicit datetime adapter#1102
Conversation
WalkthroughA custom datetime adapter is registered for SQLite connections in Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~2 minutes 🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
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).
| # 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() | ||
| ) |
There was a problem hiding this comment.
🧩 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.pyRepository: 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 2Repository: 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 -lRepository: 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 2Repository: 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 3Repository: 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 2Repository: 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 3Repository: 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 2Repository: 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 50Repository: 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 -20Repository: 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 20Repository: 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 pyRepository: 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 -100Repository: lightspeed-core/lightspeed-stack
Length of output: 4498
🏁 Script executed:
# Look for _initialize_tables implementation
rg -n "def _initialize_tables" src/quota/ -A 30Repository: 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 -200Repository: 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.pyRepository: 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 3Repository: 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.pyRepository: 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 0Repository: 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 pyRepository: 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 pyRepository: 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).
Description
LCORE-1272: explicit datetime adapter
Type of change
Tools used to create PR
Related Tickets & Documents
Summary by CodeRabbit