Skip to content

refactor: Simplified type-conforming logic #2964

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

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

edgarrmondragon
Copy link
Collaborator

@edgarrmondragon edgarrmondragon commented Apr 11, 2025

Summary by Sourcery

Refactor the type-conforming logic in the _typing module to improve code structure and readability

Enhancements:

  • Restructured the _conform_primitive_property function to use a more modular and extensible type handling approach
  • Introduced separate handler functions for different data types to improve code organization

Copy link
Contributor

sourcery-ai bot commented Apr 11, 2025

Reviewer's Guide by Sourcery

The pull request refactors the _conform_primitive_property function by introducing a type-to-handler mapping. This change improves code structure and readability by extracting type-specific conversion logic into dedicated functions.

Sequence diagram for _conform_primitive_property function

sequenceDiagram
  participant Caller
  participant _conform_primitive_property
  participant _handle_datetime
  participant _handle_date
  participant _handle_timedelta
  participant _handle_time
  participant _handle_bytes
  participant _handle_numeric

  Caller->>_conform_primitive_property: _conform_primitive_property(elem, property_schema)
  alt elem is datetime.datetime
    _conform_primitive_property->>_handle_datetime: _handle_datetime(elem, property_schema)
    _handle_datetime-->>_conform_primitive_property: return converted elem
  else elem is datetime.date
    _conform_primitive_property->>_handle_date: _handle_date(elem, property_schema)
    _handle_date-->>_conform_primitive_property: return converted elem
  else elem is datetime.timedelta
    _conform_primitive_property->>_handle_timedelta: _handle_timedelta(elem, property_schema)
    _handle_timedelta-->>_conform_primitive_property: return converted elem
  else elem is datetime.time
    _conform_primitive_property->>_handle_time: _handle_time(elem, property_schema)
    _handle_time-->>_conform_primitive_property: return converted elem
  else elem is bytes
    _conform_primitive_property->>_handle_bytes: _handle_bytes(elem, property_schema)
    _handle_bytes-->>_conform_primitive_property: return converted elem
  else elem is float or decimal.Decimal
    _conform_primitive_property->>_handle_numeric: _handle_numeric(elem, property_schema)
    _handle_numeric-->>_conform_primitive_property: return converted elem
  else elem is boolean
    _conform_primitive_property->>_conform_primitive_property: elem != 0
    _conform_primitive_property-->>Caller: return converted elem
  else
    _conform_primitive_property-->>Caller: return elem
  end
Loading

File-Level Changes

Change Details Files
Refactored the _conform_primitive_property function to improve code structure and readability by using a type-to-handler mapping.
  • Introduced a dictionary type_handlers to map data types to their corresponding handler functions.
  • Created dedicated handler functions for datetime, date, timedelta, time, bytes, and numeric types.
  • Simplified the main function by delegating type-specific conversion logic to the handler functions.
  • Added handling for None values at the beginning of the function.
singer_sdk/helpers/_typing.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

codecov bot commented Apr 11, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.54%. Comparing base (687254f) to head (0a3d517).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2964      +/-   ##
==========================================
+ Coverage   91.50%   91.54%   +0.03%     
==========================================
  Files          62       62              
  Lines        5311     5311              
  Branches      684      678       -6     
==========================================
+ Hits         4860     4862       +2     
+ Misses        318      317       -1     
+ Partials      133      132       -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

codspeed-hq bot commented Apr 11, 2025

CodSpeed Performance Report

Merging #2964 will degrade performances by 21.46%

Comparing edgarrmondragon/refactor/better-type-conforming (0a3d517) with main (687254f)

Summary

❌ 1 regressions
✅ 7 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark BASE HEAD Change
test_bench_conform_record_data_types 75.3 ms 95.9 ms -21.46%

@edgarrmondragon
Copy link
Collaborator Author

@sourcery-ai review

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @edgarrmondragon - I've reviewed your changes - here's some feedback:

Overall Comments:

  • The new structure is more readable, but consider if the handler functions could be simplified further, perhaps by sharing common logic.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

edgarrmondragon and others added 2 commits April 11, 2025 21:03
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
@edgarrmondragon edgarrmondragon marked this pull request as ready for review April 14, 2025 15:08
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @edgarrmondragon - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding type hints to the elem argument in the handler functions for better clarity.
  • The _handle_datetime function doesn't need the property_schema argument, so you can remove it.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@edgarrmondragon edgarrmondragon added this to the v0.46 milestone Apr 22, 2025
@edgarrmondragon edgarrmondragon self-assigned this Apr 22, 2025
@github-project-automation github-project-automation bot moved this to To Discuss in Office Hours Apr 22, 2025
@edgarrmondragon edgarrmondragon added the Type/Tap Singer taps label Apr 22, 2025
@edgarrmondragon edgarrmondragon modified the milestones: v0.46, v0.47 May 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type/Tap Singer taps
Projects
Status: To Discuss
Development

Successfully merging this pull request may close these issues.

1 participant