-
Notifications
You must be signed in to change notification settings - Fork 22
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
base: main
Are you sure you want to change the base?
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
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 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 |
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. |
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 Line 251 in 7519967
as Lines 340 to 342 in 7519967
and so Ah I guess looking a bit more the @APN-Pucky @eduardo-rodrigues If we don't explicitly use Line 251 in 7519967
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"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fieldnames = ["initInfo", "procInfo", "weightgroup", "LHEVersion"] |
If we don't explicitly use
Line 251 in 7519967
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Fixes the bug in Issue #281
Not sure we have a good test at the moment though.