Skip to content

Commit

Permalink
Deprecate *args
Browse files Browse the repository at this point in the history
  • Loading branch information
adamjstewart committed Jan 6, 2025
1 parent c27c12a commit 9dd17cf
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
28 changes: 26 additions & 2 deletions rtree/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class Index:

def __init__(
self,
*args: Any,
filename: str | bytes | None = None,
stream: Any | None = None,
storage: ICustomStorage | None = None,
Expand All @@ -89,10 +90,10 @@ def __init__(
) -> None:
"""Creates a new index
:param filename: a filename determining that a file-based storage for the index
:param filename: A filename determining that a file-based storage for the index
should be used.
:param stream: an iterable stream of data that will raise a StopIteration.
:param stream: An iterable stream of data that will raise a StopIteration.
It must be in the form defined by the :attr:`interleaved` attribute of the
index. The following example would assume :attr:`interleaved` is False::
Expand Down Expand Up @@ -197,6 +198,29 @@ def __init__(
"TPR-Tree type not supported with version of libspatialindex"
)

if args:
msg = "Index() parameters without keyword arguments are deprecated"
warnings.warn(msg, DeprecationWarning)

if isinstance(args[0], str) or isinstance(args[0], bytes):
# they sent in a filename
filename = args[0]
# they sent in a filename, stream or filename, buffers
if len(args) > 1:
if isinstance(args[1], tuple):
arrays = args[1]
else:
stream = args[1]
elif isinstance(args[0], ICustomStorage):
storage = args[0]
# they sent in a storage, stream
if len(args) > 1:
stream = args[1]
elif isinstance(args[0], tuple):
arrays = args[0]
else:
stream = args[0]

if filename:
self.properties.storage = RT_Disk
self.properties.filename = filename
Expand Down
17 changes: 10 additions & 7 deletions tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ def test_unicode_filenames(self) -> None:
"""Unicode filenames work as expected"""
tname = tempfile.mktemp()
filename = tname + "gilename\u4500abc"
idx = index.Index(filename)
idx = index.Index(filename=filename)
idx.insert(
4321, (34.3776829412, 26.7375853734, 49.3776829412, 41.7375853734), obj=42
)
Expand Down Expand Up @@ -431,7 +431,7 @@ def test_custom_filenames(self) -> None:
p.dat_extension = "data"
p.idx_extension = "index"
tname = tempfile.mktemp()
idx = index.Index(tname, properties=p)
idx = index.Index(filename=tname, properties=p)
for i, coords in enumerate(self.boxes15):
idx.add(i, coords)

Expand All @@ -441,7 +441,7 @@ def test_custom_filenames(self) -> None:
del idx

# Check we can reopen the index and get the same results
idx2 = index.Index(tname, properties=p)
idx2 = index.Index(filename=tname, properties=p)
hits = list(idx2.intersection((0, 0, 60, 60)))
self.assertEqual(len(hits), 10)
self.assertEqual(hits, [0, 4, 16, 27, 35, 40, 47, 50, 76, 80])
Expand All @@ -462,7 +462,10 @@ def data_gen(
p = index.Property()
tname = tempfile.mktemp()
idx = index.Index(
tname, data_gen(interleaved=False), properties=p, interleaved=False
filename=tname,
stream=data_gen(interleaved=False),
properties=p,
interleaved=False,
)
hits1 = sorted(list(idx.intersection((0, 60, 0, 60))))
self.assertEqual(len(hits1), 10)
Expand Down Expand Up @@ -598,9 +601,9 @@ def test_overwrite(self) -> None:
"""Index overwrite works as expected"""
tname = tempfile.mktemp()

idx = index.Index(tname)
idx = index.Index(filename=tname)
del idx
idx = index.Index(tname, properties=index.Property(overwrite=True))
idx = index.Index(filename=tname, properties=index.Property(overwrite=True))
assert isinstance(idx, index.Index)


Expand Down Expand Up @@ -744,7 +747,7 @@ def create_index() -> index.Index:
def gen() -> None:
raise TestException("raising here")

return index.Index(gen()) # type: ignore[func-returns-value]
return index.Index(stream=gen()) # type: ignore[func-returns-value]

self.assertRaises(TestException, create_index)

Expand Down

0 comments on commit 9dd17cf

Please sign in to comment.