Skip to content

fix: attribute error with empty list in dataclass#674

Merged
JoanFM merged 9 commits intomainfrom
fix-attribute-type-non-defied
Oct 25, 2022
Merged

fix: attribute error with empty list in dataclass#674
JoanFM merged 9 commits intomainfrom
fix-attribute-type-non-defied

Conversation

@samsja
Copy link
Copy Markdown
Member

@samsja samsja commented Oct 25, 2022

Context

Fixing issue reported here #672

Passing an empty List as field input of a dataclass will failed in some case whereas an non empty list will work

from docarray import *
from docarray.typing import *

from typing import List

@dataclass()
class A:
    img: List[Text]

Document(A(img = []))
>>> 
Traceback (most recent call last):
  File "/home/sami/.config/JetBrains/PyCharmCE2022.2/scratches/scratch_8.py", line 11, in <module>
    Document(A(img = []))
  File "/home/sami/Documents/workspace/Jina/docarray/docarray/document/__init__.py", line 147, in __init__
    super().__init__(*args, **kwargs)
  File "/home/sami/Documents/workspace/Jina/docarray/docarray/base.py", line 38, in __init__
    self._data = type(self)._from_dataclass(_obj)._data
  File "/home/sami/Documents/workspace/Jina/docarray/docarray/document/mixins/multimodal.py", line 76, in _from_dataclass
    'attribute_type': attribute_type,
UnboundLocalError: local variable 'attribute_type' referenced before assignment

Whereas passing a non empty list will work just fine

Document(A(img = [0]))

What this PR do

Fix this behavior and allow the code above to pass

@samsja samsja marked this pull request as draft October 25, 2022 08:08
@samsja samsja linked an issue Oct 25, 2022 that may be closed by this pull request
@samsja samsja marked this pull request as ready for review October 25, 2022 08:09
@codecov
Copy link
Copy Markdown

codecov bot commented Oct 25, 2022

Codecov Report

Merging #674 (db88e90) into main (058e726) will increase coverage by 0.02%.
The diff coverage is 95.23%.

@@            Coverage Diff             @@
##             main     #674      +/-   ##
==========================================
+ Coverage   88.48%   88.50%   +0.02%     
==========================================
  Files         133      133              
  Lines        6528     6541      +13     
==========================================
+ Hits         5776     5789      +13     
  Misses        752      752              
Flag Coverage Δ
docarray 88.50% <95.23%> (+0.02%) ⬆️

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

Impacted Files Coverage Δ
docarray/document/mixins/multimodal.py 93.96% <94.73%> (+0.63%) ⬆️
docarray/dataclasses/types.py 93.85% <100.00%> (+0.10%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@JoanFM JoanFM merged commit 7ac67b0 into main Oct 25, 2022
@JoanFM JoanFM deleted the fix-attribute-type-non-defied branch October 25, 2022 13:23
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.

Undeclared variable "attribute_type"

3 participants