Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply some refurb suggestions #763

Merged
merged 7 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 [])
pradyunsg marked this conversation as resolved.
Show resolved Hide resolved
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(Specifier(specifier) for specifier in split_specifiers)
DimitriPapadopoulos marked this conversation as resolved.
Show resolved Hide resolved

# Store our prereleases value so we can use it later to determine if
# we accept prereleases or not.
Expand Down
12 changes: 6 additions & 6 deletions src/packaging/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ def parse_tag(tag: str) -> FrozenSet[Tag]:
Returning a set is required due to the possibility that the tag is a
compressed tag set.
"""
tags = set()
interpreters, abis, platforms = tag.split("-")
for interpreter in interpreters.split("."):
for abi in abis.split("."):
for platform_ in platforms.split("."):
tags.add(Tag(interpreter, abi, platform_))
return frozenset(tags)
return frozenset(
Tag(interpreter, abi, platform_)
for platform_ in platforms.split(".")
for abi in abis.split(".")
for interpreter in interpreters.split(".")
)
DimitriPapadopoulos marked this conversation as resolved.
Show resolved Hide resolved


def _get_config_var(name: str, warn: bool = False) -> Union[int, str, None]:
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