From 7bcd6d8ec33f0a614f9d017d31be4b50ece1549a Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos Orfanos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Fri, 12 Jan 2024 20:31:39 +0100 Subject: [PATCH] Apply some refurb suggestions (#763) [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 Co-authored-by: Brett Cannon --- src/packaging/_parser.py | 5 +---- src/packaging/requirements.py | 2 +- src/packaging/specifiers.py | 27 +++++++-------------------- tests/test_specifiers.py | 12 ++++-------- tests/test_version.py | 6 ++---- 5 files changed, 15 insertions(+), 37 deletions(-) diff --git a/src/packaging/_parser.py b/src/packaging/_parser.py index 4576981c..684df754 100644 --- a/src/packaging/_parser.py +++ b/src/packaging/_parser.py @@ -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) diff --git a/src/packaging/requirements.py b/src/packaging/requirements.py index 0c00eba3..bdc43a7e 100644 --- a/src/packaging/requirements.py +++ b/src/packaging/requirements.py @@ -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: diff --git a/src/packaging/specifiers.py b/src/packaging/specifiers.py index 94448327..2d015bab 100644 --- a/src/packaging/specifiers.py +++ b/src/packaging/specifiers.py @@ -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 @@ -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): @@ -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. diff --git a/tests/test_specifiers.py b/tests/test_specifiers.py index ebcd9d98..a79b860e 100644 --- a/tests/test_specifiers.py +++ b/tests/test_specifiers.py @@ -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]] + @@ -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]] + @@ -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]] + @@ -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]] + diff --git a/tests/test_version.py b/tests/test_version.py index 8004c0cc..6cdbe190 100644 --- a/tests/test_version.py +++ b/tests/test_version.py @@ -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 :]] @@ -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]]