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

preliminary str parse and format methods #90

Merged
merged 5 commits into from
Nov 8, 2024
Merged

Conversation

rlskoeser
Copy link
Member

@rlskoeser rlskoeser commented Aug 29, 2024

add undate parse and format methods (~ analogous to datetime.strptime and strftime) to allow parsing and formatting dates as strings using any defined formatter class (to be expanded later for more formatting options)

this resolves #87 and is something we can built out for more flexible formatting options later

Summary by CodeRabbit

  • New Features

    • Enhanced date formatting functionality to support both specific dates and date intervals.
    • Introduced methods for parsing and formatting date strings in Undate and UndateInterval classes.
    • Expanded input types for existing methods to accommodate a wider range of date representations.
  • Bug Fixes

    • Improved error handling for unsupported date formats in parsing and formatting methods.
  • Tests

    • Added new test cases for Undate and UndateInterval classes to validate parsing and formatting functionality across various date formats.

@rlskoeser rlskoeser changed the title str parse and format methods preliminary str parse and format methods Aug 30, 2024
@rlskoeser
Copy link
Member Author

@coderabbitai full review

Copy link
Contributor

coderabbitai bot commented Aug 30, 2024

Actions performed

Full review triggered.

Copy link
Contributor

coderabbitai bot commented Aug 30, 2024

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The changes introduced enhance the functionality of the date handling system by expanding type support and improving the parsing and formatting capabilities of the Undate and UndateInterval classes. New methods for parsing and formatting date strings have been added, allowing for greater flexibility in handling different date formats. Additionally, tests have been implemented to ensure the correctness of these new features.

Changes

Files Change Summary
src/undate/dateformat/base.py Updated import statements to include Union for enhanced type hinting.
src/undate/dateformat/edtf/formatter.py, src/undate/dateformat/iso8601.py Modified to_string methods to accept both Undate and UndateInterval, adding a new helper method for string conversion.
src/undate/undate.py Introduced parse and format methods for Undate and UndateInterval classes to handle date string conversion.
tests/test_undate.py Added new test cases for parsing and formatting functionalities in Undate and UndateInterval.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Undate
    participant Formatter

    User->>Undate: parse(date_string, format)
    Undate->>Formatter: get appropriate formatter
    Formatter-->>Undate: formatter found
    Undate->>Formatter: call parse method
    Formatter-->>Undate: return Undate/UndateInterval object
    Undate-->>User: return parsed object
Loading
sequenceDiagram
    participant User
    participant Undate
    participant Formatter

    User->>Undate: format(format)
    Undate->>Formatter: get appropriate formatter
    Formatter-->>Undate: formatter found
    Undate->>Formatter: call to_string method
    Formatter-->>Undate: return formatted string
    Undate-->>User: return formatted string
Loading

Assessment against linked issues

Objective Addressed Explanation
support using different formatters for input and output (#[87])

🐇 Hopping through the fields so bright,
With new formats to parse, oh what a delight!
From Undate to intervals, we dance with glee,
Formatting dates as easy as can be!
So let’s celebrate this change with cheer,
For a world of dates is finally here! 🌼✨


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?

❤️ Share
🪧 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 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

@kypso-io kypso-io bot left a comment

Choose a reason for hiding this comment

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

📋 Review Summary

  • Number of files reviewed: 5
  • Number of comments: 0
  • Number of suggestions: 0

📚 File changes

File path File changes
src/undate/dateformat/base.py Added import for Union to support type hinting.
src/undate/dateformat/edtf/formatter.py Updated the to_string method to handle both Undate and UndateInterval types.
src/undate/dateformat/iso8601.py Updated the to_string method to accept both Undate and UndateInterval types.
src/undate/undate.py Added parse and format methods to Undate and UndateInterval classes for handling date string parsing and formatting.
tests/test_undate.py Added tests for parsing and formatting methods for Undate and UndateInterval.

@rlskoeser
Copy link
Member Author

@coderabbitai full review

Copy link
Contributor

coderabbitai bot commented Aug 30, 2024

Actions performed

Full review triggered.

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: 2

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between c65bc3e and a8802d6.

Files selected for processing (5)
  • src/undate/dateformat/base.py (1 hunks)
  • src/undate/dateformat/edtf/formatter.py (1 hunks)
  • src/undate/dateformat/iso8601.py (1 hunks)
  • src/undate/undate.py (2 hunks)
  • tests/test_undate.py (2 hunks)
Additional context used
Ruff
src/undate/dateformat/base.py

18-18: typing.Union imported but unused

Remove unused import: typing.Union

(F401)

Additional comments not posted (10)
src/undate/dateformat/base.py (1)

19-19: Acknowledgment of Python version update.

The removal of the comment regarding functools.cache suggests that the codebase now targets Python 3.9 or higher, which includes this functionality by default. This is a positive change that simplifies the codebase and leverages newer Python features.

src/undate/dateformat/edtf/formatter.py (2)

38-47: Enhanced flexibility in to_string method.

The updated to_string method now handles both Undate and UndateInterval types, aligning with the PR's objectives to support flexible date formatting. This change enhances the method's versatility and allows for more comprehensive date handling.


48-48: Introduction of _undate_to_string method.

The new _undate_to_string method encapsulates the logic for converting an Undate object to a string. This separation of concerns improves code readability and maintainability by delegating the string conversion task to a specialized method.

src/undate/dateformat/iso8601.py (2)

52-61: Enhanced flexibility in to_string method.

The updated to_string method now handles both Undate and UndateInterval types, aligning with the PR's objectives to support flexible date formatting. This change enhances the method's versatility and allows for more comprehensive date handling.


63-63: Introduction of _undate_to_string method.

The new _undate_to_string method encapsulates the logic for converting an Undate object to a string. This separation of concerns improves code readability and maintainability by delegating the string conversion task to a specialized method.

src/undate/undate.py (2)

177-185: Review the implementation of the format method in the Undate class.

The format method is well-implemented with checks for the existence of a formatter class and appropriate error handling for unsupported formats. This method aligns with the PR objectives by allowing flexible formatting of Undate objects. However, the comment on line 182 about formatters returning intervals should be addressed to ensure clarity and consistency in the method's behavior.


448-455: Review the implementation of the format method in the UndateInterval class.

This method mirrors the implementation in the Undate class, providing consistent functionality across both classes. The method properly checks for the existence of a formatter and raises a ValueError if the format is unsupported, which is good practice. Ensure that all formatters used here are capable of handling UndateInterval objects specifically, as the method assumes compatibility.

tests/test_undate.py (3)

382-399: Review the test_parse method for the Undate class.

The test_parse method adequately tests the parsing functionality with both supported and unsupported formats. It verifies that the correct objects are returned for valid inputs and that appropriate exceptions are raised for unsupported formats. This method effectively ensures that the parsing functionality behaves as expected under various conditions.


400-421: Review the test_format method for the Undate class.

This method tests the formatting functionality thoroughly by checking the output against expected results for both supported formats (EDTF and ISO8601) and handling unsupported formats with exceptions. The inclusion of edge cases like "19XX" and "XXXX-12-31" enhances the robustness of the tests. This method provides good coverage and validation for the formatting functionality.


435-448: Review the test_format method for the UndateInterval class.

The tests for the UndateInterval class's format method are comprehensive, covering both typical and open-ended interval cases. The tests ensure that the method correctly formats intervals according to both EDTF and ISO8601 standards and handles unsupported formats appropriately. This thorough testing supports the functionality's robustness and alignment with the PR objectives.

@@ -15,8 +15,8 @@
import importlib
import logging
import pkgutil
from typing import Dict, Type
from functools import lru_cache # functools.cache not available until 3.9
from typing import Dict, Type, Union
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove unused import: Union.

The import of Union from the typing module is flagged as unused by the static analysis tool. If it's not needed, consider removing it to keep the code clean and efficient.

-from typing import Dict, Type, Union
+from typing import Dict, Type
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
from typing import Dict, Type, Union
from typing import Dict, Type
Tools
Ruff

18-18: typing.Union imported but unused

Remove unused import: typing.Union

(F401)

Comment on lines +166 to +175
@classmethod
def parse(cls, date_string, format) -> Union["Undate", "UndateInterval"]:
"""parse a string to an undate or undate interval using the specified format;
for now, only supports named formatters"""
formatter_cls = BaseDateFormat.available_formatters().get(format, None)
if formatter_cls:
# NOTE: some parsers may return intervals; is that ok here?
return formatter_cls().parse(date_string)

raise ValueError(f"Unsupported format '{format}'")
Copy link
Contributor

Choose a reason for hiding this comment

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

Review the implementation of the parse method in the Undate class.

The parse method correctly retrieves a formatter class based on the provided format and uses it to parse the date string. However, the method assumes that all formatters will correctly handle the input without further validation or error handling for malformed inputs. Consider adding input validation or more detailed error handling to ensure robustness.

Additionally, the comment on line 172 notes that some parsers may return intervals. It's important to clarify whether this behavior is acceptable or if additional handling is needed when an interval is not expected.

Copy link

codecov bot commented Nov 7, 2024

Codecov Report

Attention: Patch coverage is 96.87500% with 1 line in your changes missing coverage. Please review.

Please upload report for BASE (feature/edtf-formatter@5668c52). Learn more about missing BASE report.

Files with missing lines Patch % Lines
src/undate/undate.py 93.75% 1 Missing ⚠️
Additional details and impacted files
@@                    Coverage Diff                    @@
##             feature/edtf-formatter      #90   +/-   ##
=========================================================
  Coverage                          ?   96.94%           
=========================================================
  Files                             ?       10           
  Lines                             ?      458           
  Branches                          ?        0           
=========================================================
  Hits                              ?      444           
  Misses                            ?       14           
  Partials                          ?        0           

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

Copy link

@kypso-io kypso-io bot left a comment

Choose a reason for hiding this comment

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

📋 Review Summary

  • Number of files reviewed: 5
  • Number of comments: 4
  • Number of suggestions: 1

📚 File changes

File path File changes
src/undate/dateformat/edtf/formatter.py Updated the to_string method to accept both Undate and UndateInterval types.
src/undate/dateformat/iso8601.py Updated the to_string method to accept both Undate and UndateInterval types.
src/undate/undate.py Added parse and format methods to Undate and UndateInterval classes for enhanced date string handling.
tests/test_dateformat/test_edtf.py Removed an assertion in the test_parse_invalid method to correctly test for a ValueError when parsing an invalid date format.
tests/test_undate.py Added tests for parsing and formatting methods in Undate and UndateInterval classes, including handling of unsupported formats.
Ignored comments

src/undate/dateformat/edtf/formatter.py

  • refactor_suggestion: The to_string method now handles both Undate and UndateInterval, which is a good enhancement. However, consider consolidating the logic for handling Undate and UndateInterval into a single method to reduce code duplication and improve maintainability. This could involve creating a helper method that formats the date string based on the type of undate.

src/undate/undate.py

  • refactor_suggestion: The format method in both Undate and UndateInterval classes has similar logic for handling unsupported formats. Consider extracting this logic into a separate private method to reduce code duplication and improve maintainability.

  • refactor_suggestion: The parse method in the Undate class has a similar structure to the format method. Consider extracting the common logic for retrieving the formatter class into a private method to avoid code duplication and enhance clarity.

tests/test_undate.py

  • refactor_suggestion: The import statement for DatePrecision is added, but it is not used in the test cases. If it is not needed, consider removing it to keep the code clean.

  • refactor_suggestion: The comment regarding the unset year could be more informative. Consider specifying the expected behavior when the year is unset, such as whether it should return a specific value or raise an exception.

  • refactor_suggestion: The comment about forcing the method to hit the conditional for date precision could be expanded to clarify why this is necessary and what the implications are for the test.

  • refactor_suggestion: The comment about forcing the string representation based on date precision without the day part set could be clearer. It might be helpful to explain the significance of this behavior in the context of the test.

  • refactor_suggestion: The comment about the comparison returning NotImplemented could be more descriptive. It may be beneficial to explain under what circumstances this occurs and why it is important to handle this case.

  • refactor_suggestion: The test cases for parsing and formatting could benefit from additional comments explaining the expected outcomes for each case, especially for unsupported formats.

  • refactor_suggestion: The comment about the duration not being supported for open-ended intervals could be expanded to clarify why this is the case and what the expected behavior should be.

src/undate/undate.py Show resolved Hide resolved
src/undate/undate.py Show resolved Hide resolved
src/undate/undate.py Show resolved Hide resolved
tests/test_dateformat/test_edtf.py Show resolved Hide resolved
Base automatically changed from feature/edtf-formatter to develop November 8, 2024 20:50
@rlskoeser rlskoeser merged commit 759ec58 into develop Nov 8, 2024
11 checks passed
@rlskoeser rlskoeser deleted the feature/parse-format branch November 8, 2024 20:51
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.

1 participant