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

Make ItemAdapter.ADAPTER_CLASSES an Iterable instead of a deque #74

Merged
merged 5 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Currently supported types are:
* [`dict`](https://docs.python.org/3/library/stdtypes.html#dict)
* [`dataclass`](https://docs.python.org/3/library/dataclasses.html)-based classes
* [`attrs`](https://www.attrs.org)-based classes
* [`pydantic`](https://pydantic-docs.helpmanual.io/)-based classes
* [`pydantic`](https://pydantic-docs.helpmanual.io/)-based classes (`pydantic>=2` not yet supported)

Additionally, interaction with arbitrary types is supported, by implementing
a pre-defined interface (see [extending `itemadapter`](#extending-itemadapter)).
Expand All @@ -27,7 +27,8 @@ a pre-defined interface (see [extending `itemadapter`](#extending-itemadapter)).
* Python 3.7+
* [`scrapy`](https://scrapy.org/): optional, needed to interact with `scrapy` items
* [`attrs`](https://pypi.org/project/attrs/): optional, needed to interact with `attrs`-based items
* [`pydantic`](https://pypi.org/project/pydantic/): optional, needed to interact with `pydantic`-based items
* [`pydantic`](https://pypi.org/project/pydantic/): optional, needed to interact with
`pydantic`-based items (`pydantic>=2` not yet supported)

---

Expand Down Expand Up @@ -151,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 @@ -383,7 +386,6 @@ attribute as needed:

**Example**
```python
>>> from collections import deque
>>> from itemadapter.adapter import (
... ItemAdapter,
... AttrsAdapter,
Expand All @@ -395,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
2 changes: 1 addition & 1 deletion tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
attrs
pydantic
pydantic<2
pytest-cov>=2.8
pytest>=5.4
scrapy>=2.0
2 changes: 1 addition & 1 deletion tests/test_adapter_pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
)


class DataclassTestCase(unittest.TestCase):
class PydanticTestCase(unittest.TestCase):
def test_false(self):
from itemadapter.adapter import PydanticAdapter

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