Skip to content

Dead code in TriangleDunders._prep_columns() #1045

Description

@genedan

Are you on the latest chainladder version?

  • Yes, this bug occurs on the latest version.

Describe the bug in words

Lines 147 - 150 can't be reached:

def _prep_columns(self, x, y):
x_backend, y_backend = x.array_backend, y.array_backend
if len(x.columns) == 1 and len(y.columns) > 1:
x.vdims = y.vdims
elif len(y.columns) == 1 and len(x.columns) > 1:
y.vdims = x.vdims
elif len(y.columns) == len(x.columns) == 1 and x.columns != y.columns:
y.vdims = x.vdims
elif x.shape[1] == y.shape[1] and np.array_equal(x.columns, y.columns):
return x, y
else:
# Find columns to add to each triangle
cols_to_add_to_x = [col for col in y.columns if col not in x.columns]
cols_to_add_to_y = [col for col in x.columns if col not in y.columns]
# Start with case with no new columns, y simply has a different order
new_x_cols = list(x.columns)
# Then, if there are new columns, add them.
if cols_to_add_to_x:
new_x_cols = list(x.columns) + list(cols_to_add_to_x)
x = x.reindex(columns=new_x_cols, fill_value=0)
if cols_to_add_to_y:
new_y_cols = list(y.columns) + list(cols_to_add_to_y)
y = y.reindex(columns=new_y_cols, fill_value=0)
# Ensure both triangles have the same column order
x = x[new_x_cols]
y = y[new_x_cols]
# Reset backends only if they've changed
if x.array_backend != x_backend:
x = x.set_backend(x_backend, inplace=True)
if y.array_backend != y_backend:
y = y.set_backend(y_backend, inplace=True)
return x, y

How can the bug be reproduced?

This was discovered when trying to write unit tests for them. This diagnostic was generated by Claude, so I want to double check whether it's correct before we remove the lines:

● These lines are dead code — the backend never changes inside _prep_columns. Here's why:

  1. _prep_columns is always called after _compatibility_check, which calls set_common_backend to align both triangles to the same backend before we get here.
  2. None of the operations inside _prep_columns (vdims assignment, reindex, column slicing) change the backend — I verified this empirically with a monkey-patch that would detect any change.
  So x.array_backend != x_backend and y.array_backend != y_backend are always False, and lines 146/148 can never execute. There's no test that can reach them.

What is the expected behavior?

My IDE detects that there's only 1 usage of TriangleDunders._prep_columns(), and it does occur after _compatibility_check(), so item 1 is correct. We just need to check item 2 before we remove the lines.

Would you be willing to contribute this ticket?

  • Yes, absolutely!
  • Yes, but I would like some help.
  • No.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Great First Contribution! 🌱Beginner friendly tickets with narrow scope and huge impact. Perfect to get your feet wet!

    Type

    Fields

    Priority

    Low

    Effort

    Low

    Scope

    Codebase

    Projects

    Status
    Backlog

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions