Fix version regex to prevent false matches on keys containing 'version'#60
Fix version regex to prevent false matches on keys containing 'version'#60ArieLevs wants to merge 1 commit into
Conversation
The regex pattern now requires 'version' to appear at the start of a line (with optional leading whitespace) to prevent false matches on keys like 'target-version' or 'this-is-a-version-containing-key'. Fixes #58 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #60 +/- ##
=======================================
Coverage 95.42% 95.42%
=======================================
Files 5 5
Lines 459 459
=======================================
Hits 438 438
Misses 21 21
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Code Review for PR #60SummaryThis PR fixes issue #58 by updating the version regex pattern to prevent false matches on keys containing the word "version" (like ✅ Strengths
🔍 Code Quality AnalysisThe Regex Change (src/pybump.py:13)-regex_version_pattern = re.compile(r"((?:__)?version(?:__)? ?= ?[\"'])(.+?)([\"'])")
+regex_version_pattern = re.compile(r"(^[ \t]*(?:__)?version(?:__)? ?= ?[\"'])(.+?)([\"'])", re.MULTILINE)Positives:
Potential Issues:
🧪 Test Coverage AssessmentUpdated Tests Look GoodThe test updates in Before: content_pre = 'some text before version="3.17.5", and some text after'After: content_pre = 'some text before\nversion="3.17.5"\nand some text after'This better represents actual file content where Missing Test CasesWhile the PR description mentions verification, I recommend adding explicit test cases for the reported issue: def test_version_regex_ignores_non_version_keys(self):
"""Test that keys containing 'version' but not being the version key are ignored"""
# Should NOT match target-version
content_with_target_version = '''
[project]
name = "test"
version = "1.0.0"
target-version = "py312"
'''
self.assertEqual(get_version_from_file(content_with_target_version), '1.0.0')
# Should NOT match custom-version-key
content_with_custom_key = '''
name = "test"
this-is-a-version-containing-key = "1.1.7"
version = "2.0.0"
'''
self.assertEqual(get_version_from_file(content_with_custom_key), '2.0.0')🐛 Potential Issues1. Inline Version Assignments Could Be ProblematicThe regex now requires # This would NOT match anymore:
if True: version = "1.0.0"
# But this WOULD match (with indentation):
version = "1.0.0"Question: Is this the intended behavior? Based on the typical file formats PyBump supports (setup.py, pyproject.toml, Chart.yaml), this seems correct, but it's worth documenting. 2. Comment Characters Before VersionWhat about this case? # version = "1.0.0" # This is commented out
version = "2.0.0" # This is the real versionThe regex would match both lines (since both start with optional whitespace + 3. No Test for the Specific Error MessageThe original issue showed: Consider adding a test that specifically verifies this error is no longer raised: def test_no_error_with_target_version_key(self):
"""Regression test for issue #58"""
content = '''
[tool.black]
target-version = "py312"
[project]
version = "2.0.0"
'''
# Should NOT raise "More than one 'version' found"
self.assertEqual(get_version_from_file(content), '2.0.0')⚡ Performance ConsiderationsThe change has negligible performance impact:
🔒 Security ConsiderationsNo security concerns identified. The regex change:
📋 RecommendationsHigh Priority
Medium Priority
Low Priority
regex_version_pattern = re.compile(r'''
^ # Start of line
[ \t]* # Optional whitespace (spaces or tabs)
(?:__)? # Optional double underscore prefix
version # The literal string 'version'
(?:__)? # Optional double underscore suffix
[ ]*=[ ]* # Equals sign with optional spaces
[\"'] # Opening quote
(.+?) # Version value (captured)
[\"'] # Closing quote
''', re.MULTILINE | re.VERBOSE)✅ ConclusionOverall Assessment: LGTM with minor suggestions This is a solid fix that correctly addresses the reported issue. The core change is minimal and appropriate. The main improvement would be adding explicit test cases for the reported scenarios to prevent regression. Suggested Actions:
Great work on identifying and fixing this issue! 🎉 |
Summary
versionto appear at the start of a line (with optional leading whitespace)target-versionorthis-is-a-version-containing-keyTest plan
target-version = "py312"this-is-a-version-containing-key = "1.1.7"versionkeyFixes #58
🤖 Generated with Claude Code