Skip to content
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

fix: Add missing comma in LHEInit fieldnames #282

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

matthewfeickert
Copy link
Member

Fixes the bug in Issue #281

Not sure we have a good test at the moment though.

Copy link

codecov bot commented Feb 3, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.87%. Comparing base (7519967) to head (62e326a).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #282   +/-   ##
=======================================
  Coverage   91.87%   91.87%           
=======================================
  Files           2        2           
  Lines         320      320           
  Branches       64       64           
=======================================
  Hits          294      294           
  Misses         17       17           
  Partials        9        9           
Flag Coverage Δ
unittests-3.10 91.87% <100.00%> (ø)
unittests-3.11 91.87% <100.00%> (ø)
unittests-3.12 91.87% <100.00%> (ø)
unittests-3.13 91.87% <100.00%> (ø)
unittests-3.8 91.87% <100.00%> (ø)
unittests-3.9 91.87% <100.00%> (ø)

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.

@APN-Pucky
Copy link
Member

APN-Pucky commented Feb 4, 2025

Here's what we could do. But it seems a bit unnecessary unless we want to restrict the access to the predefined fields (breaking previous "free" behavior). And just defining the list of fields in a second place (tests) seems a bit wasted/stupid as a test. I must say I don't see the point in having the variable fieldnames, unless we loop over it, but maybe there are some builtin dict functions that use it.

class CustomDict(dict):
    fieldnames = ['name', 'age', 'email']

    def __setitem__(self, key, value):
        if key not in self.fieldnames:
            raise KeyError(f"Invalid key: {key}. Allowed keys are {self.fieldnames}")
        super().__setitem__(key, value)
import pytest
from your_module import CustomDict  # Import your class

def test_valid_keys():
    data = CustomDict()
    data['name'] = 'Alice'  # Should work
    data['age'] = 30        # Should work

def test_invalid_key():
    data = CustomDict()
    with pytest.raises(KeyError, match="Invalid key: address"):
        data['address'] = '123 Street'  # Should fail

@eduardo-rodrigues
Copy link
Member

Hello. I tend to agree that a test seems a bit of an overkill. On the other hand I'm sad the bug did not get caught.

@matthewfeickert
Copy link
Member Author

I must say I don't see the point in having the variable fieldnames, unless we loop over it, but maybe there are some builtin dict functions that use it.

I tend to agree that a test seems a bit of an overkill.

Yeah, I agree with you both that adding a test as is seems like a lot of superfluous work. I think we can skip that for this PR and maybe just call this good?

Though I agree with @APN-Pucky that I don't off the top of my head remember why LHEInit has fieldnames

fieldnames = ["initInfo", "procInfo" "weightgroup", "LHEVersion"]

as LHEInit doesn't have a fromstring like LHEInitInfo does

pylhe/src/pylhe/__init__.py

Lines 340 to 342 in 7519967

@classmethod
def fromstring(cls, string):
return cls(**dict(zip(cls.fieldnames, map(float, string.split()))))

and so fieldnames is never used in LHEInit (which is why we've never seen this as a bug).

Ah I guess looking a bit more the fieldnames were added in PR #233 when large components of what used to be LHEInit got moved into LHEInitInfo.

@APN-Pucky @eduardo-rodrigues If we don't explicitly use

fieldnames = ["initInfo", "procInfo" "weightgroup", "LHEVersion"]

then should we keep them at all? (I am going through this quickly while task switching, so I might be missing the obvious.)

@@ -248,7 +248,7 @@ def _indent(elem, level=0):
class LHEInit(dict):
"""Store the <init> block as dict."""

fieldnames = ["initInfo", "procInfo" "weightgroup", "LHEVersion"]
fieldnames = ["initInfo", "procInfo", "weightgroup", "LHEVersion"]
Copy link
Member Author

Choose a reason for hiding this comment

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

Suggested change
fieldnames = ["initInfo", "procInfo", "weightgroup", "LHEVersion"]

If we don't explicitly use

fieldnames = ["initInfo", "procInfo" "weightgroup", "LHEVersion"]

then should we keep them at all?

Given that this line was never getting used (else we would have seen this non-list list break) I think we can just remove this.

Copy link
Member

Choose a reason for hiding this comment

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

I had a look at the module. Most other classes have a fromstring method, which uses fieldnames. This one LHEInit doesn't. I'm no expert to understand why. Would it be useful to add such a method?

In any case, one thing the variable fieldnames is good for is documentation. Right now the docstring is minimalistic and maybe that's worth a revamp as well?

Copy link
Member

@APN-Pucky APN-Pucky Feb 6, 2025

Choose a reason for hiding this comment

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

Most other classes have a fromstring method, which uses fieldnames. This one LHEInit doesn't. I'm no expert to understand why. Would it be useful to add such a method?

I would be in favor of adding a fromstring there too. It sort of exists as part of read_lhe_init(file_or_string) function, but that can then be mapped to use the new fromstring.

The test on the fieldnames values would then just be if tolhe(fromstring(...)) and fromstring(tolhe(...)) are consistent.

Copy link
Member

Choose a reason for hiding this comment

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

I created a PR to add fromstring #283, but just like the read_lhe_init it does not use fieldnames and I don't see the point in forcing it to use it now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants