33# Copyright (C) 2016 Andi Albrecht, [email protected] 44#
55# This module is part of python-sqlparse and is released under
6- # the BSD License: http ://www. opensource.org/licenses/bsd-license.php
6+ # the BSD License: https ://opensource.org/licenses/BSD-3-Clause
77
88from sqlparse import sql
99from sqlparse import tokens as T
@@ -21,13 +21,13 @@ def _group_matching(tlist, cls):
2121 for idx , token in enumerate (list (tlist )):
2222 tidx = idx - tidx_offset
2323
24- if token .is_whitespace () :
24+ if token .is_whitespace :
2525 # ~50% of tokens will be whitespace. Will checking early
2626 # for them avoid 3 comparisons, but then add 1 more comparison
2727 # for the other ~50% of tokens...
2828 continue
2929
30- if token .is_group () and not isinstance (token , cls ):
30+ if token .is_group and not isinstance (token , cls ):
3131 # Check inside previously grouped (ie. parenthesis) if group
3232 # of differnt type is inside (ie, case). though ideally should
3333 # should check for all open/close tokens at once to avoid recursion
@@ -121,7 +121,7 @@ def valid_prev(token):
121121
122122 def valid_next (token ):
123123 ttypes = T .DML , T .DDL
124- return not imt (token , t = ttypes )
124+ return not imt (token , t = ttypes ) and token is not None
125125
126126 def post (tlist , pidx , tidx , nidx ):
127127 return pidx , nidx
@@ -246,7 +246,7 @@ def group_comments(tlist):
246246 tidx , token = tlist .token_next_by (t = T .Comment )
247247 while token :
248248 eidx , end = tlist .token_not_matching (
249- lambda tk : imt (tk , t = T .Comment ) or tk .is_whitespace () , idx = tidx )
249+ lambda tk : imt (tk , t = T .Comment ) or tk .is_whitespace , idx = tidx )
250250 if end is not None :
251251 eidx , end = tlist .token_prev (eidx , skip_ws = False )
252252 tlist .group_tokens (sql .Comment , tidx , eidx )
@@ -343,9 +343,9 @@ def group(stmt):
343343 group_period ,
344344 group_arrays ,
345345 group_identifier ,
346- group_operator ,
347346 group_order ,
348347 group_typecasts ,
348+ group_operator ,
349349 group_as ,
350350 group_aliased ,
351351 group_assignment ,
@@ -372,15 +372,15 @@ def _group(tlist, cls, match,
372372 for idx , token in enumerate (list (tlist )):
373373 tidx = idx - tidx_offset
374374
375- if token .is_whitespace () :
375+ if token .is_whitespace :
376376 continue
377377
378- if recurse and token .is_group () and not isinstance (token , cls ):
378+ if recurse and token .is_group and not isinstance (token , cls ):
379379 _group (token , cls , match , valid_prev , valid_next , post , extend )
380380
381381 if match (token ):
382382 nidx , next_ = tlist .token_next (tidx )
383- if valid_prev (prev_ ) and valid_next (next_ ):
383+ if prev_ and valid_prev (prev_ ) and valid_next (next_ ):
384384 from_idx , to_idx = post (tlist , pidx , tidx , nidx )
385385 grp = tlist .group_tokens (cls , from_idx , to_idx , extend = extend )
386386
0 commit comments