Skip to content

Commit

Permalink
Add docs for DynamicDeps.
Browse files Browse the repository at this point in the history
  • Loading branch information
wRAR committed Jun 25, 2024
1 parent fff751c commit a9dba26
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
54 changes: 54 additions & 0 deletions docs/dynamic-deps.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
.. _dynamic-deps:

====================
Dynamic dependencies
====================

Normally the dependencies for a callback are specified statically, as type
hints for its arguments:

.. code-block:: python
import scrapy
class BooksSpider(scrapy.Spider):
...
def start_requests(self):
yield scrapy.Request("http://books.toscrape.com/", self.parse_book)
def parse_book(self, response, book_page: BookPage, other_dep: OtherDep):
...
In some cases some or all of the dependencies need to be specified dynamically
instead, e.g. because they need to be different for different pages using the
same callback. You can use :class:`scrapy_poet.DynamicDeps
<scrapy_poet.injection.DynamicDeps>` for this. If you add a callback argument
with this type you can pass a list of additional dependency types in the
request meta dictionary using the "inject" key:

.. code-block:: python
import scrapy
class BooksSpider(scrapy.Spider):
...
def start_requests(self):
yield scrapy.Request(
"http://books.toscrape.com/",
self.parse_book,
meta={"inject": [OtherDep]},
)
def parse_book(self, response, book_page: BookPage, dynamic: DynamicDeps):
other_dep = dynamic[OtherDep]
...
The types passed this way will be used in the dependency resolution as usual,
and the created instances will be available in the
:class:`scrapy_poet.DynamicDeps <scrapy_poet.injection.DynamicDeps>` instance.
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ To get started, see :ref:`intro-install` and :ref:`intro-tutorial`.
:maxdepth: 1

rules-from-web-poet
dynamic-deps
stats
providers
testing
Expand Down
6 changes: 6 additions & 0 deletions scrapy_poet/injection.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ class _UNDEFINED:


class DynamicDeps(UserDict):
"""A container for dynamic dependencies provided via the ``"inject"`` request meta key.
The dynamic dependency instances are available at the run time as dict
values with keys being dependency types.
"""

pass


Expand Down

0 comments on commit a9dba26

Please sign in to comment.