scrapy / itemadapter

Common interface for data container classes
BSD 3-Clause "New" or "Revised" License
62 stars 13 forks source link

ItemAdapter.asdict (recursive dict conversion) #29

Closed elacuesta closed 4 years ago

elacuesta commented 4 years ago

Closes #27

I decided not to use attr.asdict or dataclasses.asdict because they only handle nested objects of their own kind:

In [1]: import dataclasses
   ...: import attr 
   ...:  
   ...: @dataclasses.dataclass 
   ...: class Price: 
   ...:     value: int 
   ...:     currency: str 
   ...:  
   ...: @attr.s(auto_attribs=True) 
   ...: class Product: 
   ...:     name: str 
   ...:     price: Price  
   ...:

In [2]: attr.asdict(Product("Stuff", Price(50, "UYU")))
Out[2]: {'name': 'Stuff', 'price': Price(value=50, currency='UYU')}

In [3]: import dataclasses 
   ...: import attr 
   ...:  
   ...: @attr.s(auto_attribs=True) 
   ...: class Price: 
   ...:     value: int 
   ...:     currency: str 
   ...:  
   ...: @dataclasses.dataclass 
   ...: class Product: 
   ...:     name: str 
   ...:     price: Price  
   ...:

In [4]: dataclasses.asdict(Product("Stuff", Price(50, "UYU")))
Out[4]: {'name': 'Stuff', 'price': Price(value=50, currency='UYU')}
codecov[bot] commented 4 years ago

Codecov Report

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

Impacted file tree graph

@@            Coverage Diff            @@
##            master       #29   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            2         2           
  Lines           98       110   +12     
=========================================
+ Hits            98       110   +12     
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 aa2c8ae...4d15f55. Read the comment docs.