Rr 87 version error msg#93
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates cfa.dataops.utils.version_matcher to use packaging’s Version and SpecifierSet for selecting versions (including timestamp-like versions), and adds a dedicated test module covering expected matching and error behavior.
Changes:
- Added unit tests for
version_matcher, including date-range matching and expected failure cases. - Refactored
version_matcherto normalize timestamp strings and usepackagingfor ordering and specifier matching.
Reviewed changes
Copilot reviewed 2 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
tests/test_utils_version_matcher.py |
Adds tests for “latest”, range selection, and expected error cases for invalid/out-of-range inputs. |
cfa/dataops/utils.py |
Reimplements version_matcher using packaging.Version/SpecifierSet and timestamp normalization. |
Comments suppressed due to low confidence (2)
cfa/dataops/utils.py:219
SpecifierSet(spec_normalized)will raise for a bare version like"2025-12-18T00-00-00"(no comparison operator). The previous implementation treated bare versions as equality, and your new tests also pass bare versions. Consider normalizing bare specs to an explicit equality specifier (e.g., prefix with==) before constructingSpecifierSet.
spec_normalized = spec.replace("T", ".").replace("-", ".")
available_versions_normalized = [
v.replace("T", ".").replace("-", ".") for v in available_versions
]
specset = SpecifierSet(spec_normalized)
cfa/dataops/utils.py:228
- Mapping matches back to originals via
available_versions_normalized.index(m)is O(n^2) and will return the first occurrence when there are duplicate normalized versions, producing incorrect results. Prefer keeping a parallel list of tuples (normalized, original) and filtering/sorting tuples so you never needindex()lookups.
# Map back to original versions
original_matches = [
available_versions[available_versions_normalized.index(m)] for m in matches
]
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 3 changed files in this pull request and generated 2 comments.
Comments suppressed due to low confidence (3)
cfa/dataops/utils.py:219
SpecifierSet(spec_normalized)will raisepackaging.specifiers.InvalidSpecifierfor inputs like a bare timestamp (e.g.2025-12-18T00-00-00) because it lacks an operator. Since callers/tests pass bare timestamps, this makesversion_matcherfail; consider treating bare specs as equality (prefix==) and catchingInvalidSpecifierto re-raiseValueErrorwith the intended message.
spec_normalized = spec.replace("T", ".").replace("-", ".")
available_versions_normalized = [
v.replace("T", ".").replace("-", ".") for v in available_versions
]
specset = SpecifierSet(spec_normalized)
cfa/dataops/utils.py:228
- Mapping normalized matches back to originals via
available_versions_normalized.index(m)is O(n^2) and returns the first occurrence only, which is incorrect ifavailable_versionscontains duplicates. Prefer building a mapping from normalized->original (or filtering a list of(normalized, original)pairs) so the correspondence is stable and linear-time.
# Map back to original versions
original_matches = [
available_versions[available_versions_normalized.index(m)] for m in matches
]
cfa/dataops/utils.py:199
- This change removes the
version_matcherdocstring and return type annotation, even though it has non-trivial behavior (str | list[str] | None). Restoring documentation and an explicit return type would help callers understand what to expect (especially the newNonereturns when no matches exist).
def version_matcher(
spec: str,
available_versions: list[str],
newest: bool | None = True,
):
if spec == "latest":
if not available_versions:
return None
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Agent-Logs-Url: https://github.com/CDCgov/cfa-dataops/sessions/0d556238-b895-4618-8034-cbb1dbd85cc6 Co-authored-by: ryanraaschCDC <150935395+ryanraaschCDC@users.noreply.github.com>
…fa-dataops into rr-87-version-error-msg
damonbayer
left a comment
There was a problem hiding this comment.
a few more observations
damonbayer
left a comment
There was a problem hiding this comment.
Tests could use a once-over. There is some redundancy and others that don't test what they say they do.
damonbayer
left a comment
There was a problem hiding this comment.
Thanks @ryanraaschCDC!
No description provided.