Skip to content

Commit e256b96

Browse files
committed
simplify and make the POP doc clearer
1 parent 8e805a7 commit e256b96

File tree

2 files changed

+33
-44
lines changed

2 files changed

+33
-44
lines changed

docs/intro/overrides.rst

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -213,22 +213,6 @@ This can be done something like:
213213
duration. Calling :func:`~.web_poet.overrides.consume_modules` again makes
214214
no difference unless a new set of modules are provided.
215215

216-
.. tip::
217-
218-
If you're using External Packages which conform to the **POP**
219-
standards as described in the :ref:`intro-pop` section, then retrieving
220-
the rules could also be done as:
221-
222-
.. code-block:: python
223-
224-
import ecommerce_page_objects, gadget_sites_page_objects
225-
226-
# If on Python 3.9+
227-
rules = ecommerce_page_objects.REGISTRY | gadget_sites_page_objects.REGISTRY
228-
229-
# If on lower Python versions
230-
rules = {**ecommerce_page_objects.REGISTRY, **gadget_sites_page_objects.REGISTRY}
231-
232216
.. _`intro-rule-subset`:
233217

234218
Using only a subset of the available OverrideRules

docs/intro/pop.rst

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -123,57 +123,62 @@ Recommended Requirements
123123

124124
This covers these use cases:
125125

126-
- The minimum requirements and its use cases
126+
- The `minimum requirements` and its use cases
127127
- The ability to retrieve the declared :class:`~.OverrideRule`
128-
inside the **POP**
128+
available inside the **POP**
129129

130-
This means that a collection of :class:`~.OverrideRule` must be explicitly
131-
declared in the **POP**. This enables projects using the **POP** to know:
130+
This means that a collection of :class:`~.OverrideRule` must be properly
131+
discovered within the **POP**. This enables projects using the **POP** to know:
132132

133133
- which URL Patterns a given Page Object is expected to work
134134
- what it's trying to override `(or replace)`
135135

136-
This could be done by declaring a ``REGISTRY`` variable that can be
137-
imported as a top-level variable from the package.
138-
139-
For example, suppose our project is named **ecommerce_page_objects**
136+
To give an example, suppose our **POP** is named **ecommerce_page_objects**
140137
and is using any of the project structure options discussed in the
141-
previous sections, we can then define the ``REGISTRY`` variable as the following
142-
inside of ``ecommerce-page-objects/ecommerce_page_objects/__init__.py``:
138+
previous sections. We can then define the entry point of discovering
139+
all :class:`~.OverrideRule` by writing the following code inside of
140+
``ecommerce-page-objects/ecommerce_page_objects/__init__.py``:
143141

144142
.. code-block:: python
145143
146-
from web_poet import default_registry, consume_modules
144+
from web_poet import consume_modules
147145
148146
# This allows all of the OverrideRules declared inside the package
149147
# using @handle_urls to be properly discovered and loaded.
150148
consume_modules(__package__)
151149
152-
REGISTRY = default_registry
150+
.. note::
151+
152+
Remember, code in Python like annotations are only read and executed
153+
when the module it belongs to is imported. Thus, in order for all the
154+
``@handle_urls`` annotation to properly reflect its data, they need to
155+
be imported recursively via :func:`~.consume_modules`.
156+
157+
This allows developers to properly access all of the :class:`~.OverrideRule`
158+
declared using the ``@handle_urls`` annotation inside the **POP**. In turn,
159+
this also allows **POPs** which use ``web_poet.default_registry`` to have all
160+
their rules discovered if they are adhering to using Convention **#3**
161+
(see :ref:`best-practices`).
153162

154-
This allows any developer using a **POP** to easily access all of the
155-
:class:`~.OverrideRule` using the convention of accessing it via the
156-
``REGISTRY`` variable. For example:
163+
In other words, importing the ``ecommerce_page_objects`` **POP** to a
164+
project immediately loads all of the rules in **web-poet's**
165+
``default_registry``:
157166

158167
.. code-block:: python
159168
160-
from ecommerce_page_objects import REGISTRY
169+
from web_poet import default_registry
161170
162-
.. tip::
171+
import ecommerce_page_objects
163172
164-
The ``default_registry`` is an instance of :class:`~.PageObjectRegistry`,
165-
which in turn is simply a subclass of a ``dict``. This means that you don't
166-
necessarily have to use an instance of :class:`~.PageObjectRegistry` as long
167-
as it has a ``dict``-like interface.
173+
# All the rules are now available.
174+
rules = default_registry.get_overrides()
168175
169-
The :class:`~.PageObjectRegistry` is simply a mapping where the **key** is
170-
the Page Object to use and the **value** is the :class:`~.OverrideRule` it
171-
operates on. This means you can simply use a plain ``dict`` for the
172-
``REGISTRY`` variable.
176+
If this recommended requirement is followed properly, there's no need to
177+
call ``consume_modules("ecommerce_page_objects")`` before performing the
178+
:meth:`~.PageObjectRegistry.get_overrides`, since all the :class:`~.OverrideRule`
179+
were already discovered upon **POP** importation.
173180

174-
However, it is **recommended** to use the instances of
175-
:class:`~.PageObjectRegistry` to leverage the validation logic for its
176-
contents.
181+
.. _`best-practices`:
177182

178183

179184
Conventions and Best Practices

0 commit comments

Comments
 (0)