Skip to content

Commit

Permalink
Upgrade Python syntax with pyupgrade --py38-plus
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk authored and facelessuser committed Aug 31, 2023
1 parent c88cf1c commit 1f05c31
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 88 deletions.
25 changes: 12 additions & 13 deletions soupsieve/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,21 @@ def purge() -> None:

def closest(
select: str,
tag: 'bs4.Tag',
tag: bs4.Tag,
namespaces: dict[str, str] | None = None,
flags: int = 0,
*,
custom: dict[str, str] | None = None,
**kwargs: Any
) -> 'bs4.Tag':
) -> bs4.Tag:
"""Match closest ancestor."""

return compile(select, namespaces, flags, **kwargs).closest(tag)


def match(
select: str,
tag: 'bs4.Tag',
tag: bs4.Tag,
namespaces: dict[str, str] | None = None,
flags: int = 0,
*,
Expand All @@ -106,61 +106,60 @@ def match(

def filter( # noqa: A001
select: str,
iterable: Iterable['bs4.Tag'],
iterable: Iterable[bs4.Tag],
namespaces: dict[str, str] | None = None,
flags: int = 0,
*,
custom: dict[str, str] | None = None,
**kwargs: Any
) -> list['bs4.Tag']:
) -> list[bs4.Tag]:
"""Filter list of nodes."""

return compile(select, namespaces, flags, **kwargs).filter(iterable)


def select_one(
select: str,
tag: 'bs4.Tag',
tag: bs4.Tag,
namespaces: dict[str, str] | None = None,
flags: int = 0,
*,
custom: dict[str, str] | None = None,
**kwargs: Any
) -> 'bs4.Tag':
) -> bs4.Tag:
"""Select a single tag."""

return compile(select, namespaces, flags, **kwargs).select_one(tag)


def select(
select: str,
tag: 'bs4.Tag',
tag: bs4.Tag,
namespaces: dict[str, str] | None = None,
limit: int = 0,
flags: int = 0,
*,
custom: dict[str, str] | None = None,
**kwargs: Any
) -> list['bs4.Tag']:
) -> list[bs4.Tag]:
"""Select the specified tags."""

return compile(select, namespaces, flags, **kwargs).select(tag, limit)


def iselect(
select: str,
tag: 'bs4.Tag',
tag: bs4.Tag,
namespaces: dict[str, str] | None = None,
limit: int = 0,
flags: int = 0,
*,
custom: dict[str, str] | None = None,
**kwargs: Any
) -> Iterator['bs4.Tag']:
) -> Iterator[bs4.Tag]:
"""Iterate the specified tags."""

for el in compile(select, namespaces, flags, **kwargs).iselect(tag, limit):
yield el
yield from compile(select, namespaces, flags, **kwargs).iselect(tag, limit)


def escape(ident: str) -> str:
Expand Down
16 changes: 8 additions & 8 deletions soupsieve/__meta__.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def __new__(
raise ValueError("All version parts except 'release' should be integers.")

if release not in REL_MAP:
raise ValueError("'{}' is not a valid release type.".format(release))
raise ValueError(f"'{release}' is not a valid release type.")

# Ensure valid pre-release (we do not allow implicit pre-releases).
if ".dev-candidate" < release < "final":
Expand All @@ -118,7 +118,7 @@ def __new__(
elif dev:
raise ValueError("Version is not a development release.")

return super(Version, cls).__new__(cls, major, minor, micro, release, pre, post, dev)
return super().__new__(cls, major, minor, micro, release, pre, post, dev)

def _is_pre(self) -> bool:
"""Is prerelease."""
Expand All @@ -145,15 +145,15 @@ def _get_canonical(self) -> str:

# Assemble major, minor, micro version and append `pre`, `post`, or `dev` if needed..
if self.micro == 0:
ver = "{}.{}".format(self.major, self.minor)
ver = f"{self.major}.{self.minor}"
else:
ver = "{}.{}.{}".format(self.major, self.minor, self.micro)
ver = f"{self.major}.{self.minor}.{self.micro}"
if self._is_pre():
ver += '{}{}'.format(REL_MAP[self.release], self.pre)
ver += f'{REL_MAP[self.release]}{self.pre}'
if self._is_post():
ver += ".post{}".format(self.post)
ver += f".post{self.post}"
if self._is_dev():
ver += ".dev{}".format(self.dev)
ver += f".dev{self.dev}"

return ver

Expand All @@ -164,7 +164,7 @@ def parse_version(ver: str) -> Version:
m = RE_VER.match(ver)

if m is None:
raise ValueError("'{}' is not a valid version".format(ver))
raise ValueError(f"'{ver}' is not a valid version")

# Handle major, minor, micro
major = int(m.group('major'))
Expand Down
10 changes: 4 additions & 6 deletions soupsieve/css_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def assert_valid_input(cls, tag: Any) -> None:

# Fail on unexpected types.
if not cls.is_tag(tag):
raise TypeError("Expected a BeautifulSoup 'Tag', but instead received type {}".format(type(tag)))
raise TypeError(f"Expected a BeautifulSoup 'Tag', but instead received type {type(tag)}")

@staticmethod
def is_doc(obj: bs4.Tag) -> bool:
Expand Down Expand Up @@ -165,8 +165,7 @@ def is_root(self, el: bs4.Tag) -> bool:
def get_contents(self, el: bs4.Tag, no_iframe: bool = False) -> Iterator[bs4.PageElement]:
"""Get contents or contents in reverse."""
if not no_iframe or not self.is_iframe(el):
for content in el.contents:
yield content
yield from el.contents

def get_children(
self,
Expand Down Expand Up @@ -394,7 +393,7 @@ def validate_day(year: int, month: int, day: int) -> bool:
def validate_week(year: int, week: int) -> bool:
"""Validate week."""

max_week = datetime.strptime("{}-{}-{}".format(12, 31, year), "%m-%d-%Y").isocalendar()[1]
max_week = datetime.strptime(f"{12}-{31}-{year}", "%m-%d-%Y").isocalendar()[1]
if max_week == 1:
max_week = 53
return 1 <= week <= max_week
Expand Down Expand Up @@ -1571,8 +1570,7 @@ def select(self, tag: bs4.Tag, limit: int = 0) -> list[bs4.Tag]:
def iselect(self, tag: bs4.Tag, limit: int = 0) -> Iterator[bs4.Tag]:
"""Iterate the specified tags."""

for el in CSSMatch(self.selectors, tag, self.namespaces, self.flags).select(limit):
yield el
yield from CSSMatch(self.selectors, tag, self.namespaces, self.flags).select(limit)

def __repr__(self) -> str: # pragma: no cover
"""Representation."""
Expand Down
Loading

0 comments on commit 1f05c31

Please sign in to comment.