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

feat: trim flanking whitespace when reading a metric #217

Merged
merged 5 commits into from
Mar 8, 2025

Conversation

nh13
Copy link
Member

@nh13 nh13 commented Jan 28, 2025

Fixes #216

Also, strips any flanking whitespace when formatting a float into a string.

@nh13 nh13 requested review from tfenne and clintval as code owners January 28, 2025 17:09
Copy link
Contributor

coderabbitai bot commented Jan 28, 2025

Walkthrough

The pull request modifies the Metric class in the fgpyo/util/metric.py file, specifically updating the read and format_value methods. A new parameter, strip_whitespace, has been added to the read method, enabling the removal of leading and trailing whitespace from values read from the metric file. The format_value method's return statements have been revised to format values using f-strings. Additionally, two new test functions have been introduced in tests/fgpyo/util/test_metric.py to verify the handling of float values and whitespace in the Person class and during TSV file reading. No existing functionality was deleted or modified.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 47903c3 and 4a4a5bc.

📒 Files selected for processing (1)
  • tests/fgpyo/util/test_metric.py (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/fgpyo/util/test_metric.py

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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

📥 Commits

Reviewing files that changed from the base of the PR and between f1f89e9 and 6315fbd.

📒 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.

Comment on lines 406 to 436
@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]

Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

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.

Suggested change
@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.

Copy link

codecov bot commented Jan 28, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.81%. Comparing base (11f46ed) to head (4a4a5bc).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #217      +/-   ##
==========================================
+ Coverage   90.72%   90.81%   +0.09%     
==========================================
  Files          18       18              
  Lines        2318     2320       +2     
  Branches      343      344       +1     
==========================================
+ Hits         2103     2107       +4     
+ Misses        145      144       -1     
+ Partials       70       69       -1     

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

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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

📥 Commits

Reviewing files that changed from the base of the PR and between d1d08de and 3d73db1.

📒 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.

@nh13 nh13 force-pushed the feat/strip-metric-values branch from 3d73db1 to de3b0c7 Compare January 28, 2025 19:00
@nh13 nh13 changed the title feat: metric to trim flanking whitespace at: trim flanking whitespace when reading a metric Jan 28, 2025
@nh13 nh13 requested a review from msto January 28, 2025 19:00
Copy link
Contributor

@coderabbitai coderabbitai bot left a 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

📥 Commits

Reviewing files that changed from the base of the PR and between 3d73db1 and de3b0c7.

📒 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.

@nh13 nh13 changed the title at: trim flanking whitespace when reading a metric feat: trim flanking whitespace when reading a metric Jan 28, 2025
Copy link
Contributor

@msto msto left a comment

Choose a reason for hiding this comment

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

Thank you!

assert persons[2].name == "Someone Else"
assert persons[2].age == 47

persons = list(data_and_classes.Person.read(test_tsv, strip_whitespace=False))
Copy link
Contributor

@msto msto Jan 28, 2025

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!

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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)

226-226: Line exceeds maximum length of 100 characters.

Current line is 113 characters long according to static analysis.

-            strip_whitespace: True to strip leading and trailing whitespace from each field, False to keep as-is.
+            strip_whitespace: True to strip leading/trailing whitespace from each field, False to keep as-is.
🧰 Tools
🪛 Ruff (0.8.2)

226-226: Line too long (113 > 100)

(E501)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between de3b0c7 and 20f469d.

📒 Files selected for processing (2)
  • fgpyo/util/metric.py (4 hunks)
  • tests/fgpyo/util/test_metric.py (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/fgpyo/util/test_metric.py
🧰 Additional context used
🪛 Ruff (0.8.2)
fgpyo/util/metric.py

226-226: Line too long (113 > 100)

(E501)

🔇 Additional comments (4)
fgpyo/util/metric.py (4)

214-215: Good choice using False as default for strip_whitespace.

Setting the default to False maintains backward compatibility with existing code, preventing potential breaking changes.


269-270: Implementation correctly handles whitespace stripping.

The code properly applies stripping only when requested and iterates through all values in the list.


361-361: The f-string formatting for float values looks good.

Rounding to 5 decimal places is maintained while using modern f-string syntax.


365-365: Consistent use of f-strings for value formatting.

Updated to use modern f-string notation for general values as well.

nh13 and others added 2 commits March 7, 2025 17:30
Fixes #216

Also, strips any flanking whitespace when formatting a float into
a string.
@nh13 nh13 force-pushed the feat/strip-metric-values branch from 20f469d to faac2ac Compare March 8, 2025 00:30
Co-authored-by: Matt Stone <matt@fulcrumgenomics.com>
@nh13 nh13 force-pushed the feat/strip-metric-values branch from faac2ac to a1de7d9 Compare March 8, 2025 00:31
Copy link
Contributor

@coderabbitai coderabbitai bot left a 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)

226-226: Line exceeds length limit.

Line is 113 characters, exceeding 100 character limit.

-            strip_whitespace: True to strip leading and trailing whitespace from each field, False to keep as-is.
+            strip_whitespace: True to strip leading/trailing whitespace from each field,
+                False to keep as-is.
🧰 Tools
🪛 Ruff (0.8.2)

226-226: Line too long (113 > 100)

(E501)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 20f469d and faac2ac.

📒 Files selected for processing (2)
  • fgpyo/util/metric.py (4 hunks)
  • tests/fgpyo/util/test_metric.py (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/fgpyo/util/test_metric.py
🧰 Additional context used
🪛 Ruff (0.8.2)
fgpyo/util/metric.py

226-226: Line too long (113 > 100)

(E501)

🔇 Additional comments (4)
fgpyo/util/metric.py (4)

214-215: Appropriate default for compatibility.

Good choice setting strip_whitespace to False by default. This maintains backward compatibility and avoids breaking existing code.


269-270: Clean implementation of whitespace stripping.

The code correctly applies stripping only when requested, with a clear implementation.


361-361: Cleaner float formatting with f-string.

The f-string approach is more readable than string concatenation.


365-365: Consistent string formatting.

Good use of f-string for general values, matching the style used for float values.

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between faac2ac and a1de7d9.

📒 Files selected for processing (1)
  • fgpyo/util/metric.py (4 hunks)
🧰 Additional context used
🪛 Ruff (0.8.2)
fgpyo/util/metric.py

226-226: Trailing whitespace

Remove trailing whitespace

(W291)

🪛 GitHub Actions: test
fgpyo/util/metric.py

[error] 1-1: Ruff formatting check failed. 1 file would be reformatted. Please run 'ruff format' to fix code style issues in this file.

🔇 Additional comments (4)
fgpyo/util/metric.py (4)

213-215: Parameter added correctly with safe default.

The new strip_whitespace parameter with a default of False maintains backward compatibility, good approach.


270-271: Effective implementation of whitespace stripping.

The conditional check and list comprehension is an efficient approach to handle the whitespace stripping.


362-362: Good use of f-string for float formatting.

The f-string approach for formatting floats is clean and efficient.


366-366: Good use of f-string for general value formatting.

Converting to f-string for general values is consistent with the float formatting approach.

@nh13 nh13 merged commit 969d406 into main Mar 8, 2025
14 checks passed
@nh13 nh13 deleted the feat/strip-metric-values branch March 8, 2025 00:43
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.

Metric could optionally trim flanking whitespace from string fields
2 participants