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

Only lower if clean_key is instance of str #504

Merged
merged 2 commits into from
Dec 14, 2024
Merged

Conversation

vmatt
Copy link
Contributor

@vmatt vmatt commented Nov 18, 2024

Fix AttributeError when group_by key is None with ignore_string_case

Related Issue

Fixes #503

Description

This PR fixes a bug where using group_by with a nullable key raises an AttributeError when ignore_string_case=True. The error occurs because the code attempts to call .lower() on None values.

Changes

  • Modified the string case conversion logic in diff.py to check if the key is a string before applying .lower()
  • Added type checking using isinstance(clean_key, str) to safely handle None values
  • +1: Black formatting was automatically for tests/test_diff_text.py, @seperman let me know if it's an issue and I should revert.

Before

if self.ignore_string_case:
    clean_key = clean_key.lower()

After

if self.ignore_string_case and isinstance(clean_key, str):
    clean_key = clean_key.lower()

Testing

Added test case:

dict1 = [{'txt_field': 'FULL', 'group_id': None}]
dict2 = [{'txt_field': 'FULL', 'group_id': 'a'}]
diff = DeepDiff(
    dict1,
    dict2,
    ignore_order=True,
    group_by='group_id',
    ignore_string_case=True
)

Checklist

  • My code follows the style guidelines of this project
  • I have added tests that prove my fix is effective
  • New and existing unit tests pass locally with my changes

@vmatt vmatt marked this pull request as draft November 18, 2024 10:25
@vmatt vmatt mentioned this pull request Nov 18, 2024
Closed
@vmatt vmatt marked this pull request as ready for review November 18, 2024 11:20
@vmatt
Copy link
Contributor Author

vmatt commented Nov 26, 2024

hey @seperman, anything needed on my side to move this forward?

@seperman
Copy link
Owner

seperman commented Dec 6, 2024

@vmatt Thanks for making the PR!
Sorry I was busy in the past couple of weeks. Can you please not include the styling changes (Black formatting) in the PR?
Styling changes should be done in separate PRs that don't touch code complexity. Also, we are using Ruff, not Black and I will make a separate PR to reformat the whole package. Thanks!

@vmatt
Copy link
Contributor Author

vmatt commented Dec 7, 2024

@seperman, No worries, thanks for the feedback, I removed black formatting.

@seperman
Copy link
Owner

Thanks @vmatt !

@seperman seperman changed the base branch from master to dev December 14, 2024 20:30
Copy link
Owner

@seperman seperman left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link

codecov bot commented Dec 14, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.70%. Comparing base (6d8a4c7) to head (85adbd2).
Report is 29 commits behind head on dev.

Additional details and impacted files
@@           Coverage Diff           @@
##              dev     #504   +/-   ##
=======================================
  Coverage   96.70%   96.70%           
=======================================
  Files          14       14           
  Lines        3946     3946           
=======================================
  Hits         3816     3816           
  Misses        130      130           

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

@seperman seperman merged commit 514e025 into seperman:dev Dec 14, 2024
7 checks passed
@seperman seperman mentioned this pull request Dec 14, 2024
@seperman
Copy link
Owner

Hi @vmatt
Thanks for contributing to DeepDiff. DeepDiff 8.1.0 is released and includes your code!

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.

group_by fails if group_by key is None
2 participants