fix(strategies): use is not None for strategy presence checks (#2307)#2328
Closed
jbbqqf wants to merge 1 commit into
Closed
fix(strategies): use is not None for strategy presence checks (#2307)#2328jbbqqf wants to merge 1 commit into
is not None for strategy presence checks (#2307)#2328jbbqqf wants to merge 1 commit into
Conversation
…ionai-oss#2307) Hypothesis strategies implement ``__bool__`` to raise HypothesisWarning ("bool(from_dtype(...)) is always True, did you mean to draw a value?"). Three remaining call sites in pandera/strategies/pandas_strategies.py write ``if strategy:`` to test whether a chained parent strategy was supplied. While harmless when called with the default ``strategy=None``, the truthiness check is incorrect when a caller passes a real hypothesis strategy (the entry-points are base-strategy-only and raise BaseStrategyOnlyError in that case anyway) and silently emits the warning before the error fires. Switch to the explicit ``is not None`` form, matching the convention already used elsewhere in the same module. Add a structural regression test that fails on origin/main and passes on this branch. Co-Authored-By: Claude Code <noreply@anthropic.com> Signed-off-by: jbb <jbaptiste.braun@gmail.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2328 +/- ##
=======================================
Coverage 83.51% 83.51%
=======================================
Files 190 190
Lines 16613 16613
=======================================
Hits 13875 13875
Misses 2738 2738 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
Three remaining
if strategy:truthiness checks inpandera/strategies/pandas_strategies.pytest for a chained parent strategy. Hypothesis strategies implement__bool__to raiseHypothesisWarning("bool(from_dtype(...)) is always True, did you mean to draw a value?"), so the truthy form is incorrect (and silently emits the warning) when a real strategy is supplied. Switch the three sites to the explicitis not Noneform, matching the convention already used elsewhere in the same module.Resolves #2307.
Context
The reporter (#2307) cited two sites at lines 832 and 1055 that have already been migrated to
is None/is not None. Three remained:pandera/strategies/pandas_strategies.py:1244—series_strategyentry-pointpandera/strategies/pandas_strategies.py:1571—dataframe_strategyentry-pointpandera/strategies/pandas_strategies.py:1843—multiindex_strategyentry-pointThese are base-strategy entry-points that raise
BaseStrategyOnlyErrorwhen a parent strategy is supplied (it's a usage error to chain a parent onto a base strategy). The fix is one-token-per-site:if strategy:→if strategy is not None:.Changes
pandera/strategies/pandas_strategies.py: threeif strategy:→if strategy is not None:rewrites, plus a one-shot comment at the first site explaining why (so a future contributor doesn't "simplify" it back).tests/strategies/test_strategies.py: structural regression test that scans the module source and asserts noif strategy:lines remain. The test fails onorigin/mainand passes on this branch.A behavioural test would only fire when a caller passes a parent strategy (which is itself a usage error) — the structural test is a closer fit for the actual contract this PR enforces.
Reproduce BEFORE/AFTER yourself (copy-paste)
What I ran locally
pytest tests/strategies/test_strategies.py::test_strategy_truthiness_check_uses_is_not_none -v→ 1/1 passed.origin/main(3 remaining matches reported).The pre-existing
with pytest.raises(BaseStrategyOnlyError)cases at lines 532, 692, 731 oftests/strategies/test_strategies.pyexercise the non-None code path and continue to pass —is not Noneis logically equivalent to truthiness for the only valid input shape (a realSearchStrategyinstance).Edge cases tested
is not Nonestrategy=None) doesn't raiseRisk / blast radius
Three identical edits in one module. The new condition is logically equivalent for all observed inputs (
Noneand aSearchStrategysubclass), the latter being the only thing that ever reaches these branches.PR drafted with assistance from Claude Code. The change was reviewed manually against pandera's strategies module and against the HypothesisWarning quoted in #2307.