Skip to content

refactor: simplify da type checking#224

Merged
bwanglzu merged 3 commits into
mainfrom
refactor-typechecking
Mar 24, 2022
Merged

refactor: simplify da type checking#224
bwanglzu merged 3 commits into
mainfrom
refactor-typechecking

Conversation

@bwanglzu

@bwanglzu bwanglzu commented Mar 23, 2022

Copy link
Copy Markdown

DocumentArray is MutableSequence -> Sequence -> Iterable.

iter(da) is Generator -> Iterator -> Iterable.

chain(da1, da2) is both Iterable and Iterator.

Maybe we only need to check 1 type

from docarray import DocumentArray, Document

from typing import Generator, Iterator, Sequence, Iterable

da = DocumentArray([Document(), Document(), Document()])

print(isinstance(da, DocumentArray))
print(isinstance(da, Sequence))
print(isinstance(da, Iterable))


my_iter = iter(da)

print(isinstance(my_iter, Iterator))
print(isinstance(my_iter, Generator))

from itertools import chain

da1 = DocumentArray([Document()])
da2 = DocumentArray([Document(), Document()])

# chaining da1 and da2 numbers
da = chain(da1, da2)
print(isinstance(da, Iterator))
print(isinstance(da, Iterable))
print(type(da))

@codecov

codecov Bot commented Mar 23, 2022

Copy link
Copy Markdown

Codecov Report

Merging #224 (0a89610) into main (5a8544a) will increase coverage by 4.09%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main     #224      +/-   ##
==========================================
+ Coverage   81.18%   85.28%   +4.09%     
==========================================
  Files         123      123              
  Lines        5677     5667      -10     
==========================================
+ Hits         4609     4833     +224     
+ Misses       1068      834     -234     
Flag Coverage Δ
docarray 85.28% <100.00%> (+4.09%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
docarray/array/storage/memory/backend.py 95.23% <ø> (-0.22%) ⬇️
docarray/array/storage/memory/getsetdel.py 100.00% <ø> (ø)
docarray/array/storage/weaviate/getsetdel.py 100.00% <ø> (ø)
docarray/array/chunk.py 88.00% <100.00%> (-0.89%) ⬇️
docarray/array/match.py 86.95% <100.00%> (-1.05%) ⬇️
docarray/array/storage/annlite/backend.py 95.00% <100.00%> (-0.09%) ⬇️
docarray/array/storage/qdrant/backend.py 96.73% <100.00%> (-0.04%) ⬇️
docarray/array/storage/sqlite/backend.py 92.20% <100.00%> (-0.10%) ⬇️
docarray/array/storage/weaviate/backend.py 87.17% <100.00%> (-0.22%) ⬇️
docarray/document/mixins/_property.py 85.80% <0.00%> (+4.51%) ⬆️
... and 7 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5a8544a...0a89610. Read the comment docs.

@hanxiao

hanxiao commented Mar 24, 2022

Copy link
Copy Markdown
Member

@alaeddine-13 can you review it

@hanxiao hanxiao requested a review from alaeddine-13 March 24, 2022 06:33

@alaeddine-13 alaeddine-13 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's clearer to just check for Iterable

Comment thread docarray/array/chunk.py Outdated
)
and self._ref_doc is not None
):
if isinstance(docs, (Iterable, Iterator)) and self._ref_doc is not None:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Iterator is also a subclass of Iterable so we can just keep Iterable here

Suggested change
if isinstance(docs, (Iterable, Iterator)) and self._ref_doc is not None:
if isinstance(docs, Iterable) and self._ref_doc is not None:

Comment thread docarray/array/match.py Outdated
)
and self._ref_doc is not None
):
if isinstance(docs, (Iterable, Iterator)) and self._ref_doc is not None:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if isinstance(docs, (Iterable, Iterator)) and self._ref_doc is not None:
if isinstance(docs, Iterable) and self._ref_doc is not None:

if isinstance(
_docs, (DocumentArray, Sequence, Generator, Iterator, itertools.chain)
):
if isinstance(_docs, (Iterable, Iterator)):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if isinstance(_docs, (Iterable, Iterator)):
if isinstance(_docs, Iterable):

elif isinstance(
_docs,
(DocumentArray, Sequence, Generator, Iterator, itertools.chain),
(Iterable, Iterator),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just Iterable

if isinstance(
docs, (DocumentArray, Sequence, Generator, Iterator, itertools.chain)
):
if isinstance(docs, (Iterable, Iterator)):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if isinstance(docs, (Iterable, Iterator)):
if isinstance(docs, Iterable):

elif isinstance(
_docs, (DocumentArray, Sequence, Generator, Iterator, itertools.chain)
):
elif isinstance(_docs, (Iterable, Iterator)):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
elif isinstance(_docs, (Iterable, Iterator)):
elif isinstance(_docs, Iterable):

elif isinstance(
_docs, (DocumentArray, Sequence, Generator, Iterator, itertools.chain)
):
elif isinstance(_docs, (Iterable, Iterator)):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
elif isinstance(_docs, (Iterable, Iterator)):
elif isinstance(_docs, Iterable):

@alaeddine-13 alaeddine-13 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@bwanglzu bwanglzu merged commit c19f057 into main Mar 24, 2022
@bwanglzu bwanglzu deleted the refactor-typechecking branch March 24, 2022 10:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants