Skip to content

feat: expose str_to_datetime jinja macro #351

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

Merged
merged 9 commits into from
Feb 20, 2025
Merged

Conversation

pnilan
Copy link
Contributor

@pnilan pnilan commented Feb 20, 2025

What

  • source-tiktok-marketing needs access to the str_to_datetime jinja macro as related to this issue. This PR exposes this macro for public use and adds appropriate tests.

Summary by CodeRabbit

  • New Features
    • Introduced a public utility function for converting strings to datetime objects with UTC timezone.
  • Tests
    • Added comprehensive tests to validate the behavior of the new string-to-datetime conversion function across various scenarios.
  • Chores
    • Updated copyright year in multiple files from 2023 to 2025.

@github-actions github-actions bot added the enhancement New feature or request label Feb 20, 2025
Copy link
Contributor

coderabbitai bot commented Feb 20, 2025

📝 Walkthrough

Walkthrough

The pull request updates copyright notices in several interpolation modules from 2023 to 2025. Additionally, in the macros module, a private function (_str_to_datetime) is renamed to a public function (str_to_datetime), and its references are updated. New unit tests have been added to validate the behavior of the converted str_to_datetime function. There are no changes to the core functionalities or control flow aside from these modifications.

Changes

File(s) Change Summary
airbyte_cdk/sources/declarative/interpolation/{__init__.py, filters.py, interpolated_boolean.py, interpolated_mapping.py, interpolated_nested_mapping.py, interpolated_string.py, interpolation.py, jinja.py} Updated copyright notice from 2023 to 2025.
airbyte_cdk/sources/declarative/interpolation/macros.py Renamed _str_to_datetime to str_to_datetime and updated its references; updated copyright notice.
unit_tests/sources/declarative/interpolation/test_macros.py Added new tests to validate string-to-datetime conversions.
airbyte_cdk/sources/declarative/declarative_component_schema.yaml Added new function str_to_datetime for converting strings to datetime objects with UTC timezone.

Sequence Diagram(s)

sequenceDiagram
    participant TS as Test Suite
    participant F as str_to_datetime Function
    TS->>F: Call str_to_datetime(input_string)
    F-->>TS: Return datetime object
Loading

Would you like any additional details or modifications to this diagram, wdyt?

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

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:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • 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. (Beta)
  • @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: 2

🧹 Nitpick comments (1)
airbyte_cdk/sources/declarative/interpolation/macros.py (1)

69-74: Great job on making the datetime parsing function public! 🎉

The function is well-documented and handles timezone information appropriately. Since this is now part of the public API, would you consider adding docstring with usage examples? wdyt?

 def str_to_datetime(s: str) -> datetime.datetime:
+    """
+    Converts a string to a datetime object, assuming UTC if no timezone is specified.
+    
+    Usage:
+    `"{{ str_to_datetime('2023-01-01T00:00:00Z') }}"`
+    `"{{ str_to_datetime('2023-01-01') }}"`  # Assumes UTC
+    
+    :param s: datetime string in ISO format
+    :return: datetime object in UTC timezone
+    """
     parsed_date = parser.isoparse(s)
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between e1a182c and f2584c8.

📒 Files selected for processing (10)
  • airbyte_cdk/sources/declarative/interpolation/__init__.py (1 hunks)
  • airbyte_cdk/sources/declarative/interpolation/filters.py (1 hunks)
  • airbyte_cdk/sources/declarative/interpolation/interpolated_boolean.py (1 hunks)
  • airbyte_cdk/sources/declarative/interpolation/interpolated_mapping.py (1 hunks)
  • airbyte_cdk/sources/declarative/interpolation/interpolated_nested_mapping.py (1 hunks)
  • airbyte_cdk/sources/declarative/interpolation/interpolated_string.py (1 hunks)
  • airbyte_cdk/sources/declarative/interpolation/interpolation.py (1 hunks)
  • airbyte_cdk/sources/declarative/interpolation/jinja.py (1 hunks)
  • airbyte_cdk/sources/declarative/interpolation/macros.py (4 hunks)
  • unit_tests/sources/declarative/interpolation/test_macros.py (1 hunks)
✅ Files skipped from review due to trivial changes (8)
  • airbyte_cdk/sources/declarative/interpolation/init.py
  • airbyte_cdk/sources/declarative/interpolation/interpolated_string.py
  • airbyte_cdk/sources/declarative/interpolation/jinja.py
  • airbyte_cdk/sources/declarative/interpolation/interpolated_boolean.py
  • airbyte_cdk/sources/declarative/interpolation/interpolation.py
  • airbyte_cdk/sources/declarative/interpolation/filters.py
  • airbyte_cdk/sources/declarative/interpolation/interpolated_nested_mapping.py
  • airbyte_cdk/sources/declarative/interpolation/interpolated_mapping.py
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Check: 'source-shopify' (skip=false)
  • GitHub Check: Pytest (All, Python 3.11, Ubuntu)
  • GitHub Check: Pytest (All, Python 3.10, Ubuntu)
🔇 Additional comments (1)
airbyte_cdk/sources/declarative/interpolation/macros.py (1)

175-175: LGTM on adding to _macros_list! 👍

Good catch on adding the newly public function to the macros list.

Copy link
Contributor

@natikgadzhi natikgadzhi left a comment

Choose a reason for hiding this comment

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

One nit on docs, I'm okay if docs follow in a follow-up PR later this week.

@pnilan pnilan enabled auto-merge (squash) February 20, 2025 02:49
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 (2)
unit_tests/sources/declarative/interpolation/test_macros.py (1)

125-148: Consider adding more test cases for better coverage.

The current test cases look good, but would you consider adding tests for:

  • Invalid date strings to verify error handling
  • Different timezone offsets (e.g., +0530, -0800)
  • Microseconds precision
    wdyt?
@@ -125,6 +125,21 @@
 @pytest.mark.parametrize(
     "test_name, input_value, expected_output",
     [
+        (
+            "test_invalid_date",
+            "invalid-date",
+            pytest.raises(ValueError, match="Invalid date format")
+        ),
+        (
+            "test_datetime_with_timezone_offset",
+            "2022-01-01T13:45:30+05:30",
+            datetime.datetime(2022, 1, 1, 8, 15, 30, tzinfo=datetime.timezone.utc),
+        ),
+        (
+            "test_datetime_with_microseconds",
+            "2022-01-01T13:45:30.123456Z",
+            datetime.datetime(2022, 1, 1, 13, 45, 30, 123456, tzinfo=datetime.timezone.utc),
+        ),
         (
             "test_basic_date",
             "2022-01-14",
airbyte_cdk/sources/declarative/declarative_component_schema.yaml (1)

3747-3754: Consider adding more examples to improve documentation.

The macro documentation looks good, but would you consider adding more examples to demonstrate different input formats? For instance:

  • Datetime with time
  • Datetime with timezone
  • Datetime with microseconds
    wdyt?
       examples:
         - "{{ str_to_datetime('2022-01-14') }}"
+        - "{{ str_to_datetime('2022-01-01 13:45:30') }}"
+        - "{{ str_to_datetime('2022-01-01T13:45:30+00:00') }}"
+        - "{{ str_to_datetime('2022-01-01T13:45:30.123456Z') }}"
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between f2584c8 and 31aa51a.

📒 Files selected for processing (3)
  • airbyte_cdk/sources/declarative/declarative_component_schema.yaml (1 hunks)
  • airbyte_cdk/sources/declarative/interpolation/macros.py (4 hunks)
  • unit_tests/sources/declarative/interpolation/test_macros.py (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • airbyte_cdk/sources/declarative/interpolation/macros.py
⏰ Context from checks skipped due to timeout of 90000ms (7)
  • GitHub Check: Check: 'source-pokeapi' (skip=false)
  • GitHub Check: Check: 'source-amplitude' (skip=false)
  • GitHub Check: Check: 'source-shopify' (skip=false)
  • GitHub Check: Check: 'source-hardcoded-records' (skip=false)
  • GitHub Check: Pytest (All, Python 3.11, Ubuntu)
  • GitHub Check: Pytest (Fast)
  • GitHub Check: Pytest (All, Python 3.10, Ubuntu)

@pnilan pnilan disabled auto-merge February 20, 2025 02:52
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)
unit_tests/sources/declarative/interpolation/test_macros.py (1)

163-166: LGTM! Good error handling test.

The error case test ensures robustness against invalid inputs.

Would you like to add more specific error cases? For example:

  • Empty string
  • None input
  • Malformed but date-like strings (e.g., "2022-13-45")

wdyt?

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 31aa51a and 3bee864.

📒 Files selected for processing (2)
  • airbyte_cdk/sources/declarative/declarative_component_schema.yaml (1 hunks)
  • unit_tests/sources/declarative/interpolation/test_macros.py (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • airbyte_cdk/sources/declarative/declarative_component_schema.yaml
⏰ Context from checks skipped due to timeout of 90000ms (8)
  • GitHub Check: Check: 'source-pokeapi' (skip=false)
  • GitHub Check: Check: 'source-amplitude' (skip=false)
  • GitHub Check: Check: 'source-shopify' (skip=false)
  • GitHub Check: Check: 'source-hardcoded-records' (skip=false)
  • GitHub Check: Pytest (All, Python 3.11, Ubuntu)
  • GitHub Check: Pytest (Fast)
  • GitHub Check: Pytest (All, Python 3.10, Ubuntu)
  • GitHub Check: Analyze (python)
🔇 Additional comments (2)
unit_tests/sources/declarative/interpolation/test_macros.py (2)

2-2: LGTM!

The copyright notice has been updated appropriately.


125-161: Great test coverage! 🎯

The test cases comprehensively cover various date string formats and edge cases. I particularly like the inclusion of timezone offsets and microseconds precision.

However, I noticed from the past review comment that there might be a signature mismatch with the implementation. The implementation in macros.py accepts only (s: str), but some test cases seem to expect format parameters. Should we:

  1. Update the implementation to support custom formats, or
  2. Remove format-related test cases?

wdyt?

@pnilan pnilan enabled auto-merge (squash) February 20, 2025 03:00
@pnilan pnilan merged commit 978be1b into main Feb 20, 2025
23 of 24 checks passed
@pnilan pnilan deleted the pnilan/update-jinja-macro branch February 20, 2025 03:15
rpopov added a commit to rpopov/airbyte-python-cdk that referenced this pull request Mar 5, 2025
* main:
  fix: update cryptography package to latest version to address CVE (airbytehq#377)
  fix: (CDK) (HttpRequester) - Make the `HttpRequester.path` optional (airbytehq#370)
  feat: improved custom components handling (airbytehq#350)
  feat: add microseconds timestamp format (airbytehq#373)
  fix: Replace Unidecode with anyascii for permissive license (airbytehq#367)
  feat: add IncrementingCountCursor (airbytehq#346)
  feat: (low-code cdk)  datetime format with milliseconds (airbytehq#369)
  fix: (CDK) (AsyncRetriever) - Improve UX on variable naming and interpolation (airbytehq#368)
  fix: (CDK) (AsyncRetriever) - Add the `request` and `response` to each `async` operations (airbytehq#356)
  fix: (CDK) (ConnectorBuilder) - Add `auxiliary requests` to slice; support `TestRead` for AsyncRetriever (part 1/2) (airbytehq#355)
  feat(concurrent perpartition cursor): Add parent state updates (airbytehq#343)
  fix: update csv parser for builder compatibility (airbytehq#364)
  feat(low-code cdk): add interpolation for limit field in Rate (airbytehq#353)
  feat(low-code cdk): add AbstractStreamFacade processing as concurrent streams in declarative source (airbytehq#347)
  fix: (CDK) (CsvParser) - Fix the `\\` escaping when passing the `delimiter` from Builder's UI (airbytehq#358)
  feat: expose `str_to_datetime` jinja macro (airbytehq#351)
  fix: update CDK migration for 6.34.0 (airbytehq#348)
  feat: Removes `stream_state` interpolation from CDK (airbytehq#320)
  fix(declarative): Pass `extra_fields` in `global_substream_cursor` (airbytehq#195)
  feat(concurrent perpartition cursor): Refactor ConcurrentPerPartitionCursor (airbytehq#331)
  feat(HttpMocker): adding support for PUT requests and bytes responses (airbytehq#342)
  chore: use certified source for manifest-only test (airbytehq#338)
  feat: check for request_option mapping conflicts in individual components (airbytehq#328)
  feat(file-based): sync file acl permissions and identities (airbytehq#260)
  fix: (CDK) (Connector Builder) - refactor the `MessageGrouper` > `TestRead` (airbytehq#332)
  fix(low code): Fix missing cursor for ClientSideIncrementalRecordFilterDecorator (airbytehq#334)
  feat(low-code): Add API Budget (airbytehq#314)
  chore(decoder): clean decoders and make csvdecoder available (airbytehq#326)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants