Skip to content

Commit

Permalink
Provide complete API documentation coverage of scrapy.item
Browse files Browse the repository at this point in the history
  • Loading branch information
Gallaecio committed Sep 16, 2019
1 parent d4b8bf1 commit 2239c6a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
3 changes: 3 additions & 0 deletions docs/conf.py
Expand Up @@ -262,6 +262,9 @@
# details that are not documented.
r'^scrapy\.extensions\.[a-z]\w*?\.[A-Z]\w*?\.', # methods
r'^scrapy\.extensions\.[a-z]\w*?\.[a-z]', # helper functions

# Never documented before, and deprecated now.
r'^scrapy\.item\.DictItem$',
]


Expand Down
6 changes: 6 additions & 0 deletions docs/topics/items.rst
Expand Up @@ -263,3 +263,9 @@ Field objects
.. _dict: https://docs.python.org/2/library/stdtypes.html#dict


Other classes related to Item
=============================

.. autoclass:: BaseItem

.. autoclass:: ItemMeta
31 changes: 28 additions & 3 deletions scrapy/item.py
Expand Up @@ -4,13 +4,15 @@
See documentation in docs/topics/item.rst
"""

import collections
from abc import ABCMeta
from pprint import pformat
from copy import deepcopy
import collections
from pprint import pformat
from warnings import warn

import six

from scrapy.utils.deprecate import ScrapyDeprecationWarning
from scrapy.utils.trackref import object_ref


Expand All @@ -21,7 +23,19 @@


class BaseItem(object_ref):
"""Base class for all scraped items."""
"""Base class for all scraped items.
In Scrapy, an object is considered an *item* if it is an instance of either
:class:`BaseItem` or :class:`dict`. For example, when the output of a
spider callback is evaluated, only instances of :class:`BaseItem` or
:class:`dict` are passed to :ref:`item pipelines <topics-item-pipeline>`.
If you need instances of a custom class to be considered items by Scrapy,
you must inherit from either :class:`BaseItem` or :class:`dict`.
Unlike instances of :class:`dict`, instances of :class:`BaseItem` may be
:ref:`tracked <topics-leaks-trackrefs>` to debug memory leaks.
"""
pass


Expand All @@ -30,6 +44,10 @@ class Field(dict):


class ItemMeta(ABCMeta):
"""Metaclass_ of :class:`Item` that handles field definitions.
.. _metaclass: https://realpython.com/python-metaclasses
"""

def __new__(mcs, class_name, bases, attrs):
classcell = attrs.pop('__classcell__', None)
Expand All @@ -56,6 +74,13 @@ class DictItem(MutableMapping, BaseItem):

fields = {}

def __new__(cls, *args, **kwargs):
if cls is DictItem:
warn('scrapy.item.DictItem is deprecated, please use '
'scrapy.item.Item instead',
ScrapyDeprecationWarning, stacklevel=2)
return super(DictItem, cls).__new__(cls, *args, **kwargs)

def __init__(self, *args, **kwargs):
self._values = {}
if args or kwargs: # avoid creating dict for most common case
Expand Down

0 comments on commit 2239c6a

Please sign in to comment.