Skip to content

Commit

Permalink
Mypy compatibility.
Browse files Browse the repository at this point in the history
String conversions.
  • Loading branch information
coady committed Jul 6, 2024
1 parent 9ba251d commit 097845f
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 30 deletions.
10 changes: 5 additions & 5 deletions lupyne/engine/analyzers.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def positionIncrement(self, index: int):
@property
def charTerm(self) -> str:
"""term text"""
return self.CharTerm.toString()
return str(self.CharTerm)

@charTerm.setter
def charTerm(self, text: str):
Expand Down Expand Up @@ -142,12 +142,12 @@ def parse(self, query: str, field='', op='', parser=None, **attrs) -> search.Que
cls = queryparser.classic.MultiFieldQueryParser
if isinstance(field, str):
cls = queryparser.classic.QueryParser
args = field, self
args: tuple = field, self
if isinstance(field, Mapping):
boosts = HashMap()
for key in field:
boosts.put(key, Float(field[key]))
args = list(field), self, boosts # type: ignore
args = list(field), self, boosts
parser = (parser or cls)(*args)
if op:
parser.defaultOperator = getattr(queryparser.classic.QueryParser.Operator, op.upper())
Expand All @@ -161,7 +161,7 @@ def parse(self, query: str, field='', op='', parser=None, **attrs) -> search.Que
if isinstance(parser, PythonQueryParser):
parser.finalize()

def highlight(self, query: search.Query, field: str, content: str, count: int = 1):
def highlight(self, query: search.Query, field: str, content: str, count: int = 1) -> str:
"""Return highlighted content.
Args:
Expand All @@ -171,4 +171,4 @@ def highlight(self, query: search.Query, field: str, content: str, count: int =
count: optional maximum number of passages
"""
highlighter = uhighlight.UnifiedHighlighter(None, self)
return highlighter.highlightWithoutSearcher(field, query, content, count).toString()
return str(highlighter.highlightWithoutSearcher(field, query, content, count))
28 changes: 12 additions & 16 deletions lupyne/engine/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import datetime
import operator
from collections.abc import Callable, Iterator, Sequence
from typing import Optional, Union, no_type_check
from typing import Optional, Union
import lucene # noqa
from java.lang import Long
from java.util import Arrays, HashSet
Expand Down Expand Up @@ -137,9 +137,9 @@ def __init__(self, name: str, sep: str = '.', **settings):

def values(self, value: str) -> Iterator[str]:
"""Generate component field values in order."""
value = value.split(self.sep) # type: ignore
for stop in range(1, len(value) + 1):
yield self.sep.join(value[:stop])
values = value.split(self.sep)
for stop in range(1, len(values) + 1):
yield self.sep.join(values[:stop])

def items(self, *values: str) -> Iterator[document.Field]:
"""Generate indexed component fields."""
Expand Down Expand Up @@ -228,33 +228,29 @@ class ShapeField:
def __init__(self, name: str, indexed=True, docvalues=False):
self.name, self.indexed, self.docvalues = name, bool(indexed), bool(docvalues)

@staticmethod
def as_tuple(shape: geo.Geometry) -> tuple:
def apply(self, func: Callable, shape: geo.Geometry):
if isinstance(shape, geo.Point):
return shape.lat, shape.lon
return func(self.name, shape.lat, shape.lon)
if isinstance(shape, geo.XYPoint):
return shape.x, shape.y
return (shape,)
return func(self.name, shape.x, shape.y)
return func(self.name, shape)

@no_type_check
def items(self, *shapes: geo.Geometry) -> Iterator[document.Field]:
"""Generate lucene shape fields from geometries."""
for shape in shapes:
cls = document.XYShape if isinstance(shape, geo.XYGeometry) else document.LatLonShape
args = self.as_tuple(shape)
if self.indexed:
yield from cls.createIndexableFields(self.name, *args)
yield from self.apply(cls.createIndexableFields, shape)
if self.docvalues:
yield cls.createDocValueField(self.name, *args)
yield self.apply(cls.createDocValueField, shape)

def distances(self, point: Union[geo.Point, geo.XYPoint]) -> search.SortField:
"""Return distance SortField."""
xy = isinstance(point, geo.XYGeometry)
cls = document.XYDocValuesField if xy else document.LatLonDocValuesField
return cls.newDistanceSort(self.name, *self.as_tuple(point))
return self.apply(cls.newDistanceSort, point)

@no_type_check
def query(self, relation: QueryRelation, *shapes: geo.Geometry) -> search.Query:
def query(self, relation: QueryRelation, *shapes: geo.Geometry) -> search.Query: # type: ignore
shape = shapes[0]
cls = document.XYShape if isinstance(shape, geo.XYGeometry) else document.LatLonShape
func = cls.newGeometryQuery
Expand Down
8 changes: 4 additions & 4 deletions lupyne/engine/indexers.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def __contains__(self, id: int):
def __iter__(self) -> Iterator[int]:
ids = range(self.maxDoc())
bits = self.bits
return filter(bits.get, ids) if bits else iter(ids) # type: ignore
return filter(bits.get, ids) if bits else iter(ids)

@property
def bits(self) -> util.Bits:
Expand Down Expand Up @@ -160,7 +160,7 @@ def sortfield(self, name: str, type=None, reverse=False) -> search.SortField:
reverse: reverse flag used with sort
"""
if type is None:
type = self.fieldinfos[name].docValuesType.toString()
type = str(self.fieldinfos[name].docValuesType)
type = Field.types.get(type, type).upper()
return search.SortField(name, getattr(search.SortField.Type, type), reverse)

Expand All @@ -175,7 +175,7 @@ def docvalues(self, name: str, type=None) -> DocValues.Sorted:
"""
types = {int: int, float: util.NumericUtils.sortableLongToDouble}
type = types.get(type, util.BytesRef.utf8ToString)
docValuesType = self.fieldinfos[name].docValuesType.toString().title().replace('_', '')
docValuesType = str(self.fieldinfos[name].docValuesType).title().replace('_', '')
method = getattr(index.MultiDocValues, f'get{docValuesType}Values')
return getattr(DocValues, docValuesType)(method(self.indexReader, name), len(self), type)

Expand Down Expand Up @@ -247,7 +247,7 @@ def positions(self, name: str, value, payloads=False, offsets=False) -> Iterator
docsenum = index.MultiTerms.getTermPostingsEnum(
self.indexReader, name, util.BytesRef(value)
)
for doc in iter(docsenum.nextDoc, index.PostingsEnum.NO_MORE_DOCS) if docsenum else (): # type: ignore
for doc in iter(docsenum.nextDoc, index.PostingsEnum.NO_MORE_DOCS) if docsenum else ():
positions = (docsenum.nextPosition() for _ in range(docsenum.freq()))
if payloads:
positions = (
Expand Down
2 changes: 1 addition & 1 deletion lupyne/engine/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def convert(value):
if util.BytesRef.instance_(value):
return util.BytesRef.cast_(value).utf8ToString()
if not Number.instance_(value):
return value.toString() if Object.instance_(value) else value
return str(value) if Object.instance_(value) else value
value = Number.cast_(value)
if Float.instance_(value) or Double.instance_(value):
return value.doubleValue()
Expand Down
6 changes: 2 additions & 4 deletions lupyne/services/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,8 @@ def index(self) -> dict:
"""index information"""
searcher = self.searcher
if isinstance(searcher, engine.MultiSearcher): # pragma: no cover
return {
reader.directory().toString(): reader.numDocs() for reader in searcher.indexReaders
}
return {searcher.directory.toString(): len(searcher)}
return {str(reader.directory()): reader.numDocs() for reader in searcher.indexReaders}
return {str(searcher.directory): len(searcher)}

def refresh(self, spellcheckers: bool = False) -> dict:
"""Refresh index version."""
Expand Down

0 comments on commit 097845f

Please sign in to comment.