Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid upstream exceptions in ItemAdapter.__repr__ #31

Merged
merged 1 commit into from
Jun 24, 2020
Merged

Conversation

elacuesta
Copy link
Member

@elacuesta elacuesta commented Jun 23, 2020

Related to #30, scrapy/itemloaders#14

Replace the repr call, display the currently set fields.

Keep in mind that this only prevents the upstream AttributeError in ItemAdapter.__repr__: displaying the original wrapped item will still fail if an attribute is missing:

>>> from dataclasses import dataclass
>>> from itemadapter import ItemAdapter
>>> 
>>> @dataclass(init=False)
... class Product:
...     name: str
...     price: float
... 
>>> p = Product()
>>> a = ItemAdapter(p)
>>> a["name"] = "foo"
>>> a  # this works
<ItemAdapter for Product(name='foo')>
>>> p  # but this still breaks
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.7/dataclasses.py", line 357, in wrapper
    result = user_function(self)
  File "<string>", line 2, in __repr__
AttributeError: 'Product' object has no attribute 'price'

/cc @ejulio

@elacuesta elacuesta changed the title Avoid uptream exceptions in ItemAdapter.__repr__ Avoid upstream exceptions in ItemAdapter.__repr__ Jun 23, 2020
@codecov
Copy link

codecov bot commented Jun 23, 2020

Codecov Report

Merging #31 into master will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master       #31   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            2         2           
  Lines          110       111    +1     
=========================================
+ Hits           110       111    +1     
Impacted Files Coverage Δ
itemadapter/adapter.py 100.00% <100.00%> (ø)

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 ec8f783...391fcc6. Read the comment docs.

Copy link
Member

@Gallaecio Gallaecio left a comment

Choose a reason for hiding this comment

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

Cool

@elacuesta elacuesta merged commit 3bc56f2 into master Jun 24, 2020
@elacuesta
Copy link
Member Author

For the record, I will open a separate PR to add a test using @dataclass(init=False)

@elacuesta elacuesta deleted the update-repr branch June 24, 2020 15:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants