1
1
mirror of https://git.launchpad.net/beautifulsoup synced 2025-10-06 00:12:49 +02:00

Issue a warning if given a parse_only filter that's going to exclude everything.

This commit is contained in:
Leonard Richardson
2023-04-11 16:08:05 -04:00
parent 7044f2adda
commit db78010ffc
2 changed files with 16 additions and 1 deletions

View File

@@ -220,7 +220,14 @@ class BeautifulSoup(Tag):
parse_only = parse_only or deprecated_argument(
"parseOnlyThese", "parse_only")
if (parse_only is not None
and parse_only.string_rules and
(parse_only.name_rules or parse_only.attribute_rules)):
warnings.warn(
f"Value for parse_only will exclude everything, since it puts restrictions on both tags and strings: {parse_only}",
UserWarning, stacklevel=3
)
from_encoding = from_encoding or deprecated_argument(
"fromEncoding", "from_encoding")

View File

@@ -287,6 +287,14 @@ class TestWarnings(SoupTest):
soup = self.soup("<a><b></b></a>")
assert [] == w
def test_warning_if_strainer_filters_everything(self):
strainer = SoupStrainer(name="a", string="b")
with warnings.catch_warnings(record=True) as w:
soup = self.soup("<a><b></b></a>", parse_only=strainer)
warning = self._assert_warning(w, UserWarning)
msg = str(warning.message)
assert msg.startswith("Value for parse_only will exclude everything, since it puts restrictions on both tags and strings:")
def test_parseOnlyThese_renamed_to_parse_only(self):
with warnings.catch_warnings(record=True) as w:
soup = BeautifulSoup(