Skip to content

feat: dataclass implementation #307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft

Conversation

APN-Pucky
Copy link
Member

@APN-Pucky APN-Pucky commented Aug 17, 2025

I quickly drafted a demo implementation of dataclasses instead of dicts. I considered

  1. Backwards compatibility functions:
  • __setitem__ and __getitem__ to mimic old dict-behavior like e.g. self["beamA"], but with a deprecation warning in order to get people to change their code.
  1. Minimal modifications to the tests to maximize backwards compatibility:
  • RuntimeError -> TypeError
  • BREAKING CHANGE: Since the classes do not inherit from dict anymore we can not directly compare against a dict, however the dataclass.asdict can be used in the tests now.
  • BREAKING CHANGE: LHEInit and LHEProcInfo previously could be instantiated without parameters, but I don't think that makes sense. Turning all their dataclass fields to optional also is ugly in my eyes.
  1. Removal of fieldnames variable, but added again for backwards compatibility as a dynamic @property that reads the fields.

TODOs and considerations:

  • one could add deprecation warnings to fieldnames() property and __setitem__/__getitem__.
  • LHEVersion should not be a float (stupid AI...)
  • => Test value of LHEVersion
  • Add tests of the fieldnames() property to be the same as previously
  • Coverage decrease, due to untested backwards-compatibility functions ... => corrected by adding backwards-compatibility tests

Closes: #303

Copy link

codecov bot commented Aug 17, 2025

Codecov Report

❌ Patch coverage is 98.36066% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 95.82%. Comparing base (55ec452) to head (7c9aea8).

Files with missing lines Patch % Lines
src/pylhe/__init__.py 98.36% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #307      +/-   ##
==========================================
+ Coverage   95.10%   95.82%   +0.72%     
==========================================
  Files           2        2              
  Lines         347      407      +60     
  Branches       63       59       -4     
==========================================
+ Hits          330      390      +60     
  Misses         10       10              
  Partials        7        7              
Flag Coverage Δ
unittests-3.10 95.82% <98.36%> (+0.72%) ⬆️
unittests-3.11 95.82% <98.36%> (+0.72%) ⬆️
unittests-3.12 95.82% <98.36%> (+0.72%) ⬆️
unittests-3.13 95.82% <98.36%> (+0.72%) ⬆️
unittests-3.9 95.82% <98.36%> (+0.72%) ⬆️

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

☔ View full report in Codecov by Sentry.
📢 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.

@APN-Pucky APN-Pucky requested a review from Copilot August 17, 2025 20:16
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR converts the LHE data classes from dict-based implementations to proper dataclasses for improved type safety and clarity. The implementation maintains backwards compatibility through dictionary-like access methods and properties.

  • Converts LHEEvent, LHEEventInfo, LHEParticle, LHEInitInfo, LHEProcInfo, LHEInit, and LHEFile to dataclasses with proper type annotations
  • Adds backwards compatibility methods (getitem, setitem) with deprecation warnings for dict-like access
  • Changes LHEVersion from float to string type and updates related tests

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/pylhe/init.py Core implementation converting classes to dataclasses with backwards compatibility methods
tests/test_classes.py Updated tests for TypeError exceptions, added backwards compatibility tests, and dataclass comparison updates
tests/test_lhe_reader.py Updated LHEVersion test assertion from float to string comparison

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

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.

transition from dict to dataclass?
1 participant