Repository navigation
Fix PythonRstNode.__contains__ returning a match index instead of a bool - #248
Merged
Merged
Conversation
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.
Problem
__contains__returned the raw result offind_in_list, which is an index, not a boolean:find_in_listreturns the end index of the match, or a negative sentinel when there is none. Python coerces whatever__contains__returns withbool(), so theinoperator was inverted at both ends of the range:find_in_listitem in node-2TrueFalse0FalseTruenTrueTrueSo
x in nodeanswered "yes" for everything that is not there, and "no" for a match on the first child.How it surfaced
Adding the missing
-> boolreturn annotation (ANN204) made pyright report:The magic
-2find_in_listhad a bare-2as its "not found" value, while the module already defines sentinels on lines 14-15:-2is not one of them and carried no meaning — every caller only tests the sign (found_position >= 0inmatch_pattern,less_than(0)intest_match_tree). It is replaced byMIS_MATCH, which is what the function means and is already the sentinelfind_variantsuses internally for a rejected variant. No caller or test depends on the literal-2(verified by search).Test correction
test_is_match_all_stmtpassed only because of the bug:find_in_listreturned-2, and-2is truthy.Two things were wrong.
create()wraps the pattern in a module, so.nodewas theModuletranslation unit (pattern.py), not a statement — it could never match a statement inatu. Andpattern_kindis resolved on thePythonPatternwrapper, not on the inner.node, so evencreate_statement(...).nodeis not recognised asMATCH_ALL:parser_kindpattern_kindfind_in_listcreate("$$pa")ModuleNone-2create("$$pa").nodeModuleNone-2create_statement("$$pa").nodeExprNone-2create_statement("$$pa")Exprmatch_all3The test now uses the form that actually expresses its intent, and passes for the right reason — a
$$pawildcard matches the whole 4-statement module, end index3.Changes
src/renaissance/integrations/python/ast/rst_node.py—return find_in_list(self.children, item) >= 0src/renaissance/syntax_tree/match_finder.py—return -2→return MIS_MATCH, docstring updatedtest/python/ast/test_patternic_style.py— assert on the pattern, not on its.nodeVerification
ruff checkclean,ruff format --checkcleanlint_budget --checkgreen; removes thereportReturnTyperegression