11#!/usr/bin/env python3
22"""Pubmed query translator."""
3+ import typing
34from collections import defaultdict
45from itertools import permutations
56
@@ -119,6 +120,7 @@ def _combine_tiab(cls, query: "Query") -> None:
119120 for child in query .children :
120121 cls ._combine_tiab (child )
121122
123+ # pylint: disable=too-many-locals, too-many-branches
122124 @classmethod
123125 def _collapse_near_queries (cls , query : Query ) -> Query :
124126 """Recursively collapse NEAR queries in the query tree."""
@@ -132,11 +134,12 @@ def _collapse_near_queries(cls, query: Query) -> Query:
132134 query .children .pop ()
133135 return query
134136
135- elif query .value == Operators .OR :
137+ if query .value == Operators .OR :
136138 # Extract NEAR queries
137- near_queries = []
138- other_queries = []
139+ near_queries : typing . List [ Query ] = []
140+ other_queries : typing . List [ Query ] = []
139141 for child in query .children :
142+ # pylint: disable=unidiomatic-typecheck
140143 (near_queries if type (child ) is NEARQuery else other_queries ).append (
141144 child
142145 )
@@ -152,11 +155,13 @@ def _collapse_near_queries(cls, query: Query) -> Query:
152155
153156 combined_near_queries = []
154157 for distance , queries in grouped_queries .items ():
155- # For each group, extract term pairs from NEAR queries and map them to corresponding fields
158+ # For each group, extract term pairs from NEAR queries and
159+ # map them to corresponding fields
156160 term_field_map = defaultdict (set )
157161 for q in queries :
158162 term_a = q .children [0 ].value
159163 term_b = q .children [1 ].value
164+ assert q .children [0 ].search_field
160165 term_field_map [(min (term_a , term_b ), max (term_a , term_b ))].add (
161166 q .children [0 ].search_field .value
162167 )
@@ -175,7 +180,7 @@ def _collapse_near_queries(cls, query: Query) -> Query:
175180 children = [
176181 Term (
177182 value = f'"{ term_a } { term_b } "' ,
178- search_field = field ,
183+ search_field = SearchField ( value = field ) ,
179184 platform = "deactivated" ,
180185 )
181186 ],
@@ -210,6 +215,7 @@ def translate_search_fields_to_generic(cls, query: Query) -> Query:
210215 if query .children :
211216 if query .value == Operators .NEAR :
212217 # Expand NEAR queries
218+ assert query .children [0 ].search_field
213219 search_field_set = syntax_str_to_generic_search_field_set (
214220 query .children [0 ].search_field .value
215221 )
@@ -254,22 +260,25 @@ def _expand_combined_fields(cls, query: Query, search_fields: set) -> Query:
254260 )
255261 )
256262 return OrQuery (
257- children = query_children ,
263+ children = query_children , # type: ignore
258264 search_field = None ,
259265 )
260266
261267 @classmethod
262268 def _expand_near_query (cls , query : Query , search_fields : set ) -> Query :
263269 """Expand NEAR query into an OR query"""
264- if type (query ) is not NEARQuery :
270+ if type (query ) is not NEARQuery : # pylint: disable=unidiomatic-typecheck
265271 return query
272+ distance = 0
273+ if hasattr (query , "distance" ):
274+ distance = int (query .distance ) # type: ignore
266275
267- distance = query .distance if hasattr (query , "distance" ) else 0
268276 query_children = []
269277 search_terms = query .children [0 ].value .strip ('"' ).split ()
270278 # Handle [tiab] by generating NEAR queries for both 'title' and 'abstract'
271279 for search_field in sorted (list (search_fields )):
272- # Get all possible ordered pairs of search terms in the proximity search phrase
280+ # Get all possible ordered pairs of search terms
281+ # in the proximity search phrase
273282 pairs = list (permutations (search_terms , 2 ))
274283 for pair in pairs :
275284 # Create binary near query for each pair
@@ -290,7 +299,7 @@ def _expand_near_query(cls, query: Query, search_fields: set) -> Query:
290299 )
291300 )
292301 return OrQuery (
293- children = query_children ,
302+ children = query_children , # type: ignore
294303 search_field = None ,
295304 )
296305
0 commit comments