Skip to content

refactor: simplify parsing internals and fix duplicate deprecation warnings#384

Open
henryiii wants to merge 1 commit into
mainfrom
refactor-internal-cleanups
Open

refactor: simplify parsing internals and fix duplicate deprecation warnings#384
henryiii wants to merge 1 commit into
mainfrom
refactor-internal-cleanups

Conversation

@henryiii

Copy link
Copy Markdown
Member

🤖 AI text below 🤖

Summary

Internal-only cleanups to src/pylhe/__init__.py — no public API changes.

  • Cleanup 1 — collapse duplicate generator-parsing branches: LHEInit._fromcontext had two near-identical branches for lhe-v3 (name + version) and lhe-v2 (version only). Collapsed into a single LHEGenerator(name=element.attrib.get("name", ""), ...) construction; behavior is identical.
  • Cleanup 2 — replace _indent with stdlib ET.indent: Deleted the Stack Overflow copy-paste _indent helper (added pre-3.9) and replaced both call sites with ET.indent(root, space=" ") + root.tail = "\n". Output is byte-identical; confirmed by existing writer tests.
  • Cleanup 3 — annotation and dead-logic fixes:
    • LHEHeader._fromcontext return type narrowed from Union["LHEHeader", None] to "LHEHeader" (it never returns None).
    • All three _fromcontext context parameters typed from Any to Iterator[tuple[str, ET.Element]].
    • Removed dead if w guard in <weights> parsing (items from str.split() are never falsy).
    • Replaced bare KeyError when <weights> has more entries than <initrwgt> declares with a clear ValueError including entry counts.
  • Cleanup 4 — mothers() emits exactly one warning: LHEParticle.mothers() was internally accessing self.event (the deprecated property) three times, emitting 3 extra DeprecationWarnings per call. Changed to use self._event directly. Test updated to assert exactly one warning per call.

Part of #378

Test plan

  • uv run pytest — 121 passed, 0 failed
  • uv run mypy — no issues
  • prek -a --quiet — all hooks pass

🤖 Generated with Claude Code

…rnings

- Collapse duplicate generator-parsing branches in LHEInit._fromcontext
  into a single construction using element.attrib.get("name", "")
- Replace hand-rolled _indent helper with stdlib ET.indent (Python 3.9+),
  preserving byte-identical output by setting root.tail = "\n"
- Tighten type annotations: _fromcontext context params typed as
  Iterator[tuple[str, ET.Element]]; LHEHeader._fromcontext return type
  narrowed from Union["LHEHeader", None] to "LHEHeader"
- Remove dead falsy check on w in weights parsing (str.split() items
  are never empty); raise clear ValueError when <weights> block has more
  entries than <initrwgt> declares (was a cryptic KeyError)
- Fix LHEParticle.mothers() to use self._event internally so it emits
  exactly one DeprecationWarning per call instead of four; add assertion
  to test suite verifying the warning count

Assisted-by: ClaudeCode:claude-sonnet-4-6
@henryiii henryiii marked this pull request as ready for review June 12, 2026 17:57
@github-actions

Copy link
Copy Markdown

🏎️ Benchmark Comparison


--------------------------------------------------------------------------------------------------- benchmark: 4 tests --------------------------------------------------------------------------------------------------
Name (time in ms)                                   Min                   Max                  Mean             StdDev                Median                IQR            Outliers     OPS            Rounds  Iterations
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
test_count_events_benchmark (0002_mr)          417.2039 (1.0)        426.6482 (1.0)        419.6252 (1.0)       2.7391 (2.26)       419.0451 (1.0)       1.9823 (1.45)          1;1  2.3831 (1.0)          10           1
test_count_events_benchmark (0001_base)        423.1277 (1.01)       427.2827 (1.00)       424.5258 (1.01)      1.2118 (1.0)        424.3578 (1.01)      1.3648 (1.0)           3;1  2.3556 (0.99)         10           1
test_fromfile_and_to_awkward (0002_mr)       3,358.2184 (8.05)     3,401.6349 (7.97)     3,376.6169 (8.05)     14.4294 (11.91)    3,371.8502 (8.05)     23.5574 (17.26)         3;0  0.2962 (0.12)         10           1
test_fromfile_and_to_awkward (0001_base)     3,453.2945 (8.28)     3,486.5743 (8.17)     3,468.3276 (8.27)     11.1177 (9.17)     3,470.6981 (8.28)     19.8130 (14.52)         4;0  0.2883 (0.12)         10           1
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Legend:
  Outliers: 1 Standard Deviation from Mean; 1.5 IQR (InterQuartile Range) from 1st Quartile and 3rd Quartile.
  OPS: Operations Per Second, computed as 1 / Mean

@codecov

codecov Bot commented Jun 12, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 86.66667% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 99.66%. Comparing base (7e7441b) to head (1376785).

Files with missing lines Patch % Lines
src/pylhe/__init__.py 86.66% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##              main     #384      +/-   ##
===========================================
- Coverage   100.00%   99.66%   -0.34%     
===========================================
  Files            2        2              
  Lines          608      597      -11     
===========================================
- Hits           608      595      -13     
- Misses           0        2       +2     
Flag Coverage Δ
unittests-3.10 99.66% <86.66%> (-0.34%) ⬇️
unittests-3.11 99.66% <86.66%> (-0.34%) ⬇️
unittests-3.12 99.66% <86.66%> (-0.34%) ⬇️
unittests-3.13 99.66% <86.66%> (-0.34%) ⬇️
unittests-3.14 99.63% <86.66%> (-0.37%) ⬇️
unittests-3.9 99.62% <86.66%> (-0.38%) ⬇️

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.

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