Skip to content

BUG: Fix AttributeError in from_adjlist when weight column is missing#859

Open
nimra06 wants to merge 2 commits into
pysal:mainfrom
nimra06:fix/669-try-weightcol-getattr-bug
Open

BUG: Fix AttributeError in from_adjlist when weight column is missing#859
nimra06 wants to merge 2 commits into
pysal:mainfrom
nimra06:fix/669-try-weightcol-getattr-bug

Conversation

@nimra06

@nimra06 nimra06 commented Jan 27, 2026

Copy link
Copy Markdown
  1. You have run tests on this submission locally using pytest on your changes. Continuous integration will be run on all PRs with GitHub Actions, but it is good practice to test changes locally prior to a making a PR.
  2. This pull request is directed to the pysal/master branch.
  3. This pull introduces new functionality covered by
    docstrings and
    unittests?
  4. You have assigned a
    reviewer
    and added relevant labels
  5. The justification for this PR is:

Found a bug in from_adjlist() where it crashes with AttributeError if the weight column doesn't exist in the DataFrame. The code was using getattr() to check for the column but that checks object attributes not DataFrame columns. So when the column is missing, it throws an error before it can create the default weight column.

I replaced the getattr() with weight_col in adjlist.columns which properly checks if the column exists. Now when the weight column is missing, it creates it with value 1 as the docstring says it should. Added a couple tests to make sure this works for both the default weight column and custom column names.

Fixes #669
AI assistance was used for code development. All explanations are written by me.

Replace getattr() call with column existence check to properly handle
DataFrames without the specified weight column. The previous code would
raise AttributeError before checking if the column exists, making the
fallback code unreachable.

- Replace getattr(adjlist, weight_col) with weight_col in adjlist.columns check
- Remove unreachable try_weightcol None check
- Add tests for missing weight column scenarios

Fixes pysal#669
@codecov

codecov Bot commented Jan 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.5%. Comparing base (7304239) to head (8745dc8).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@          Coverage Diff          @@
##            main    #859   +/-   ##
=====================================
  Coverage   85.5%   85.5%           
=====================================
  Files        151     151           
  Lines      16092   16105   +13     
=====================================
+ Hits       13758   13773   +15     
+ Misses      2334    2332    -2     
Files with missing lines Coverage Δ
libpysal/weights/tests/test_adjlist.py 100.0% <100.0%> (ø)
libpysal/weights/weights.py 93.7% <100.0%> (+0.3%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

try_weightcol is incorrectly defined using getattr()

1 participant