Skip to content

Commit 7bcd6d8

Browse files
DimitriPapadopoulospradyunsgbrettcannon
authored
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 <pradyunsg@gmail.com> Co-authored-by: Brett Cannon <brett@python.org>
1 parent d0067e9 commit 7bcd6d8

File tree

5 files changed

+15
-37
lines changed

5 files changed

+15
-37
lines changed

src/packaging/_parser.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,10 +324,7 @@ def _parse_marker_var(tokenizer: Tokenizer) -> MarkerVar:
324324

325325

326326
def process_env_var(env_var: str) -> Variable:
327-
if (
328-
env_var == "platform_python_implementation"
329-
or env_var == "python_implementation"
330-
):
327+
if env_var in ("platform_python_implementation", "python_implementation"):
331328
return Variable("platform_python_implementation")
332329
else:
333330
return Variable(env_var)

src/packaging/requirements.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def __init__(self, requirement_string: str) -> None:
3838

3939
self.name: str = parsed.name
4040
self.url: Optional[str] = parsed.url or None
41-
self.extras: Set[str] = set(parsed.extras if parsed.extras else [])
41+
self.extras: Set[str] = set(parsed.extras or [])
4242
self.specifier: SpecifierSet = SpecifierSet(parsed.specifier)
4343
self.marker: Optional[Marker] = None
4444
if parsed.marker is not None:

src/packaging/specifiers.py

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,7 @@
1111
import abc
1212
import itertools
1313
import re
14-
from typing import (
15-
Callable,
16-
Iterable,
17-
Iterator,
18-
List,
19-
Optional,
20-
Set,
21-
Tuple,
22-
TypeVar,
23-
Union,
24-
)
14+
from typing import Callable, Iterable, Iterator, List, Optional, Tuple, TypeVar, Union
2515

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

700-
return (list(itertools.chain(*left_split)), list(itertools.chain(*right_split)))
690+
return (
691+
list(itertools.chain.from_iterable(left_split)),
692+
list(itertools.chain.from_iterable(right_split)),
693+
)
701694

702695

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

732-
# Parsed each individual specifier, attempting first to make it a
733-
# Specifier.
734-
parsed: Set[Specifier] = set()
735-
for specifier in split_specifiers:
736-
parsed.add(Specifier(specifier))
737-
738-
# Turn our parsed specifiers into a frozen set and save them for later.
739-
self._specs = frozenset(parsed)
725+
# Make each individual specifier a Specifier and save in a frozen set for later.
726+
self._specs = frozenset(map(Specifier, split_specifiers))
740727

741728
# Store our prereleases value so we can use it later to determine if
742729
# we accept prereleases or not.

tests/test_specifiers.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,7 @@ def test_specifiers_hash(self, specifier):
235235

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

261260
@pytest.mark.parametrize(
262261
("left", "right", "op"),
263-
itertools.chain(
264-
*
262+
itertools.chain.from_iterable(
265263
# Verify that the equal (==) operator works correctly
266264
[[(x, x, operator.ne) for x in SPECIFIERS]]
267265
+
@@ -815,8 +813,7 @@ def test_specifiers_combine_not_implemented(self):
815813

816814
@pytest.mark.parametrize(
817815
("left", "right", "op"),
818-
itertools.chain(
819-
*
816+
itertools.chain.from_iterable(
820817
# Verify that the equal (==) operator works correctly
821818
[[(x, x, operator.eq) for x in SPECIFIERS]]
822819
+
@@ -836,8 +833,7 @@ def test_comparison_true(self, left, right, op):
836833

837834
@pytest.mark.parametrize(
838835
("left", "right", "op"),
839-
itertools.chain(
840-
*
836+
itertools.chain.from_iterable(
841837
# Verify that the equal (==) operator works correctly
842838
[[(x, x, operator.ne) for x in SPECIFIERS]]
843839
+

tests/test_version.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -667,8 +667,7 @@ def test_version_is_postrelease(self, version, expected):
667667
("left", "right", "op"),
668668
# Below we'll generate every possible combination of VERSIONS that
669669
# should be True for the given operator
670-
itertools.chain(
671-
*
670+
itertools.chain.from_iterable(
672671
# Verify that the less than (<) operator works correctly
673672
[
674673
[(x, y, operator.lt) for y in VERSIONS[i + 1 :]]
@@ -710,8 +709,7 @@ def test_comparison_true(self, left, right, op):
710709
("left", "right", "op"),
711710
# Below we'll generate every possible combination of VERSIONS that
712711
# should be False for the given operator
713-
itertools.chain(
714-
*
712+
itertools.chain.from_iterable(
715713
# Verify that the less than (<) operator works correctly
716714
[
717715
[(x, y, operator.lt) for y in VERSIONS[: i + 1]]

0 commit comments

Comments
 (0)