Skip to content

Commit

Permalink
Apply some refurb suggestions (#763)
Browse files Browse the repository at this point in the history
[FURB108]: Replace `x == y or x == z` with `x in (y, z)`
[FURB110]: Replace `x if x else y` with `x or y`
[FURB142]: Replace `for x in y: s.add(...)` with `s.update(... for x in y)`
[FURB179]: Replace `itertools.chain(*x)` with `itertools.chain.from_iterable(x)`

---------

Co-authored-by: Pradyun Gedam <[email protected]>
Co-authored-by: Brett Cannon <[email protected]>
  • Loading branch information
3 people authored Jan 12, 2024
1 parent d0067e9 commit 7bcd6d8
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 37 deletions.
5 changes: 1 addition & 4 deletions src/packaging/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,7 @@ def _parse_marker_var(tokenizer: Tokenizer) -> MarkerVar:


def process_env_var(env_var: str) -> Variable:
if (
env_var == "platform_python_implementation"
or env_var == "python_implementation"
):
if env_var in ("platform_python_implementation", "python_implementation"):
return Variable("platform_python_implementation")
else:
return Variable(env_var)
Expand Down
2 changes: 1 addition & 1 deletion src/packaging/requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def __init__(self, requirement_string: str) -> None:

self.name: str = parsed.name
self.url: Optional[str] = parsed.url or None
self.extras: Set[str] = set(parsed.extras if parsed.extras else [])
self.extras: Set[str] = set(parsed.extras or [])
self.specifier: SpecifierSet = SpecifierSet(parsed.specifier)
self.marker: Optional[Marker] = None
if parsed.marker is not None:
Expand Down
27 changes: 7 additions & 20 deletions src/packaging/specifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,7 @@
import abc
import itertools
import re
from typing import (
Callable,
Iterable,
Iterator,
List,
Optional,
Set,
Tuple,
TypeVar,
Union,
)
from typing import Callable, Iterable, Iterator, List, Optional, Tuple, TypeVar, Union

from .utils import canonicalize_version
from .version import Version
Expand Down Expand Up @@ -697,7 +687,10 @@ def _pad_version(left: List[str], right: List[str]) -> Tuple[List[str], List[str
left_split.insert(1, ["0"] * max(0, len(right_split[0]) - len(left_split[0])))
right_split.insert(1, ["0"] * max(0, len(left_split[0]) - len(right_split[0])))

return (list(itertools.chain(*left_split)), list(itertools.chain(*right_split)))
return (
list(itertools.chain.from_iterable(left_split)),
list(itertools.chain.from_iterable(right_split)),
)


class SpecifierSet(BaseSpecifier):
Expand Down Expand Up @@ -729,14 +722,8 @@ def __init__(
# strip each item to remove leading/trailing whitespace.
split_specifiers = [s.strip() for s in specifiers.split(",") if s.strip()]

# Parsed each individual specifier, attempting first to make it a
# Specifier.
parsed: Set[Specifier] = set()
for specifier in split_specifiers:
parsed.add(Specifier(specifier))

# Turn our parsed specifiers into a frozen set and save them for later.
self._specs = frozenset(parsed)
# Make each individual specifier a Specifier and save in a frozen set for later.
self._specs = frozenset(map(Specifier, split_specifiers))

# Store our prereleases value so we can use it later to determine if
# we accept prereleases or not.
Expand Down
12 changes: 4 additions & 8 deletions tests/test_specifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,7 @@ def test_specifiers_hash(self, specifier):

@pytest.mark.parametrize(
("left", "right", "op"),
itertools.chain(
*
itertools.chain.from_iterable(
# Verify that the equal (==) operator works correctly
[[(x, x, operator.eq) for x in SPECIFIERS]]
+
Expand All @@ -260,8 +259,7 @@ def test_comparison_canonicalizes(self, left, right):

@pytest.mark.parametrize(
("left", "right", "op"),
itertools.chain(
*
itertools.chain.from_iterable(
# Verify that the equal (==) operator works correctly
[[(x, x, operator.ne) for x in SPECIFIERS]]
+
Expand Down Expand Up @@ -815,8 +813,7 @@ def test_specifiers_combine_not_implemented(self):

@pytest.mark.parametrize(
("left", "right", "op"),
itertools.chain(
*
itertools.chain.from_iterable(
# Verify that the equal (==) operator works correctly
[[(x, x, operator.eq) for x in SPECIFIERS]]
+
Expand All @@ -836,8 +833,7 @@ def test_comparison_true(self, left, right, op):

@pytest.mark.parametrize(
("left", "right", "op"),
itertools.chain(
*
itertools.chain.from_iterable(
# Verify that the equal (==) operator works correctly
[[(x, x, operator.ne) for x in SPECIFIERS]]
+
Expand Down
6 changes: 2 additions & 4 deletions tests/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,8 +667,7 @@ def test_version_is_postrelease(self, version, expected):
("left", "right", "op"),
# Below we'll generate every possible combination of VERSIONS that
# should be True for the given operator
itertools.chain(
*
itertools.chain.from_iterable(
# Verify that the less than (<) operator works correctly
[
[(x, y, operator.lt) for y in VERSIONS[i + 1 :]]
Expand Down Expand Up @@ -710,8 +709,7 @@ def test_comparison_true(self, left, right, op):
("left", "right", "op"),
# Below we'll generate every possible combination of VERSIONS that
# should be False for the given operator
itertools.chain(
*
itertools.chain.from_iterable(
# Verify that the less than (<) operator works correctly
[
[(x, y, operator.lt) for y in VERSIONS[: i + 1]]
Expand Down

0 comments on commit 7bcd6d8

Please sign in to comment.