Make ascii_tree's optional df argument usable, and stop it mutating the caller's frame - #1225
Open
ppcvote wants to merge 1 commit into
Open
Make ascii_tree's optional df argument usable, and stop it mutating the caller's frame#1225ppcvote wants to merge 1 commit into
ppcvote wants to merge 1 commit into
Conversation
`ascii_tree(dt, df)` raised ValueError for every non-None df, so the second parameter in its own signature could never be used. helpers.py:380 tested the sentinel with `==`. On a DataFrame that is elementwise, so it returns a same-shaped boolean frame and the enclosing `if` calls DataFrame.__bool__, which pandas raises from by design. The default path survived only because `None == None` is a plain scalar True, and every caller in the repository and both README examples omit df, so nothing exercised it. base.py:706 re-exports the function, so both public routes failed identically. Dropping the "row" column is no longer done in place. That in-place drop was harmless only while df could not be supplied, because the frame always belonged to this function. Making the argument work makes the mutation reachable, and a caller who passes a frame carrying a "row" column would silently lose the column from their own object. Verified before the change: caller columns went from ['row', ...] to [...] after the call. Adds src/test/decision_tables/test_helpers.py; ascii_tree had no test coverage. Five tests, four of which fail on unmodified main. The fifth covers the df-omitted path and passes either way, so the suite is not vacuous. resolves CERTCC#1224
sei-vsarvepalli
self-requested a review
August 13, 2026 17:54
sei-vsarvepalli
approved these changes
Aug 13, 2026
sei-vsarvepalli
left a comment
Contributor
There was a problem hiding this comment.
Thanks for this patch.
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.
What was wrong
ascii_tree(dt, df)raisedValueErrorfor every non-Nonedf, so the optional second parameter in its own signature could never be used.helpers.py:380tested the sentinel with==:On a DataFrame
==is elementwise, so it returns a same-shaped boolean frame and the enclosingifcallsDataFrame.__bool__, which pandas raises from by design. The default path survived only becauseNone == Noneis a plain scalarTrue. Every caller in the repository and both README examples omitdf, which is why nothing ever exercised it.base.py:706re-exports the function, so both public routes failed the same way.What changed
Two lines.
if df == None:becomesif df is None:. The parameter is documented aspd.DataFrame | Noneand the sentinel is identity, soisis the right test rather thanisinstance.The
"row"column is no longer dropped in place. This is the part I would most like a second opinion on, because it is not in the issue.The in-place drop was harmless only while
dfcould not be supplied: the frame always belonged to this function. Making the argument work makes the mutation reachable. A caller who passes a frame carrying a"row"column silently loses that column from their own object. Measured on unmodified code, with theis Nonefix applied so the call could proceed at all:df = df.drop(columns="row")keeps the column out of the tree while leaving the caller's object alone. If you would rather keep the in-place drop and document the mutation instead, that is a reasonable call and I will split it out.Tests
ascii_treehad no test coverage, so this addssrc/test/decision_tables/test_helpers.pywith five tests:maintest_df_omittedtest_df_suppliedtest_df_supplied_via_basetest_caller_frame_is_not_modifiedtest_row_column_is_excluded_from_the_treeThe first is deliberate: it covers the path every current caller takes and passes either way, so it demonstrates the fixture and the suite is not vacuous. The other four fail before and pass after, confirmed by stashing the change and re-running.
src/testhas the same single pre-existing failure before and after this branch,test_main.py::MyTestCase::test_expected_routers, which is unrelated.black --checkis clean on the new test file. I lefthelpers.pyformatting alone: it does not satisfyblackonmaineither, the diffblackwants is in the HTML block well away from this change, and thepsf/blackstep inpython-app.ymlis commented out, so reformatting it here would be unrelated churn.