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

DM-48074: Introduce keyCheck callback function for DictField and ConfigDictField #113

Merged
merged 11 commits into from
Dec 24, 2024

Conversation

enourbakhsh
Copy link
Contributor

@enourbakhsh enourbakhsh commented Dec 11, 2024

Checklist

  • ran Jenkins
  • added a release note for user-visible changes to doc/changes

Copy link

codecov bot commented Dec 11, 2024

Codecov Report

Attention: Patch coverage is 89.83051% with 6 lines in your changes missing coverage. Please review.

Project coverage is 86.00%. Comparing base (89c8672) to head (a3a6a69).
Report is 12 commits behind head on main.

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
python/lsst/pex/config/listField.py 57.14% 3 Missing ⚠️
python/lsst/pex/config/history.py 66.66% 1 Missing ⚠️
tests/test_configDictField.py 92.30% 1 Missing ⚠️
tests/test_dictField.py 93.75% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #113      +/-   ##
==========================================
+ Coverage   85.87%   86.00%   +0.13%     
==========================================
  Files          46       46              
  Lines        3604     3645      +41     
==========================================
+ Hits         3095     3135      +40     
- Misses        509      510       +1     

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

Copy link

@taranu taranu left a comment

Choose a reason for hiding this comment

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

Looks good. I have a few comments/questions, as usual.

# validate key using keycheck
if self._field.keyCheck is not None and not self._field.keyCheck(k):
msg = f"Key {k!r} is not a valid key"
raise FieldValidationError(self._field, self._config, msg)
Copy link

Choose a reason for hiding this comment

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

I kind of wish this method would collect all errors before throwing in case there are multiple, but I suppose that's not how it was designed. Oh well.

@@ -191,12 +200,15 @@ def __init__(
raise ValueError(f"'itemtype' {_typeStr(itemtype)} is not a supported type")
if dictCheck is not None and not hasattr(dictCheck, "__call__"):
Copy link

Choose a reason for hiding this comment

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

These errors could actually be consolidated into one, e.g.:

check_errors = []
for name, check in (("dictCheck", dictCheck), ("keyCheck", keyCheck), ("itemCheck", itemCheck)):
    if check is not None and not hasattr(check, "__call__"):
        check_errors.append(name)
if check_errors:
    raise ValueError(f"{', '.join(check_errors)} must be callable")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea, I just wanted to be minimally invasive in my approach but this sounds like an improvement.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

@@ -312,12 +320,15 @@ def __init__(
raise ValueError(f"'itemtype' {_typeStr(itemtype)} is not a supported type")
if dictCheck is not None and not hasattr(dictCheck, "__call__"):
Copy link

Choose a reason for hiding this comment

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

Same comment about consolidating errors here. Also it seems like a lot of code could have been shared between configDictField and regular dictField, but that's another issue for another day.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

for i, vt in enumerate(output):
if writeSourceLine:
vt[0][0] = "%-*s" % (sourceLength, vt[0][0])
vt[0][0] = f"{vt[0][0]:<{sourceLength}}"
Copy link

Choose a reason for hiding this comment

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

I assume you've checked that these formats and the ones above are identical (it is not totally obvious to me).


output[i] = " ".join([_colorize(v, t) for v, t in vt])

line += ("\n%*s" % (valueLength + 1, "")).join(output)
line += ("\n" + f"{'':>{valueLength + 1}}").join(output)
Copy link

Choose a reason for hiding this comment

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

Any reason why the \n can't go inside the f-string?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice catch! Moved it inside the f-string.

class BadKeyCheck(pexConfig.Config):
d = pexConfig.DictField("...", keytype=int, itemtype=int, keyCheck=4)

except Exception:
Copy link

Choose a reason for hiding this comment

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

Not ValueError, or a with self.assertRaises(ValueError, ... like below?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree, and that does seem more suitable to me as well. However, there are four more try...except blocks in this test case, both above and below, and I didn’t want to break consistency. I think we should modernize all these old tests in a new ticket.

@enourbakhsh enourbakhsh merged commit c5452a3 into main Dec 24, 2024
14 checks passed
@enourbakhsh enourbakhsh deleted the tickets/DM-48074 branch December 24, 2024 16:24
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.

2 participants