Skip to content

Fix TypeError in _token_matching when index is None (issue747)#869

Open
deepakganesh78 wants to merge 1 commit into
andialbrecht:masterfrom
deepakganesh78:fix/issue747-token-matching-none-idx
Open

Fix TypeError in _token_matching when index is None (issue747)#869
deepakganesh78 wants to merge 1 commit into
andialbrecht:masterfrom
deepakganesh78:fix/issue747-token-matching-none-idx

Conversation

@deepakganesh78

Copy link
Copy Markdown
Contributor

Summary

TokenList._token_matching returns a bare None when called with a None start index, but all callers (token_matching, token_first, token_not_matching, ...) treat the return value as a (idx, token) tuple. As reported in #747 this makes a None index crash:

import sqlparse
p = sqlparse.parse("SELECT id FROM table")[0]
p.token_matching([lambda x: isinstance(x, sqlparse.sql.Token)], None)
# TypeError: 'NoneType' object is not subscriptable

Root cause

The early-out branch for start is None returned None instead of the (None, None) tuple the normal no-match path returns, breaking the method's contract with every caller.

Fix

Return (None, None) from that branch so a None index simply reports "no match"; token_matching then correctly returns None.

Tests

Added test_token_matching_none_idx_issue747 in tests/test_regressions.py. Full suite:

492 passed, 2 xfailed, 1 xpassed

Fixes #747

`TokenList._token_matching` returned a bare `None` when it was given a
`None` start index, but every caller (`token_matching`, `token_first`,
`token_not_matching`, ...) unpacks or subscripts the result as a
`(idx, token)` tuple. Passing `None` therefore raised

    TypeError: 'NoneType' object is not subscriptable

instead of reporting "no match".

Return `(None, None)` in that early-out branch so the contract matches
the normal no-match return value. Adds a regression test.

Fixes andialbrecht#747
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.

Token matching generates an exception when index is None

1 participant