-
Notifications
You must be signed in to change notification settings - Fork 3
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
feat: trim flanking whitespace when reading a metric #217
base: main
Are you sure you want to change the base?
Conversation
Warning Rate limit exceeded@nh13 has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 23 minutes and 13 seconds before requesting another review. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. 📒 Files selected for processing (1)
WalkthroughThe pull request modifies the Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 1
🧹 Nitpick comments (1)
fgpyo/util/metric.py (1)
356-356
: Add docstring to document whitespace stripping behavior.The new whitespace stripping behavior should be documented in the method's docstring.
Add this to the docstring:
Args: value: the value to format. + + Note: + Leading and trailing whitespace is stripped from all string representations.Also applies to: 360-360
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
fgpyo/util/metric.py
(1 hunks)tests/fgpyo/util/test_metric.py
(1 hunks)
🧰 Additional context used
🪛 GitHub Actions: Python package
tests/fgpyo/util/test_metric.py
[error] 408-408: Test failure in test_metric_whitespace_padded_strip: String comparison mismatch. Expected 'John Doe' but got ' John Doe \t ' with extra whitespace and tab characters.
tests/fgpyo/util/test_metric.py
Outdated
@pytest.mark.parametrize("data_and_classes", (attr_data_and_classes, dataclasses_data_and_classes)) | ||
def test_metric_whitespace_padded_strip(data_and_classes: DataBuilder) -> None: | ||
assert list(data_and_classes.Person(name=" John Doe \t ", age=42).values()) == ["John Doe", 42] | ||
|
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.
Fix test failure: values not being stripped.
Test expects whitespace stripping but fails. The values()
method returns raw values, while formatted_values()
returns formatted (stripped) values.
Fix the test by using formatted_values()
:
- assert list(data_and_classes.Person(name=" John Doe \t ", age=42).values()) == ["John Doe", 42]
+ assert data_and_classes.Person(name=" John Doe \t ", age=42).formatted_values() == ["John Doe", "42"]
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
@pytest.mark.parametrize("data_and_classes", (attr_data_and_classes, dataclasses_data_and_classes)) | |
def test_metric_whitespace_padded_strip(data_and_classes: DataBuilder) -> None: | |
assert list(data_and_classes.Person(name=" John Doe \t ", age=42).values()) == ["John Doe", 42] | |
@pytest.mark.parametrize("data_and_classes", (attr_data_and_classes, dataclasses_data_and_classes)) | |
def test_metric_whitespace_padded_strip(data_and_classes: DataBuilder) -> None: | |
assert data_and_classes.Person(name=" John Doe \t ", age=42).formatted_values() == ["John Doe", "42"] | |
🧰 Tools
🪛 GitHub Actions: Python package
[error] 408-408: Test failure in test_metric_whitespace_padded_strip: String comparison mismatch. Expected 'John Doe' but got ' John Doe \t ' with extra whitespace and tab characters.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #217 +/- ##
==========================================
+ Coverage 91.00% 91.08% +0.08%
==========================================
Files 18 18
Lines 2300 2300
Branches 341 341
==========================================
+ Hits 2093 2095 +2
+ Misses 136 135 -1
+ Partials 71 70 -1 ☔ View full report in Codecov by Sentry. |
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.
Actionable comments posted: 0
🧹 Nitpick comments (1)
fgpyo/util/metric.py (1)
213-213
: Fix line length.Line exceeds maximum length of 100 characters.
- def read(cls, path: Path, ignore_extra_fields: bool = True, strip_whitespace: bool = True) -> Iterator[Any]: + def read( + cls, + path: Path, + ignore_extra_fields: bool = True, + strip_whitespace: bool = True, + ) -> Iterator[Any]:🧰 Tools
🪛 Ruff (0.8.2)
213-213: Line too long (112 > 100)
(E501)
🪛 GitHub Actions: Python package
[warning] File needs formatting. Run 'ruff format' to fix formatting issues.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
fgpyo/util/metric.py
(4 hunks)tests/fgpyo/util/test_metric.py
(2 hunks)
🧰 Additional context used
🪛 GitHub Actions: Python package
tests/fgpyo/util/test_metric.py
[warning] File needs formatting. Run 'ruff format' to fix formatting issues.
fgpyo/util/metric.py
[warning] File needs formatting. Run 'ruff format' to fix formatting issues.
🪛 Ruff (0.8.2)
fgpyo/util/metric.py
213-213: Line too long (112 > 100)
(E501)
🔇 Additional comments (5)
fgpyo/util/metric.py (2)
267-268
: LGTM!Clean implementation of whitespace stripping with proper conditional check.
🧰 Tools
🪛 GitHub Actions: Python package
[warning] File needs formatting. Run 'ruff format' to fix formatting issues.
359-363
: LGTM!Clean implementation of whitespace handling in value formatting.
🧰 Tools
🪛 GitHub Actions: Python package
[warning] File needs formatting. Run 'ruff format' to fix formatting issues.
tests/fgpyo/util/test_metric.py (3)
157-161
: LGTM!Clean implementation of float age support.
🧰 Tools
🪛 GitHub Actions: Python package
[warning] File needs formatting. Run 'ruff format' to fix formatting issues.
411-419
: LGTM!Test properly verifies float rounding behavior.
🧰 Tools
🪛 GitHub Actions: Python package
[warning] File needs formatting. Run 'ruff format' to fix formatting issues.
421-438
: LGTM!Comprehensive test coverage for whitespace stripping functionality.
🧰 Tools
🪛 GitHub Actions: Python package
[warning] File needs formatting. Run 'ruff format' to fix formatting issues.
Fixes #216 Also, strips any flanking whitespace when formatting a float into a string.
3d73db1
to
de3b0c7
Compare
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.
Actionable comments posted: 0
🧹 Nitpick comments (1)
tests/fgpyo/util/test_metric.py (1)
411-417
: Add edge cases to float rounding test.Consider adding test cases for:
- Very small numbers (e.g., 0.0000001)
- Very large numbers (e.g., 1e10)
- Negative numbers
@pytest.mark.parametrize("data_and_classes", (attr_data_and_classes, dataclasses_data_and_classes)) def test_metric_round_floats(data_and_classes: DataBuilder) -> None: - assert list(data_and_classes.Person(name="John Doe", age=42.123456).formatted_values()) == [ - "John Doe", - "42.12346", - ] + test_cases = [ + (42.123456, "42.12346"), + (0.0000001, "0.0"), + (1e10, "10000000000.0"), + (-42.123456, "-42.12346"), + ] + for value, expected in test_cases: + assert list(data_and_classes.Person(name="John Doe", age=value).formatted_values()) == [ + "John Doe", + expected, + ]
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
fgpyo/util/metric.py
(4 hunks)tests/fgpyo/util/test_metric.py
(2 hunks)
🔇 Additional comments (5)
fgpyo/util/metric.py (3)
213-215
: LGTM! Well-documented parameter addition.The new
strip_whitespace
parameter is properly documented and maintains backward compatibility.Also applies to: 226-226
269-270
: LGTM! Clean implementation of whitespace stripping.The implementation correctly handles the
strip_whitespace
parameter.
361-361
: LGTM! Consistent whitespace handling.The changes ensure uniform whitespace handling for both float and general values.
Also applies to: 365-365
tests/fgpyo/util/test_metric.py (2)
157-161
: LGTM! Well-structured test class.The
PersonAgeFloat
class is properly implemented for testing float values.
419-436
: LGTM! Comprehensive whitespace handling test.Test covers all important whitespace scenarios: leading, trailing, and both.
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.
Thank you!
@@ -353,11 +358,11 @@ def format_value(cls, value: Any) -> str: # noqa: C901 | |||
+ "}" | |||
) | |||
elif isinstance(value, float): | |||
return str(round(value, 5)) | |||
return f"{round(value, 5)}".strip() |
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 think the .strip()
is unnecessary here because the f-string interpolation won't add any trailing whitespace
return f"{round(value, 5)}".strip() | |
return f"{round(value, 5)}" |
@@ -221,6 +223,7 @@ def read(cls, path: Path, ignore_extra_fields: bool = True) -> Iterator[Any]: | |||
Args: | |||
path: the path to the metrics file. | |||
ignore_extra_fields: True to ignore any extra columns, False to raise an exception. | |||
strip_whitespace: True to strip leading and trailing whitespace, False to keep as-is. |
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.
Just a small suggestion! It might be helpful to clarify that the whitespace stripping applies to each field individually, not to the row as a whole
strip_whitespace: True to strip leading and trailing whitespace, False to keep as-is. | |
strip_whitespace: True to strip leading and trailing whitespace from each field, False to keep as-is. |
@@ -210,7 +210,9 @@ def _parsers(cls) -> Dict[type, Callable[[str], Any]]: | |||
return {} | |||
|
|||
@classmethod | |||
def read(cls, path: Path, ignore_extra_fields: bool = True) -> Iterator[Any]: | |||
def read( | |||
cls, path: Path, ignore_extra_fields: bool = True, strip_whitespace: bool = True |
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.
Should this default be False
? Otherwise it will be a breaking change
cls, path: Path, ignore_extra_fields: bool = True, strip_whitespace: bool = True | |
cls, path: Path, ignore_extra_fields: bool = True, strip_whitespace: bool = False |
assert persons[2].name == "Someone Else" | ||
assert persons[2].age == 47 | ||
|
||
persons = list(data_and_classes.Person.read(test_tsv, strip_whitespace=False)) |
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.
🙌 good catch to cover both cases!
Fixes #216
Also, strips any flanking whitespace when formatting a float into a string.