STY: Add strict=True in zip() in \core, \tests and \scripts #62984
+33
−21
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This change enforces Ruff rule B905 (zip-without-explicit-strict) by adding
strict=Trueorstrict=Falseparameters to allzip()function calls in several critical pandas modules. This ensures that input iterables are of equal length, preventing silent truncation and potential data inconsistencies.According to issue #62434, all
zip()calls should ideally specifystrict=Truesince most of the time there's an underlying assumption that the input arguments are of equal length.Modified Files
1. pandas/core/reshape/merge.py
Purpose: Core merge operation logic for combining DataFrames
Specific Changes (9
zip()calls updated):zip(self.join_names, self.left_on, self.right_on)zip(self.join_names, self.left_on, self.right_on, strict=True)zip(self.join_names, self.left_on, self.right_on)zip(self.join_names, self.left_on, self.right_on, strict=True)zip(self.left_on, self.right_on)zip(self.left_on, self.right_on, strict=True)zip(self.right.index.levels, self.right.index.codes)zip(self.right.index.levels, self.right.index.codes, strict=True)zip(self.left.index.levels, self.left.index.codes)zip(self.left.index.levels, self.left.index.codes, strict=True)zip(self.left_join_keys, self.right_join_keys, self.join_names)zip(self.left_join_keys, self.right_join_keys, self.join_names, strict=True)zip(*mapped)zip(*mapped, strict=True)zip(left_join_keys, right_join_keys)zip(left_join_keys, right_join_keys, strict=True)zip(*mapped)zip(*mapped, strict=True)Rationale: In merge operations, the join keys, levels, and codes must be of equal length. Using
strict=Trueensures that any data inconsistency will raise an error rather than silently truncating data.2. pandas/core/reshape/concat.py
Purpose: Concatenation of DataFrame/Series objects
Specific Changes (3
zip()calls updated):zip(zipped, levels)zip(zipped, levels, strict=True)zip(hlevel, indexes)zip(hlevel, indexes, strict=False)zip(zipped, levels)zip(zipped, levels, strict=True)3. pandas/tests/plotting/frame/test_frame.py
Purpose: Test cases for DataFrame plotting functionality
Specific Changes (7
zip()calls updated):zip(string.ascii_letters[:10], range(10))zip(string.ascii_letters[:10], range(10), strict=True)zip(normal_lines, stacked_lines)zip(normal_lines, stacked_lines, strict=True)zip(result_yticklabels, expected_yticklabels)zip(result_yticklabels, expected_yticklabels, strict=True)zip(axes, labels)zip(axes, labels, strict=True)dict(zip(["A", "B"], ...))dict(zip(["A", "B"], ..., strict=True))zip(axes, df.columns)zip(axes, df.columns, strict=True)dict(zip(xticklabels, ax.get_xticks()))dict(zip(xticklabels, ax.get_xticks(), strict=False))dict(zip(xticklabels, ax.get_xticks()))dict(zip(xticklabels, ax.get_xticks(), strict=False))zip(axes, expected_labels)zip(axes, expected_labels, strict=True)4. pandas/tests/reshape/merge/test_merge_asof.py
Purpose: Test cases for asof-merge functionality
Specific Changes (3
zip()calls updated):list(zip([0, 5, 10, 15, 20, 25], [0, 1, 2, 3, 4, 5]))list(zip([0, 5, 10, 15, 20, 25], [0, 1, 2, 3, 4, 5], strict=True))list(zip([0, 3, 9, 12, 15, 18], [0, 1, 2, 3, 4, 5]))list(zip([0, 3, 9, 12, 15, 18], [0, 1, 2, 3, 4, 5], strict=True))strict=Trueto thezip()callRationale: Test data construction expects exact pairing of time intervals and values to ensure asof-merge semantics are correctly tested.
5. scripts/validate_unwanted_patterns.py
Purpose: Script for validating code patterns
Specific Changes (1
zip()call updated):zip(tokens, tokens[1:], tokens[2:])zip(tokens, tokens[1:], tokens[2:], strict=False)Rationale: The three lists have different lengths (original vs. slices), so
strict=Falseis appropriate.