fix: honor boolean operators in -k snapshot selection - #1127
Merged
noahnu merged 1 commit intoJun 26, 2026
Conversation
frenck
marked this pull request as draft
June 26, 2026 11:46
Contributor
Author
|
Aah messed up my branches. Sorry, let me quickly fix that. ../Frenck Blogging my personal ramblings at frenck.dev |
frenck
force-pushed
the
frenck/fix-770-keyword-expression
branch
from
June 26, 2026 11:47
32ff7b3 to
1c8e65e
Compare
frenck
marked this pull request as ready for review
June 26, 2026 11:48
Contributor
Author
|
Done. ../Frenck Blogging my personal ramblings at frenck.dev |
syrupy reimplemented the pytest -k keyword expression with a hand rolled matcher that flattened the expression into tokens and returned true if any token was a substring of the snapshot name. This ignored the and/or/not and parenthesis operators, so a negated expression like -k 'not one' treated 'one' as a positive match and considered the deselected test_parametrized[one] snapshot unused. With --snapshot-update that unused snapshot was then deleted. Delegate parsing and evaluation to pytest's own _pytest.mark.expression so selection matches -k exactly. syrupy already requires pytest >= 8 and report.py already imports from _pytest, and compiled expressions are cached. Closes syrupy-project#770
frenck
force-pushed
the
frenck/fix-770-keyword-expression
branch
from
June 26, 2026 11:49
8d0387c to
cc3b7c7
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. @@ Coverage Diff @@
## main #1127 +/- ##
=======================================
Coverage 97.44% 97.44%
=======================================
Files 24 24
Lines 1758 1758
=======================================
Hits 1713 1713
Misses 45 45 🚀 New features to boost your workflow:
|
noahnu
approved these changes
Jun 26, 2026
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.
Description
syrupy reimplemented the pytest
-kkeyword expression with a hand rolled matcher (report.Expression) that flattened the expression into a set of tokens and returned true if any token was a substring of the snapshot name. This ignored theand/or/notand parenthesis operators entirely.As a result, a negated expression like
-k 'not one'treatedoneas a positive match, so the deselectedtest_parametrized[one]snapshot was considered unused. On a normal run that reports a spurious unused snapshot (and fails the suite), and with--snapshot-updatethe snapshot is then deleted, which is data loss.Minimal reproduction (from #770), no
pytest-bddrequired:This delegates parsing and evaluation to pytest's own
_pytest.mark.expression.Expression, so selection matches-kexactly, including boolean operators and parentheses. syrupy already requirespytest >= 8, andreport.pyalready imports from_pytest(_pytest.skipping), so this does not introduce a new kind of dependency. Compiled expressions are cached.Related Issues
-kexpression.Checklist
Additional Comments
Added regression tests in
tests/integration/test_snapshot_option_filter_keyword.pycovering a positive filter, a negated filter, and a negated filter under--snapshot-update(asserting the deselected snapshot is kept). The two negated cases fail onmainand pass with this change. Bracket and dash keyword forms that the existing tests rely on (for example-k 'test_used[') keep working, since pytest's parser accepts them as substring matchers. One# type: ignore[arg-type]is needed where syrupy'sCallable[[str], bool]matcher meets pytest's keyword-argument-tolerant matcher protocol; keyword matching only ever passes the positional name.