Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[flake8]
max-line-length = 88
ignore = E501, E203, W503, ANN101, ANN102
ignore = E501, E203, W503, ANN101, ANN102, SIM106

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that we ignore SIM106 in poetry as well. So I'm fine here. In many cases this warning helps to improve the code. But I've no idea how often it is not helpful in our cases. Maybe we should evaluate it some point later, and only ignore it case-by-case then.

ban-relative-imports = True
mypy-init-return = True
format-greedy = 1
Expand Down
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ repos:
- flake8-annotations
- flake8-bugbear
- flake8-comprehensions
- flake8-simplify
- flake8-tidy-imports
- flake8-use-fstring

Expand Down
8 changes: 1 addition & 7 deletions src/poetry/core/masonry/builders/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,13 +377,7 @@ def __init__(
else:
self.path = self.path

try:
self.path = self.path.resolve()
except FileNotFoundError:
# this is an issue in in python 3.5, since resolve uses strict=True by
# default, this workaround needs to be maintained till python 2.7 and
# python 3.5 are dropped, until we can use resolve(strict=False).
pass
self.path = self.path.resolve()

def __eq__(self, other: Union["BuildIncludeFile", Path]) -> bool:
if hasattr(other, "path"):
Expand Down
29 changes: 14 additions & 15 deletions src/poetry/core/masonry/builders/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,24 +101,23 @@ def build(self) -> None:
new_mode = normalize_file_permissions(st_mode)
os.chmod(temp_path, new_mode)

with os.fdopen(fd, "w+b") as fd_file:
with zipfile.ZipFile(
fd_file, mode="w", compression=zipfile.ZIP_DEFLATED
) as zip_file:
if not self._editable:
if not self._poetry.package.build_should_generate_setup():
self._build(zip_file)
self._copy_module(zip_file)
else:
self._copy_module(zip_file)
self._build(zip_file)
with os.fdopen(fd, "w+b") as fd_file, zipfile.ZipFile(
fd_file, mode="w", compression=zipfile.ZIP_DEFLATED
) as zip_file:
if not self._editable:
if not self._poetry.package.build_should_generate_setup():
self._build(zip_file)
self._copy_module(zip_file)
else:
self._copy_module(zip_file)
self._build(zip_file)
self._add_pth(zip_file)
else:
self._build(zip_file)
self._add_pth(zip_file)

self._copy_file_scripts(zip_file)
self._write_metadata(zip_file)
self._write_record(zip_file)
self._copy_file_scripts(zip_file)
self._write_metadata(zip_file)
self._write_record(zip_file)

wheel_path = dist_dir / self.wheel_filename
if wheel_path.exists():
Expand Down
13 changes: 4 additions & 9 deletions src/poetry/core/packages/constraints/multi_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ def constraints(self) -> Tuple[Constraint]:
return self._constraints

def allows(self, other: "ConstraintTypes") -> bool:
for constraint in self._constraints:
if not constraint.allows(other):
return False

return True
return all(constraint.allows(other) for constraint in self._constraints)

def allows_all(self, other: "ConstraintTypes") -> bool:
if other.is_any():
Expand Down Expand Up @@ -64,10 +60,9 @@ def allows_any(self, other: "ConstraintTypes") -> bool:
return self.allows(other)

if isinstance(other, MultiConstraint):
for c1 in self.constraints:
for c2 in other.constraints:
if c1.allows(c2):
return True
return any(
c1.allows(c2) for c1 in self.constraints for c2 in other.constraints
)

return False

Expand Down
17 changes: 6 additions & 11 deletions src/poetry/core/packages/constraints/union_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ def constraints(self) -> Tuple[Constraint]:
def allows(
self, other: Union[Constraint, MultiConstraint, "UnionConstraint"]
) -> bool:
for constraint in self._constraints:
if constraint.allows(other):
return True

return False
return any(constraint.allows(other) for constraint in self._constraints)

def allows_any(self, other: "ConstraintTypes") -> bool:
if other.is_empty():
Expand All @@ -41,12 +37,11 @@ def allows_any(self, other: "ConstraintTypes") -> bool:
else:
constraints = other.constraints

for our_constraint in self._constraints:
for their_constraint in constraints:
if our_constraint.allows_any(their_constraint):
return True

return False
return any(
our_constraint.allows_any(their_constraint)
for our_constraint in self._constraints
for their_constraint in constraints
)

def allows_all(self, other: "ConstraintTypes") -> bool:
if other.is_any():
Expand Down
7 changes: 3 additions & 4 deletions src/poetry/core/packages/dependency.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import re

from contextlib import suppress
from pathlib import Path
from typing import TYPE_CHECKING
from typing import Any
Expand Down Expand Up @@ -573,16 +574,14 @@ def create_from_pep_508(
name=name, path=path, base=relative_to, extras=req.extras
)
else:
try:
with suppress(ValueError):
# this is a local path not using the file URI scheme
dep = _make_file_or_dir_dep(
name=name,
path=Path(req.url),
base=relative_to,
extras=req.extras,
)
except ValueError:
pass

if dep is None:
dep = Dependency(name, version or "*", extras=req.extras)
Expand Down Expand Up @@ -613,7 +612,7 @@ def __eq__(self, other: Any) -> bool:
)

def __ne__(self, other: Any) -> bool:
return not self == other
return not self.__eq__(other)

def __hash__(self) -> int:
return super().__hash__() ^ hash(self._constraint) ^ hash(self._extras)
Expand Down
11 changes: 4 additions & 7 deletions src/poetry/core/packages/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,9 @@ def full_pretty_version(self) -> str:
if self.source_type not in ["hg", "git"]:
return self._pretty_version

if self.source_resolved_reference:
if len(self.source_resolved_reference) == 40:
ref = self.source_resolved_reference[0:7]
return f"{self._pretty_version} {ref}"
if self.source_resolved_reference and len(self.source_resolved_reference) == 40:
ref = self.source_resolved_reference[0:7]
return f"{self._pretty_version} {ref}"

# if source reference is a sha1 hash -- truncate
if len(self.source_reference) == 40:
Expand Down Expand Up @@ -271,9 +270,7 @@ def license(self, value: Optional[Union[str, "License"]]) -> None:
from poetry.core.spdx.helpers import license_by_id
from poetry.core.spdx.license import License # noqa

if value is None:
self._license = value
elif isinstance(value, License):
if value is None or isinstance(value, License):
self._license = value
else:
self._license = license_by_id(value)
Expand Down
7 changes: 4 additions & 3 deletions src/poetry/core/packages/specification.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ def is_same_package_as(self, other: "PackageSpecification") -> bool:
if self._source_type != other.source_type:
return False

if self._source_url or other.source_url:
if self._source_url != other.source_url:
return False
if (
self._source_url or other.source_url
) and self._source_url != other.source_url:
return False

# We check the resolved reference first:
# if they match we assume equality regardless
Expand Down
9 changes: 4 additions & 5 deletions src/poetry/core/pyproject/tables.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from contextlib import suppress
from pathlib import Path
from typing import TYPE_CHECKING
from typing import List
Expand Down Expand Up @@ -39,7 +40,9 @@ def dependencies(self) -> List["Dependency"]:
except ValueError:
# PEP 517 requires can be path if not PEP 508
path = Path(requirement)
try:
# compatibility Python < 3.8
# https://docs.python.org/3/library/pathlib.html#methods
with suppress(OSError):
if path.is_file():
dependency = FileDependency(
name=canonicalize_name(path.name), path=path
Expand All @@ -48,10 +51,6 @@ def dependencies(self) -> List["Dependency"]:
dependency = DirectoryDependency(
name=canonicalize_name(path.name), path=path
)
except OSError:
# compatibility Python < 3.8
# https://docs.python.org/3/library/pathlib.html#methods
pass

if dependency is None:
# skip since we could not determine requirement
Expand Down
11 changes: 5 additions & 6 deletions src/poetry/core/semver/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,15 @@ def parse_single_constraint(constraint: str) -> VersionTypes:

if op == "<":
return VersionRange(max=version)
elif op == "<=":
if op == "<=":
return VersionRange(max=version, include_max=True)
elif op == ">":
if op == ">":
return VersionRange(min=version)
elif op == ">=":
if op == ">=":
return VersionRange(min=version, include_min=True)
elif op == "!=":
if op == "!=":
return VersionUnion(VersionRange(max=version), VersionRange(min=version))
else:
return version
return version

from poetry.core.semver.exceptions import ParseConstraintError

Expand Down
21 changes: 5 additions & 16 deletions src/poetry/core/version/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,11 +461,7 @@ def union(self, other: MarkerTypes) -> MarkerTypes:
return other.union(self)

def validate(self, environment: Dict[str, Any]) -> bool:
for m in self._markers:
if not m.validate(environment):
return False

return True
return all(m.validate(environment) for m in self._markers)

def without_extras(self) -> MarkerTypes:
return self.exclude("extra")
Expand Down Expand Up @@ -521,9 +517,7 @@ def __hash__(self) -> int:
def __str__(self) -> str:
elements = []
for m in self._markers:
if isinstance(m, SingleMarker):
elements.append(str(m))
elif isinstance(m, MultiMarker):
if isinstance(m, (SingleMarker, MultiMarker)):
elements.append(str(m))
else:
elements.append(f"({str(m)})")
Expand Down Expand Up @@ -625,11 +619,7 @@ def union(self, other: MarkerTypes) -> MarkerTypes:
return MarkerUnion.of(*new_markers)

def validate(self, environment: Dict[str, Any]) -> bool:
for m in self._markers:
if m.validate(environment):
return True

return False
return any(m.validate(environment) for m in self._markers)

def without_extras(self) -> MarkerTypes:
return self.exclude("extra")
Expand Down Expand Up @@ -733,9 +723,8 @@ def _compact_markers(tree_elements: "Tree", tree_prefix: str = "") -> MarkerType

value = value[1:-1]
groups[-1] = MultiMarker.of(groups[-1], SingleMarker(name, f"{op}{value}"))
elif token.data == f"{tree_prefix}BOOL_OP":
if token.children[0] == "or":
groups.append(MultiMarker())
elif token.data == f"{tree_prefix}BOOL_OP" and token.children[0] == "or":
groups.append(MultiMarker())

for i, group in enumerate(reversed(groups)):
if group.is_empty():
Expand Down
6 changes: 3 additions & 3 deletions tests/masonry/builders/test_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def test_metadata_with_vcs_dependencies():

requires_dist = metadata["Requires-Dist"]

assert "cleo @ git+https://github.com/sdispater/cleo.git@master" == requires_dist
assert requires_dist == "cleo @ git+https://github.com/sdispater/cleo.git@master"


def test_metadata_with_url_dependencies():
Expand All @@ -158,8 +158,8 @@ def test_metadata_with_url_dependencies():
requires_dist = metadata["Requires-Dist"]

assert (
"demo @ https://python-poetry.org/distributions/demo-0.1.0-py2.py3-none-any.whl"
== requires_dist
requires_dist
== "demo @ https://python-poetry.org/distributions/demo-0.1.0-py2.py3-none-any.whl"
)


Expand Down
4 changes: 2 additions & 2 deletions tests/masonry/builders/test_sdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,9 +468,9 @@ def test_default_with_excluded_data(mocker: "MockerFixture"):
"my-package-1.2.3/PKG-INFO",
]:
# generated files have timestamp set to 0
assert 0 == tarinfo.mtime
assert tarinfo.mtime == 0
continue
assert 0 < tarinfo.mtime
assert tarinfo.mtime > 0


def test_src_excluded_nested_data():
Expand Down
2 changes: 1 addition & 1 deletion tests/masonry/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def test_prepare_metadata_for_build_wheel():
with temporary_directory() as tmp_dir, cwd(os.path.join(fixtures, "complete")):
dirname = api.prepare_metadata_for_build_wheel(tmp_dir)

assert "my_package-1.2.3.dist-info" == dirname
assert dirname == "my_package-1.2.3.dist-info"

dist_info = Path(tmp_dir, dirname)

Expand Down
Loading