Skip to content

Raise exceptions directly instead of logging-and-raising - #58

Merged
galjos merged 2 commits into
mainfrom
refactor-log-and-raise
Jun 29, 2026
Merged

Raise exceptions directly instead of logging-and-raising#58
galjos merged 2 commits into
mainfrom
refactor-log-and-raise

Conversation

@galjos

@galjos galjos commented Jun 29, 2026

Copy link
Copy Markdown
Member

Replaces the package's log-and-raise control-flow pattern with explicit raise, removing a real footgun.

Problem

40 call sites did self.logger.error("msg", exception=TSValueError), where the custom logger raises the exception as a side effect of logging. That:

  • reads as logging when it is actually control flow;
  • depends on which package last won logging.setLoggerClass (PQAnalysis's CustomLogger also raises on .error, so the behaviour is non-obvious);
  • hides the control flow from pylint — every such function looked like it could fall through (13 inconsistent-return-statements).

I hit this twice while building screening (had to log failures at warning, and a %-format crash).

Change

  • self.logger.error("msg", exception=ExcType) -> raise ExcType("msg") across api.py, system.py, thermo.py, atoms.py, inputFileReader.py (40 sites, net -121 lines).
  • Exception types and messages are unchanged, so every pytest.raises(..., match=...) / assert str(e.value) == ... still holds.
  • except: lookups now raise ... from None (no misleading chain).
  • Fixes a latent bug in dimensionality(): a 0D branch constructed a ValueError without raising it (silently returned None); it now raises.

Result

  • Clears inconsistent-return-statements (13 -> 0), plus the no-else-raise/raise-missing-from the change would otherwise introduce.
  • pylint 8.79 -> 8.93; full suite 159 passed, 0 skipped (with the DFTB+ binaries).

Note

This removes ThermoScreening's dependence on log-and-raise. The underlying logger machinery (the exception= param in custom_logging.py) is left in place but now unused by the package; screening.py still logs failures at warning because the active logger still raises on .error. Fully untangling the dual CustomLogger is a separate, larger change.

Replace the 40 logger.error("msg", exception=ExcType) call sites with plain
raise ExcType("msg"). The custom logger raised the exception as a side effect of
logging, which (a) reads as logging when it is really control flow, (b) depends
on which package last won logging.setLoggerClass, and (c) hid the control flow
from pylint (every such function looked like it could fall through). Exception
types and messages are unchanged, so the pytest.raises assertions are preserved.

Also fixes a latent bug in dimensionality(): a 0D branch built a ValueError
without raising it (and fell through returning None); it now raises.

Clears inconsistent-return-statements, no-else-raise and raise-missing-from;
pylint 8.79 -> 8.93. No behaviour change (full suite green with binaries).
@codecov

codecov Bot commented Jun 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.93%. Comparing base (eacd9ea) to head (8de892b).

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #58      +/-   ##
==========================================
+ Coverage   95.25%   95.93%   +0.68%     
==========================================
  Files          22       22              
  Lines        1286     1280       -6     
==========================================
+ Hits         1225     1228       +3     
+ Misses         61       52       -9     
Flag Coverage Δ
unittests 95.93% <100.00%> (+0.68%) ⬆️

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

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Add tests for the reachable Thermo.__init__ guards (unsupported engine,
negative temperature, negative pressure). Mark as no-cover the guards that are
unreachable while beartype enforces parameter types (the None checks and the
dead _transform_units branch) and the unreachable 0D branch in dimensionality.
@galjos
galjos merged commit 33b6469 into main Jun 29, 2026
3 checks passed
@galjos
galjos deleted the refactor-log-and-raise branch June 29, 2026 07:54
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