Skip to content

Commit 10dff5b

Browse files
committed
fix bug where module substring paths are not filtered out correctly
1 parent c000cbc commit 10dff5b

File tree

4 files changed

+18
-11
lines changed

4 files changed

+18
-11
lines changed

tests/po_lib/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from url_matcher import Patterns
77

8-
from .. import po_sub_lib # NOTE: this module contains a PO with @handle_rules
8+
from .. import po_lib_sub # NOTE: this module contains a PO with @handle_rules
99
from web_poet import handle_urls, PageObjectRegistry
1010

1111

tests/po_sub_lib/__init__.py renamed to tests/po_lib_sub/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ class POBase:
1414
expected_meta: Dict[str, Any]
1515

1616

17-
class POSubLibOverriden:
17+
class POLibSubOverriden:
1818
...
1919

2020

21-
@handle_urls("sub_example.com", POSubLibOverriden)
22-
class POSubLib(POBase):
23-
expected_overrides = POSubLibOverriden
21+
@handle_urls("sub_example.com", POLibSubOverriden)
22+
class POLibSub(POBase):
23+
expected_overrides = POLibSubOverriden
2424
expected_patterns = Patterns(["sub_example.com"])
2525
expected_meta = {} # type: ignore

tests/test_overrides.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22
from url_matcher import Patterns
33

4-
from tests.po_sub_lib import POSubLib
4+
from tests.po_lib_sub import POLibSub
55
from tests.po_lib import POTopLevel1, POTopLevel2, POTopLevelOverriden2, secondary_registry
66
from tests.po_lib.a_module import POModule
77
from tests.po_lib.nested_package import PONestedPkg
@@ -22,7 +22,7 @@ def test_list_page_objects_all():
2222

2323
# Ensure that ALL Override Rules are returned as long as the given
2424
# registry's @handle_urls annotation was used.
25-
assert page_objects == POS.union({POSubLib})
25+
assert page_objects == POS.union({POLibSub})
2626
for rule in rules:
2727
assert rule.instead_of == rule.use.expected_overrides, rule.use
2828
assert rule.for_patterns == rule.use.expected_patterns, rule.use
@@ -35,9 +35,9 @@ def test_list_page_objects_from_pkg():
3535
page_objects = {po.use for po in rules}
3636

3737
# Ensure that the "tests.po_lib", which imports another module named
38-
# "tests.po_sub_lib" which contains @handle_urls decorators, does not
38+
# "tests.po_lib_sub" which contains @handle_urls decorators, does not
3939
# retrieve the override rules from the external package.
40-
assert POSubLib not in page_objects
40+
assert POLibSub not in page_objects
4141

4242
assert page_objects == POS
4343
for rule in rules:

web_poet/overrides.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,19 @@ def _filter_from_module(self, module: str) -> Dict[Callable, OverrideRule]:
174174
return {
175175
cls: rule
176176
for cls, rule in self.data.items()
177-
if cls.__module__.startswith(module)
177+
178+
# A "." is added at the end to prevent incorrect matching on cases
179+
# where package names are substrings of one another. For example,
180+
# if module = "my_project.po_lib", then it filters like so:
181+
# - "my_project.po_lib_sub.POLibSub" (filtered out)
182+
# - "my_project.po_lib.POTopLevel1" (accepted)
183+
# - "my_project.po_lib.nested_package.PONestedPkg" (accepted)
184+
if cls.__module__.startswith(module + ".") or cls.__module__ == module
178185
}
179186

180187

181188
# For ease of use, we'll create a default registry so that users can simply
182-
# use its `handles_url()` method directly by `from web_poet import handles_url`
189+
# use its `handle_urls()` method directly by `from web_poet import handle_urls`
183190
default_registry = PageObjectRegistry()
184191
handle_urls = default_registry.handle_urls
185192

0 commit comments

Comments
 (0)