Skip to content

🧹 refactor: use idiomatic sqlite result handling in PersistentDict#83

Open
clayauld wants to merge 3 commits intomainfrom
refactor-persistent-dict-idiomatic-sqlite-7134018262499593634
Open

🧹 refactor: use idiomatic sqlite result handling in PersistentDict#83
clayauld wants to merge 3 commits intomainfrom
refactor-persistent-dict-idiomatic-sqlite-7134018262499593634

Conversation

@clayauld
Copy link
Owner

@clayauld clayauld commented Mar 6, 2026

🎯 What: The code health issue addressed

  • Refactored PersistentDict to use sqlite3.Row for its connection row_factory.
  • Updated __getitem__, __iter__, and __len__ to use named column access and more concise patterns.
  • Removed redundant manual cursor mapping in __iter__.

💡 Why: How this improves maintainability

  • Enhances code readability by using named columns (e.g., row["key"]) instead of numerical indices.
  • Simplifies the implementation of dictionary methods, making the code more idiomatic and easier to maintain.
  • Reduces boilerplate code associated with manual cursor management.

Verification: How you confirmed the change is safe

  • Ran the dedicated test suite for PersistentDict: pytest tests/test_persistent_dict.py. All 8 tests passed.
  • Verified that sqlite3.Row maintains compatibility with index-based access, ensuring no regressions.

Result: The improvement achieved

  • A cleaner, more maintainable PersistentDict implementation that follows Python best practices for SQLite interaction.

PR created automatically by Jules for task 7134018262499593634 started by @clayauld

- Set row_factory to sqlite3.Row in _connect
- Refactor __getitem__ and __iter__ to use named column access
- Simplify __len__ implementation by combining execute and fetchone
- Remove redundant manual cursor mapping in __iter__

Co-authored-by: clayauld <2290528+clayauld@users.noreply.github.com>
@google-labs-jules
Copy link
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@github-actions
Copy link

github-actions bot commented Mar 6, 2026

🤖 Automated Code Review

🔍 Linting Results Summary

✅ Black (Code Formatting) - PASSED

✅ Flake8 (Code Quality) - PASSED

✅ isort (Import Sorting) - PASSED

⚠️ MyPy (Type Checking)

src/persistent_dict.py:167: error: Returning Any from function declared to return "int"  [no-any-return]

Fix: Add missing type annotations and fix type errors.

✅ Bandit (Security) - PASSED

✅ All Linting Checks Passed!


This comment was automatically generated by GitHub Actions. Check the Actions tab for detailed logs.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refactors the PersistentDict class to enhance its interaction with SQLite, making the code more Pythonic and maintainable. By configuring the SQLite connection to use sqlite3.Row, the implementation now benefits from named column access, which significantly improves the clarity of database operations within the dictionary methods. This change simplifies the codebase, reduces potential for errors, and aligns the PersistentDict with best practices for SQLite usage in Python.

Highlights

  • sqlite3.Row Integration: The PersistentDict class now utilizes sqlite3.Row as its connection's row_factory, enabling named column access for query results.
  • Method Refactoring: The getitem, iter, and len methods have been updated to leverage named column access and more concise patterns, improving readability and reducing boilerplate.
  • Cursor Management Simplification: Manual cursor mapping in iter has been removed, streamlining the iteration process.
Changelog
  • src/persistent_dict.py
    • Configured the SQLite connection to use sqlite3.Row as its row_factory.
    • Updated getitem to directly execute the query and fetch the row, then access the value by name (row["value"]).
    • Refactored iter to use a generator expression for yielding keys, accessing them by name (row["key"]), and removing explicit cursor assignment.
    • Streamlined len by directly executing the count query and fetching the result without an intermediate cursor variable.
Activity
  • The pull request was automatically created by Jules for a task initiated by @clayauld.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@github-actions
Copy link

github-actions bot commented Mar 6, 2026

📊 Pull Request Analysis Report

🧪 Test Results

Metric Value
Total Tests 111
✅ Passed 111
❌ Failed 0
⏭️ Skipped 0
💥 Errors 0
📈 Success Rate 100.0%

📋 Coverage Report

Metric Value
Overall Coverage 89.6%
Total Lines 767
Covered Lines 687
Missing Lines 80

📚 Documentation Report

Metric Value
Documentation Coverage 100.0%
Status ✅ Up to date

📁 File Coverage Breakdown

  • config/__init__.py: 100.0%
  • config/config.py: 95.3%
  • src/__init__.py: 100.0%
  • src/caltopo_reporter.py: 95.0%
  • src/gateway.py: 84.4%
  • src/gateway_app.py: 83.6%
  • src/mqtt_client.py: 92.0%
  • src/persistent_dict.py: 90.4%
  • src/utils.py: 100.0%

🎯 Recommendations

✅ Excellent Code Quality

  • Great work maintaining high test pass rate and coverage!

📊 Analysis generated by GitHub Actions • View detailed test report

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request is a great refactoring that modernizes the PersistentDict class by using sqlite3.Row. This change makes the code more readable and idiomatic Python by enabling named-column access. The updates to __getitem__ and __iter__ are excellent examples of this improvement. I have one suggestion for the __len__ method to improve its consistency with the rest of your changes and simplify the implementation.

google-labs-jules bot and others added 2 commits March 6, 2026 09:05
- Set row_factory to sqlite3.Row in _connect
- Refactor __getitem__ and __iter__ to use named column access
- Simplify __len__ implementation by combining execute and fetchone
- Remove redundant manual cursor mapping in __iter__

Co-authored-by: clayauld <2290528+clayauld@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
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