1
1
mirror of https://git.launchpad.net/beautifulsoup synced 2025-10-05 16:02:46 +02:00

Manually made some changes recommended by ruff check.

This commit is contained in:
Leonard Richardson
2025-01-02 15:43:56 -05:00
parent 7e24c2c3d1
commit 4a7984044d
12 changed files with 23 additions and 35 deletions

View File

@@ -23,10 +23,7 @@ __license__ = "MIT"
__all__ = ["BeautifulSoup"]
from collections import Counter
import os
import re
import sys
import traceback
import warnings
# The very first thing we do is give a useful error if someone is
@@ -77,9 +74,7 @@ from typing import (
List,
Sequence,
Optional,
Tuple,
Type,
TYPE_CHECKING,
Union,
)

View File

@@ -356,7 +356,7 @@ class Element(BeautifulSoupNode):
def appendChild(self, node: "BeautifulSoupNode") -> None:
string_child: Optional[NavigableString] = None
child: PageElement
if type(node.element) == NavigableString:
if type(node.element) is NavigableString:
string_child = child = node.element
else:
child = node.element
@@ -372,7 +372,7 @@ class Element(BeautifulSoupNode):
if (
string_child is not None
and self.element.contents
and type(self.element.contents[-1]) == NavigableString
and type(self.element.contents[-1]) is NavigableString
):
# We are appending a string onto another string.
# TODO This has O(n^2) performance, for input like
@@ -464,13 +464,13 @@ class Element(BeautifulSoupNode):
) -> None:
index = self.element.index(refNode.element)
if (
type(node.element) == NavigableString
type(node.element) is NavigableString
and self.element.contents
and type(self.element.contents[index - 1]) == NavigableString
and type(self.element.contents[index - 1]) is NavigableString
):
# (See comments in appendChild)
old_node = self.element.contents[index - 1]
assert type(old_node) == NavigableString
assert type(old_node) is NavigableString
new_str = self.soup.new_string(old_node + node.element)
old_node.replace_with(new_str)
else:

View File

@@ -1,7 +1,6 @@
# encoding: utf-8
from __future__ import annotations
"""Use the HTMLParser library to parse HTML files that aren't too bad."""
from __future__ import annotations
# Use of this source code is governed by the MIT license.
__license__ = "MIT"
@@ -43,7 +42,6 @@ from bs4.builder import (
STRICT,
)
from bs4.exceptions import ParserRejectedMarkup
if TYPE_CHECKING:

View File

@@ -279,7 +279,8 @@ class EntitySubstitution(object):
def _substitute_html_entity(cls, matchobj: re.Match) -> str:
"""Used with a regular expression to substitute the
appropriate HTML entity for a special character string."""
entity = cls.CHARACTER_TO_HTML_ENTITY.get(matchobj.group(0))
original_entity = matchobj.group(0)
entity = cls.CHARACTER_TO_HTML_ENTITY.get(original_entity)
if entity is None:
return "&%s;" % original_entity
return "&%s;" % entity
@@ -881,7 +882,7 @@ class UnicodeDammit:
else:
if orig in self.MS_CHARS:
substitutions = self.MS_CHARS[orig]
if type(substitutions) == tuple:
if type(substitutions) is tuple:
if self.smart_quotes_to == "xml":
sub = b"&#x" + substitutions[1].encode() + b";"
else:

View File

@@ -997,11 +997,11 @@ class PageElement(object):
# NOTE: We can't use _find_one because findParents takes a different
# set of arguments.
r = None
l = self.find_parents(
results = self.find_parents(
name, attrs, 1, consider_self=consider_self, _stacklevel=3, **kwargs
)
if l:
r = l[0]
if results:
r = results[0]
return r
findParent = _deprecated_function_alias("findParent", "find_parent", "4.0.0")
@@ -1064,9 +1064,9 @@ class PageElement(object):
**kwargs: _StrainableAttribute,
) -> _AtMostOneElement:
r: _AtMostOneElement = None
l: _QueryResults = method(name, attrs, string, 1, _stacklevel=4, **kwargs)
if l:
r = l[0]
results: _QueryResults = method(name, attrs, string, 1, _stacklevel=4, **kwargs)
if results:
r = results[0]
return r
def _find_all(
@@ -1080,7 +1080,6 @@ class PageElement(object):
**kwargs: _StrainableAttribute,
) -> _QueryResults:
"""Iterates over a generator looking for things that match."""
results: _QueryResults
if string is None and "text" in kwargs:
string = kwargs.pop("text")
@@ -2645,9 +2644,9 @@ class Tag(PageElement):
:kwargs: Additional filters on attribute values.
"""
r = None
l = self.find_all(name, attrs, recursive, string, 1, _stacklevel=3, **kwargs)
if l:
r = l[0]
results = self.find_all(name, attrs, recursive, string, 1, _stacklevel=3, **kwargs)
if results:
r = results[0]
return r
findChild = _deprecated_function_alias("findChild", "find", "3.0.0")

View File

@@ -218,11 +218,11 @@ class TestFormatters(SoupTest):
def test_prettify_outputs_unicode_by_default(self):
soup = self.soup("<a></a>")
assert str == type(soup.prettify())
assert str is type(soup.prettify())
def test_prettify_can_encode_data(self):
soup = self.soup("<a></a>")
assert bytes == type(soup.prettify("utf-8"))
assert bytes is type(soup.prettify("utf-8"))
def test_html_entity_substitution_off_by_default(self):
markup = "<b>Sacr\N{LATIN SMALL LETTER E WITH ACUTE} bleu!</b>"

View File

@@ -128,7 +128,7 @@ class TestConstructor(SoupTest):
def feed(self, *args, **kwargs):
raise ParserRejectedMarkup("Nope.")
def prepare_markup(self, *args, **kwargs):
def prepare_markup(self, markup, *args, **kwargs):
# We're going to try two different ways of preparing this markup,
# but feed() will reject both of them.
yield markup, None, None, False

View File

@@ -11,7 +11,6 @@
# All configuration values have a default; values that are commented out
# serve to show the default.
import sys, os
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the

View File

@@ -11,7 +11,6 @@
# All configuration values have a default; values that are commented out
# serve to show the default.
import sys, os
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the

View File

@@ -11,7 +11,6 @@
# All configuration values have a default; values that are commented out
# serve to show the default.
import sys, os
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the

View File

@@ -11,7 +11,6 @@
# All configuration values have a default; values that are commented out
# serve to show the default.
import sys, os
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the

View File

@@ -50,7 +50,6 @@ DEMO_MARKUP = """A bare string
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">"""
from io import StringIO
import os
import sys
from bs4 import BeautifulSoup
parsers = ['html.parser']
@@ -58,13 +57,13 @@ parsers = ['html.parser']
try:
from bs4.builder import _lxml
parsers.append('lxml')
except ImportError as e:
except ImportError:
pass
try:
from bs4.builder import _html5lib
parsers.append('html5lib')
except ImportError as e:
except ImportError:
pass
class Demonstration(object):