Skip to content

fixing pytest and pre-commit warnings and consistency fixes#306

Merged
ParticularlyPythonicBS merged 5 commits intounstablefrom
fix/pytest_warning
Apr 10, 2026
Merged

fixing pytest and pre-commit warnings and consistency fixes#306
ParticularlyPythonicBS merged 5 commits intounstablefrom
fix/pytest_warning

Conversation

@ParticularlyPythonicBS
Copy link
Copy Markdown
Member

@ParticularlyPythonicBS ParticularlyPythonicBS commented Apr 10, 2026

Summary by CodeRabbit

  • Chores

    • Normalized trailing whitespace and line endings across docs, configs, and static assets.
    • Updated pre-commit export hooks and related configuration.
  • Refactor

    • Standardized resource management to ensure deterministic cleanup of database and writer resources.
  • Tests

    • Updated test suite to follow the improved resource-handling patterns and formatting.

@ParticularlyPythonicBS ParticularlyPythonicBS added the Maintenance Code quality fixes and deprecation management label Apr 10, 2026
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 10, 2026

Warning

Rate limit exceeded

@ParticularlyPythonicBS has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 10 minutes and 2 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 10 minutes and 2 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 4a0cd08b-b0da-4e1c-a883-0bf035369c80

📥 Commits

Reviewing files that changed from the base of the PR and between 2b5b153 and b06a653.

⛔ Files ignored due to path filters (1)
  • docs/source/images/TemoaLogo.svg is excluded by !**/*.svg
📒 Files selected for processing (36)
  • .github/ISSUE_TEMPLATE/bug_report.md
  • .pre-commit-config.yaml
  • docs/source/References.bib
  • docs/source/default/layout.html
  • docs/source/default/static/google_analytics.js
  • docs/source/default/static/my_theme.css
  • docs/source/mga.rst
  • docs/technology groups.md
  • output_files/.gitignore
  • temoa/_internal/run_actions.py
  • temoa/_internal/temoa_sequencer.py
  • temoa/cli.py
  • temoa/data_processing/README.md
  • temoa/extensions/method_of_morris/morris.py
  • temoa/extensions/method_of_morris/morris_evaluate.py
  • temoa/extensions/method_of_morris/morris_sequencer.py
  • temoa/extensions/modeling_to_generate_alternatives/MGA Future Development.md
  • temoa/extensions/modeling_to_generate_alternatives/MGA Solve Process Notes.md
  • temoa/extensions/modeling_to_generate_alternatives/make_flow_summary_table.sql
  • temoa/extensions/monte_carlo/mc_sequencer.py
  • temoa/extensions/monte_carlo/monte carlo design.md
  • temoa/extensions/stochastics/scenario_creator.py
  • temoa/model_checking/unit_checking/screener.py
  • temoa/types/dict_types.py
  • temoa/utilities/clear_db_outputs.py
  • temoa/utilities/master_migration.py
  • temoa/utilities/run_all_v4_migrations.py
  • tests/conftest.py
  • tests/test_cli.py
  • tests/test_emission_results.py
  • tests/test_full_runs.py
  • tests/test_linked_tech.py
  • tests/test_material_results.py
  • tests/test_unit_propagation.py
  • tests/test_v4_migration.py
  • tests/testing_outputs/README.txt

Walkthrough

Refactors SQLite connection management to use contextlib.closing(...) and wraps TableWriter usage with with-statements across the codebase. Also applies widespread whitespace/newline normalization, minor formatting changes (type alias, exception quote style), and updates pre-commit hooks to use uv-export. No public APIs changed.

Changes

Cohort / File(s) Summary
Documentation & static assets
​.github/ISSUE_TEMPLATE/bug_report.md, docs/source/References.bib, docs/source/default/layout.html, docs/source/default/static/google_analytics.js, docs/source/default/static/my_theme.css, docs/source/mga.rst, docs/technology groups.md, output_files/.gitignore, temoa/data_processing/README.md, temoa/extensions/modeling_to_generate_alternatives/*, temoa/extensions/monte_carlo/monte carlo design.md, tests/testing_outputs/README.txt
Whitespace and newline normalization: removed trailing spaces/blank lines, added missing final newlines, reflowed line breaks. No semantic changes.
Pre-commit config
.pre-commit-config.yaml
Replaced pip-compile hooks with uv-export hooks and expanded hook file triggers to include uv.lock. Hook ids/names and command-line args updated; behavior unchanged aside from tool used.
Core sequencer & actions
temoa/_internal/temoa_sequencer.py, temoa/_internal/run_actions.py, temoa/cli.py
Switched SQLite connection usage from with sqlite3.connect(...) as con: to with contextlib.closing(sqlite3.connect(...)) as con: in multiple methods; TableWriter is now used via with TableWriter(...) as ...: in run_actions. Formatting/whitespace adjustments only elsewhere.
Extensions: Morris, MGA, Monte Carlo, Stochastics
temoa/extensions/method_of_morris/morris.py, .../morris_evaluate.py, .../morris_sequencer.py, temoa/extensions/monte_carlo/mc_sequencer.py, temoa/extensions/stochastics/scenario_creator.py
Replaced direct sqlite3 context-manager usage with contextlib.closing(...) and wrapped TableWriter usages in context managers where applicable. SQL and logic preserved.
Model checking & utilities
temoa/model_checking/unit_checking/screener.py, temoa/utilities/clear_db_outputs.py, temoa/utilities/master_migration.py, temoa/utilities/run_all_v4_migrations.py
Standardized DB connection closing using contextlib.closing(...); minor formatting changes (exception string quotes, function signature layout, print statement formatting). No behavioral changes.
Type alias formatting
temoa/types/dict_types.py
Reformatted EfficiencyVariableDict type alias from multi-line to single-line subscription. Signature unchanged.
Tests: DB resource handling & whitespace
tests/conftest.py, tests/test_cli.py, tests/test_emission_results.py, tests/test_full_runs.py, tests/test_linked_tech.py, tests/test_material_results.py, tests/test_unit_propagation.py, tests/test_v4_migration.py, tests/...
Updated tests and fixtures to use contextlib.closing(sqlite3.connect(...)) for deterministic connection closing; added local import contextlib where needed. Also applied whitespace/formatting adjustments in several tests. Assertions and test logic unchanged.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~28 minutes

Possibly related PRs

Suggested labels

refactor

🚥 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 accurately describes the primary changes: fixing pytest warnings (unclosed database resources) and consistency fixes throughout the codebase.
Docstring Coverage ✅ Passed Docstring coverage is 80.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 unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/pytest_warning

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
Copy Markdown

@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: 4

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
temoa/extensions/method_of_morris/morris.py (1)

50-57: ⚠️ Potential issue | 🔴 Critical

Cursor is used after connection close (runtime failure).

cur.execute() at line 56 runs outside the with contextlib.closing() block that closes at line 53. The connection is already closed, causing a database operational error at runtime. The second query and all cursor operations after line 53 must be moved inside the with block.

🐛 Proposed fix
     with contextlib.closing(sqlite3.connect(db_file)) as con:
         cur = con.cursor()
         cur.execute('SELECT * FROM output_objective')
         output_query = cur.fetchall()
+        for row in output_query:
+            y_of = row[-1]
+        cur.execute("SELECT emis_comm, SUM(emission) FROM output_emission WHERE emis_comm='co2'")
+        output_query = cur.fetchall()
+        for row in output_query:
+            y_cumulative_co2 = row[-1]
-    for row in output_query:
-        y_of = row[-1]
-    cur.execute("SELECT emis_comm, SUM(emission) FROM output_emission WHERE emis_comm='co2'")
-    output_query = cur.fetchall()
-    for row in output_query:
-        y_cumulative_co2 = row[-1]
     morris_objectives = []
     morris_objectives.append(y_of)
     morris_objectives.append(y_cumulative_co2)
-    con.close()
     return morris_objectives
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@temoa/extensions/method_of_morris/morris.py` around lines 50 - 57, The code
uses cur.execute("SELECT emis_comm, SUM(emission) FROM output_emission WHERE
emis_comm='co2'") after the with contextlib.closing(sqlite3.connect(db_file)) as
con: block has exited, causing the connection/cur to be closed; move the second
query and any cursor operations (the cur variable, the SELECT on output_emission
and the subsequent fetchall/use of output_query) inside the same with block
where con and cur are valid, ensuring y_of is set and all database access
happens before the with block closes (references: contextlib.closing,
sqlite3.connect, con, cur, output_query, y_of).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@docs/technology` groups.md:
- Around line 23-24: The sentence fragment beginning "specific names, and the
functionality should remain consistent. For RPS groups..." continues after the
comma with "The migration tool"; change that capitalization to lowercase ("the
migration tool") so the sentence reads as a single continuous sentence; update
the text where the phrase "The migration tool attempts to find a common set of
members..." appears to use "the" instead of "The".
- Line 13: The sentence contains a subject-verb agreement error: replace the
phrase "parameters that accepts" with "parameters that accept" so the plural
noun "parameters" matches the plural verb form; update the sentence fragment
"These group names can now be used in any of the parameters that accepts
technology groups as an index." to use "accept" instead of "accepts".

In `@temoa/cli.py`:
- Around line 587-590: Replace the use of
contextlib.closing(sqlite3.connect(target_database)) with the sqlite3.Connection
context manager so transactions are committed/rolled back properly: change the
"with contextlib.closing(sqlite3.connect(target_database)) as conn:" block that
calls conn.executescript(sql_content) to use "with
sqlite3.connect(target_database) as conn:" (or equivalently use the connection
object's context manager) and apply the same replacement for the fallback SQL
path that also uses contextlib.closing and sqlite3.connect at the other
occurrence; keep using executescript(sql_content) on the conn.

In `@temoa/extensions/modeling_to_generate_alternatives/MGA` Solve Process
Notes.md:
- Around line 30-31: Add a blank line after the markdown headings "####
Alternate (Current) Approach" and "#### Original Intent" so each heading is
followed by an empty line before the paragraph text (to satisfy markdownlint
MD022); locate those headings in the file and insert a single blank line
immediately after each heading line.

---

Outside diff comments:
In `@temoa/extensions/method_of_morris/morris.py`:
- Around line 50-57: The code uses cur.execute("SELECT emis_comm, SUM(emission)
FROM output_emission WHERE emis_comm='co2'") after the with
contextlib.closing(sqlite3.connect(db_file)) as con: block has exited, causing
the connection/cur to be closed; move the second query and any cursor operations
(the cur variable, the SELECT on output_emission and the subsequent fetchall/use
of output_query) inside the same with block where con and cur are valid,
ensuring y_of is set and all database access happens before the with block
closes (references: contextlib.closing, sqlite3.connect, con, cur, output_query,
y_of).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 9ca6652c-ae1d-44f1-99e8-a4d23096cfe6

📥 Commits

Reviewing files that changed from the base of the PR and between cf49164 and 4c889da.

⛔ Files ignored due to path filters (1)
  • docs/source/images/TemoaLogo.svg is excluded by !**/*.svg
📒 Files selected for processing (35)
  • .github/ISSUE_TEMPLATE/bug_report.md
  • docs/source/References.bib
  • docs/source/default/layout.html
  • docs/source/default/static/google_analytics.js
  • docs/source/default/static/my_theme.css
  • docs/source/mga.rst
  • docs/technology groups.md
  • output_files/.gitignore
  • temoa/_internal/run_actions.py
  • temoa/_internal/temoa_sequencer.py
  • temoa/cli.py
  • temoa/data_processing/README.md
  • temoa/extensions/method_of_morris/morris.py
  • temoa/extensions/method_of_morris/morris_evaluate.py
  • temoa/extensions/method_of_morris/morris_sequencer.py
  • temoa/extensions/modeling_to_generate_alternatives/MGA Future Development.md
  • temoa/extensions/modeling_to_generate_alternatives/MGA Solve Process Notes.md
  • temoa/extensions/modeling_to_generate_alternatives/make_flow_summary_table.sql
  • temoa/extensions/monte_carlo/mc_sequencer.py
  • temoa/extensions/monte_carlo/monte carlo design.md
  • temoa/extensions/stochastics/scenario_creator.py
  • temoa/model_checking/unit_checking/screener.py
  • temoa/types/dict_types.py
  • temoa/utilities/clear_db_outputs.py
  • temoa/utilities/master_migration.py
  • temoa/utilities/run_all_v4_migrations.py
  • tests/conftest.py
  • tests/test_cli.py
  • tests/test_emission_results.py
  • tests/test_full_runs.py
  • tests/test_linked_tech.py
  • tests/test_material_results.py
  • tests/test_unit_propagation.py
  • tests/test_v4_migration.py
  • tests/testing_outputs/README.txt
💤 Files with no reviewable changes (5)
  • temoa/data_processing/README.md
  • docs/source/References.bib
  • output_files/.gitignore
  • docs/source/default/static/google_analytics.js
  • docs/source/default/layout.html

Comment thread docs/technology groups.md Outdated
Comment thread docs/technology groups.md Outdated
Comment thread temoa/cli.py Outdated
@ParticularlyPythonicBS ParticularlyPythonicBS changed the title fixing pytest unclosed database resource warning fixing pytest and pre-commit warnings and consistency fixes Apr 10, 2026
Copy link
Copy Markdown

@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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
temoa/extensions/method_of_morris/morris.py (1)

49-63: 🧹 Nitpick | 🔵 Trivial

Remove redundant close after context-managed connection.

Line 63 manually closes con even though with contextlib.closing(...) already owns closure. Keep a single owner for connection lifecycle.

♻️ Proposed fix
@@
-    con.close()
     return morris_objectives
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@temoa/extensions/method_of_morris/morris.py` around lines 49 - 63, Remove the
redundant manual close: delete the explicit con.close() call and rely on the
contextlib.closing(sqlite3.connect(db_file)) as con block to manage connection
lifecycle; ensure that any use of con (queries that populate y_of and
y_cumulative_co2) happens inside the with block and that morris_objectives (and
variables y_of, y_cumulative_co2) are assembled only after those values are
retrieved so no code references con after the context manager scope.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@temoa/extensions/method_of_morris/morris.py`:
- Around line 49-63: Remove the redundant manual close: delete the explicit
con.close() call and rely on the contextlib.closing(sqlite3.connect(db_file)) as
con block to manage connection lifecycle; ensure that any use of con (queries
that populate y_of and y_cumulative_co2) happens inside the with block and that
morris_objectives (and variables y_of, y_cumulative_co2) are assembled only
after those values are retrieved so no code references con after the context
manager scope.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: ca011943-ec7d-475d-8a27-b96d71a38f0d

📥 Commits

Reviewing files that changed from the base of the PR and between 4c889da and 2b5b153.

📒 Files selected for processing (5)
  • .pre-commit-config.yaml
  • docs/technology groups.md
  • temoa/cli.py
  • temoa/extensions/method_of_morris/morris.py
  • temoa/extensions/modeling_to_generate_alternatives/MGA Solve Process Notes.md

- Wrapped short-lived connections in context handlers
- Bound TableWriter lifespan correctly for reliable closures
- Replaced explicit contextlib with core SQLite context managers in generation logic
@ParticularlyPythonicBS ParticularlyPythonicBS merged commit 3390585 into unstable Apr 10, 2026
13 checks passed
@ParticularlyPythonicBS ParticularlyPythonicBS deleted the fix/pytest_warning branch April 10, 2026 18:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Maintenance Code quality fixes and deprecation management refactor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant