Skip to content

Commit

Permalink
create from_apply_rules method in PageObjectRegistry; deprecate from_…
Browse files Browse the repository at this point in the history
…override_rules
  • Loading branch information
BurnzZ committed Oct 3, 2022
1 parent 3ed8415 commit 4cc42c2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Deprecations:
* The ``overrides`` parameter from ``@handle_urls`` is now deprecated.
Use the ``instead_of`` parameter instead.
* ``OverrideRule`` is now deprecated. Use ``ApplyRule`` instead.
* The ``from_override_rules`` method of ``PageObjectRegistry`` is now deprecated.
Use ``from_apply_rules`` instead.

0.5.1 (2022-09-23)
------------------
Expand Down
4 changes: 2 additions & 2 deletions docs/intro/overrides.rst
Original file line number Diff line number Diff line change
Expand Up @@ -253,14 +253,14 @@ using.

After gathering all the pre-selected rules, we can then store it in a new instance
of :class:`~.PageObjectRegistry` in order to separate it from the ``default_registry``
which contains all of the rules. We can use the :meth:`~.PageObjectRegistry.from_override_rules`
which contains all of the rules. We can use the :meth:`~.PageObjectRegistry.from_apply_rules`
for this:

.. code-block:: python
from web_poet import PageObjectRegistry
my_new_registry = PageObjectRegistry.from_override_rules(rules)
my_new_registry = PageObjectRegistry.from_apply_rules(rules)
.. _`intro-improve-po`:
Expand Down
26 changes: 23 additions & 3 deletions tests/test_overrides.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
}


def test_override_rule_uniqueness() -> None:
def test_apply_rule_uniqueness() -> None:
"""The same instance of an ApplyRule with the same attribute values should
have the same hash identity.
"""
Expand Down Expand Up @@ -140,7 +140,7 @@ def test_registry_search_overrides() -> None:
assert len(rules) == 0


def test_from_override_rules() -> None:
def test_from_apply_rules() -> None:
rules = [
ApplyRule(
for_patterns=Patterns(include=["sample.com"]),
Expand All @@ -149,7 +149,27 @@ def test_from_override_rules() -> None:
)
]

registry = PageObjectRegistry.from_override_rules(rules)
registry = PageObjectRegistry.from_apply_rules(rules)

assert registry.get_overrides() == rules
assert default_registry.get_overrides() != rules


def test_from_override_rules_deprecation() -> None:
rules = [
ApplyRule(
for_patterns=Patterns(include=["sample.com"]),
use=POTopLevel1,
instead_of=POTopLevelOverriden2,
)
]

msg = (
"The 'from_override_rules' method is deprecated. "
"Use 'from_apply_rules' instead."
)
with pytest.warns(DeprecationWarning, match=msg):
registry = PageObjectRegistry.from_override_rules(rules)

assert registry.get_overrides() == rules
assert default_registry.get_overrides() != rules
Expand Down
11 changes: 10 additions & 1 deletion web_poet/overrides.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class ExampleComProductPage(WebPage):
"""

@classmethod
def from_override_rules(
def from_apply_rules(
cls: Type[PageObjectRegistryTV], rules: List[ApplyRule]
) -> PageObjectRegistryTV:
"""An alternative constructor for creating a :class:`~.PageObjectRegistry`
Expand All @@ -111,6 +111,15 @@ def from_override_rules(
"""
return cls({rule.use: rule for rule in rules})

@classmethod
def from_override_rules(
cls: Type[PageObjectRegistryTV], rules: List[ApplyRule]
) -> PageObjectRegistryTV:
"""Deprecated. Use :meth:`~.PageObjectRegistry.from_apply_rules` instead."""
msg = "The 'from_override_rules' method is deprecated. Use 'from_apply_rules' instead."
warnings.warn(msg, DeprecationWarning, stacklevel=2)
return cls.from_apply_rules(rules)

def handle_urls(
self,
include: Strings,
Expand Down

0 comments on commit 4cc42c2

Please sign in to comment.