Skip to content

Commit

Permalink
Make ItemAdapter.ADAPTER_CLASSES an Iterable instead of a deque
Browse files Browse the repository at this point in the history
  • Loading branch information
elacuesta committed Jul 5, 2023
1 parent bff7a0c commit 9b4a044
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,12 @@ interface, providing a `dict`-like API to manipulate data for the object it wrap

**Attributes**

#### class attribute `ADAPTER_CLASSES: collections.deque`
#### class attribute `ADAPTER_CLASSES: Iterable`

Stores the currently registered adapter classes. Being a
[`collections.deque`](https://docs.python.org/3/library/collections.html#collections.deque),
it supports efficient addition/deletion of adapters classes to both ends.
Stores the currently registered adapter classes. The default implementaion uses a
[`collections.deque`](https://docs.python.org/3/library/collections.html#collections.deque)
to support efficient addition/deletion of adapters classes to both ends, but any other iterable
(e.g. `list`, `tuple`) will work.

The order in which the adapters are registered is important. When an `ItemAdapter` object is
created for a specific item, the registered adapters are traversed in order and the first
Expand Down Expand Up @@ -383,7 +384,6 @@ attribute as needed:

**Example**
```python
>>> from collections import deque
>>> from itemadapter.adapter import (
... ItemAdapter,
... AttrsAdapter,
Expand All @@ -395,10 +395,10 @@ attribute as needed:
>>> from scrapy.item import Item, Field
>>>
>>> class BuiltinTypesItemAdapter(ItemAdapter):
... ADAPTER_CLASSES = deque([DictAdapter, DataclassAdapter])
... ADAPTER_CLASSES = [DictAdapter, DataclassAdapter]
...
>>> class ThirdPartyTypesItemAdapter(ItemAdapter):
... ADAPTER_CLASSES = deque([AttrsAdapter, PydanticAdapter, ScrapyItemAdapter])
... ADAPTER_CLASSES = [AttrsAdapter, PydanticAdapter, ScrapyItemAdapter]
...
>>> class ScrapyItem(Item):
... foo = Field()
Expand Down
4 changes: 2 additions & 2 deletions itemadapter/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from collections import deque
from collections.abc import KeysView, MutableMapping
from types import MappingProxyType
from typing import Any, Deque, Iterator, Type, Optional, List
from typing import Any, Iterable, Iterator, Type, Optional, List

from itemadapter.utils import (
_get_pydantic_model_metadata,
Expand Down Expand Up @@ -271,7 +271,7 @@ class ItemAdapter(MutableMapping):
to extract and set data without having to take the object's type into account.
"""

ADAPTER_CLASSES: Deque[Type[AdapterInterface]] = deque(
ADAPTER_CLASSES: Iterable[Type[AdapterInterface]] = deque(
[
ScrapyItemAdapter,
DictAdapter,
Expand Down
3 changes: 1 addition & 2 deletions tests/test_itemadapter.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import unittest
from collections import deque

from itemadapter.adapter import ItemAdapter, DictAdapter


class DictOnlyItemAdapter(ItemAdapter):
ADAPTER_CLASSES = deque([DictAdapter])
ADAPTER_CLASSES = [DictAdapter]


class ItemAdapterTestCase(unittest.TestCase):
Expand Down

0 comments on commit 9b4a044

Please sign in to comment.