Skip to content

Commit

Permalink
Merge pull request #74 from scrapy/remove-deque-type-hint
Browse files Browse the repository at this point in the history
Make ItemAdapter.ADAPTER_CLASSES an Iterable instead of a deque
  • Loading branch information
kmike authored Jul 6, 2023
2 parents ca28ce6 + b30b6a9 commit 0b65bf3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,19 +152,21 @@ 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 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
adapter class to return `True` for the `is_item` class method is used for all subsequent
operations. The default order is the one defined in the
[built-in adapters](#built-in-adapters) section.

See the section on [extending itemadapter](#extending-itemadapter) for additional information.
The default implementation 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 if you are
deriving a subclass (see the section on [extending itemadapter](#extending-itemadapter)
for additional information), any other iterable (e.g. `list`, `tuple`) will work.

**Methods**

Expand Down Expand Up @@ -384,7 +386,6 @@ attribute as needed:

**Example**
```python
>>> from collections import deque
>>> from itemadapter.adapter import (
... ItemAdapter,
... AttrsAdapter,
Expand All @@ -396,10 +397,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 0b65bf3

Please sign in to comment.