The ItemAdapter class is a wrapper for data container objects, providing a
common interface to handle objects of different types in an uniform manner,
regardless of their underlying implementation.
Additionally, interaction with arbitrary types is supported, by implementing
a pre-defined interface (see extending itemadapter).
Requirements
Python 3.11+, either the CPython implementation (default) or the PyPy
implementation
scrapy 2.7+: optional, needed to interact with
scrapy items
attrs 22.1.0+: optional, needed to
interact with attrs-based items
pydantic 2.7+: optional, needed to
interact with -based items
pydantic
Installation
itemadapter is available on PyPI, it can be installed with pip:
pip install itemadapter
For attrs, pydantic and scrapy support, install the corresponding extra
to ensure that a supported version of the corresponding dependencies is
installed. For example:
pip install itemadapter[scrapy]
Mind that you can install multiple extras as needed. For example:
This is the main entrypoint for the package. Tipically, user code
wraps an item using this class, and proceeds to handle it with the provided interface.
ItemAdapter implements the
MutableMapping
interface, providing a dict-like API to manipulate data for the object it wraps
(which is modified in-place).
Attributes
class attribute ADAPTER_CLASSES: Iterable
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 class method is used for all subsequent
operations. The default order is the one defined in the
built-in adapters section.
The default implementation is a tuple, but any other iterable (e.g. list,
collections.deque)
will work.
Which adapter class handles a given item class is cached, so is_item_class must
return the same result every time it is called with the same item class.
Methods
class method is_item(item: Any) -> bool
Return True if any of the registered adapters can handle the class of the item
(i.e. if any of them returns True for its is_item_class method with
item.__class__ as argument), False otherwise.
class method is_item_class(item_class: type) -> bool
Return True if any of the registered adapters can handle the item class
(i.e. if any of them returns True for its is_item_class method with
item_class as argument), False otherwise.
class method get_field_meta_from_class(item_class: type, field_name: str) -> MappingProxyType
Return a types.MappingProxyType
object, which is a read-only mapping with metadata about the given field. If the item class does not
support field metadata, or there is no metadata for the given field, an empty object is returned.
The returned value is taken from the following sources, depending on the item type:
the first mapping in the
typing.Annotated
metadata of the field type hint for TypedDict-based items
class method get_field_names_from_class(item_class: type) -> Optional[list[str]]
Return a list with the names of all the fields defined for the item class.
If an item class doesn't support defining fields upfront, None is returned.
class method get_json_schema(item_class: type) -> dict[str, Any]
Return a dict with a JSON Schema representation of
the item class.
The generated JSON Schema reflects field type hints, attribute docstrings and
class and field metadata of any supported item class. It also supports using
item classes in field types of other item classes.
For example, given:
from dataclasses import dataclass
import attrs
@dataclass
class Brand:
name: str
@attrs.define
class Product:
name: str
"""Product name"""
brand: Brand | None
in_stock: bool = True
Note that, for Pydantic items, itemadapter does not use
model_json_schema()
and instead uses its own implementation. That way, the output JSON Schema is
consistent across different item types. It also makes it possible to generate
JSON Schemas for Pydantic models that have nested non-Pydantic item classes as
fields. The downside is that JSON Schema support in itemadapter may not be as
advanced as Pydantic‘s.
The following are some known limitations of JSON Schema generation in
itemadapter:
Attribute docstrings are read with
inspect.getsource(),
and may not be readable at run time in some cases. For such cases, define
description within json_schema_extra instead (see below).
String pattern contraints are translated to the regular expression dialect
of JSON Schema where possible, and silently ignored where not, e.g. when
they are case-insensitive or multiline.
Recursion is silently ignored: if you have an item class that has an
attribute with that same item class as a type or as part of its type, a
simple {"type": "object"} is used to map the nested instances of that
item class.
Return metadata for the given field, if available. Unless overriden in a custom adapter class, by default
this method calls the adapter's get_field_meta_from_class method, passing the wrapped item's class.
field_names() -> collections.abc.KeysView
Return a keys view
with the names of all the defined fields for the item.
asdict() -> dict
Return a dict object with the contents of the adapter. This works slightly different than
calling dict(adapter), because it's applied recursively to nested items (if there are any).
function itemadapter.utils.is_item(obj: Any) -> bool
Return True if the given object belongs to (at least) one of the supported types,
False otherwise. This is an alias, using the itemadapter.adapter.ItemAdapter.is_item
class method is encouraged for better performance.
function itemadapter.utils.get_field_meta_from_class(item_class: type, field_name: str) -> types.MappingProxyType
Alias for itemadapter.adapter.ItemAdapter.get_field_meta_from_class
Metadata support
scrapy.item.Item, dataclass, attrs, pydantic and TypedDict objects
allow the definition of arbitrary field metadata. This can be accessed through a
MappingProxyType
object, which can be retrieved from an item instance with
itemadapter.adapter.ItemAdapter.get_field_meta, or from an item class
with the itemadapter.adapter.ItemAdapter.get_field_meta_from_class
method (or its alias itemadapter.utils.get_field_meta_from_class).
The source of the data depends on the underlying type (see the docs for
ItemAdapter.get_field_meta_from_class).
scrapy.item.Item objects
>>> from scrapy.item import Item, Field
>>> from itemadapter import ItemAdapter
>>> class InventoryItem(Item):
... name = Field(serializer=str)
... value = Field(serializer=int, limit=100)
...
>>> adapter = ItemAdapter(InventoryItem(name="foo", value=10))
>>> adapter.get_field_meta("name")
mappingproxy({'serializer': <class 'str'>})
>>> adapter.get_field_meta("value")
mappingproxy({'serializer': <class 'int'>, 'limit': 100})
>>>
TypedDict instances are plain dictionaries at run time, with no reference to
the TypedDict subclass they were declared as, so metadata can only be read
from the item class:
This package allows to handle arbitrary item classes, by implementing an adapter interface:
class itemadapter.adapter.AdapterInterface(item: Any)
Abstract Base Class for adapters. An adapter that handles a specific type of item must
inherit from this class and implement the abstract methods defined on it. AdapterInterface
inherits from collections.abc.MutableMapping,
so all methods from the MutableMapping interface must be implemented as well.
class method is_item_class(cls, item_class: type) -> bool
Return True if the adapter can handle the given item class, False otherwise. Abstract (mandatory).
class method is_item(cls, item: Any) -> bool
Return True if the adapter can handle the given item, False otherwise.
The default implementation calls cls.is_item_class(item.__class__).
class method get_field_meta_from_class(cls, item_class: type) -> bool
Return metadata for the given item class and field name, if available.
By default, this method returns an empty MappingProxyType object. Please supply your
own method definition if you want to handle field metadata based on custom logic.
See the section on metadata support for additional information.
Return metadata for the given field name, if available. It's usually not necessary to
override this method, since the itemadapter.adapter.AdapterInterface base class
provides a default implementation that calls ItemAdapter.get_field_meta_from_class
with the wrapped item's class as argument.
See the section on metadata support for additional information.
Return a dynamic view
of the item's field names. By default, this method returns the result of calling keys() on
the current adapter, i.e., its return value depends on the implementation of the methods from the
MutableMapping interface (more specifically, it depends on the return value of __iter__).
You might want to override this method if you want a way to get all fields for an item, whether or not
they are populated. For instance, Scrapy uses this method to define column names when exporting items to CSV.
Registering an adapter
Add your custom adapter class to the
itemadapter.adapter.ItemAdapter.ADAPTER_CLASSES class attribute in order to
handle custom item classes.
If you need to have different handlers and/or priorities for different cases
you can subclass the ItemAdapter class and set the ADAPTER_CLASSES
attribute as needed: