From ebf4174d5e649b5145362c7897433b3791587b7a Mon Sep 17 00:00:00 2001 From: uttam12331 Date: Mon, 20 Jul 2026 10:15:15 +0530 Subject: [PATCH] Fix Function.get_window() crashing when there is no OVER clause token_next_by() returns a (idx, token) tuple, which is always truthy, so 'if not over_clause' never triggered; when a function had no OVER clause, over_clause[1] was None and over_clause[1].tokens[-1] raised AttributeError instead of returning None. Guard on the token, matching the sibling get_parameters(). --- sqlparse/sql.py | 2 +- tests/test_grouping.py | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/sqlparse/sql.py b/sqlparse/sql.py index 5489f055..dabd88bd 100644 --- a/sqlparse/sql.py +++ b/sqlparse/sql.py @@ -638,7 +638,7 @@ def get_parameters(self): def get_window(self): """Return the window if it exists.""" over_clause = self.token_next_by(i=Over) - if not over_clause: + if over_clause[1] is None: return None return over_clause[1].tokens[-1] diff --git a/tests/test_grouping.py b/tests/test_grouping.py index bf278817..0caaf9a0 100644 --- a/tests/test_grouping.py +++ b/tests/test_grouping.py @@ -393,6 +393,10 @@ def test_grouping_function(): assert isinstance(p.tokens[0], sql.Function) assert len(list(p.tokens[0].get_parameters())) == 1 assert isinstance(p.tokens[0].get_window(), sql.Parenthesis) + # A function without an OVER clause has no window. + p = sqlparse.parse('foo(5)')[0] + assert isinstance(p.tokens[0], sql.Function) + assert p.tokens[0].get_window() is None def test_grouping_function_not_in():