-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add solutions for common errors (#2396)
- Loading branch information
Showing
16 changed files
with
196 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .python_requirement_solution_provider import PythonRequirementSolutionProvider |
30 changes: 30 additions & 0 deletions
30
poetry/mixology/solutions/providers/python_requirement_solution_provider.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import re | ||
|
||
from typing import List | ||
|
||
from crashtest.contracts.has_solutions_for_exception import HasSolutionsForException | ||
from crashtest.contracts.solution import Solution | ||
|
||
|
||
class PythonRequirementSolutionProvider(HasSolutionsForException): | ||
def can_solve(self, exception): # type: (Exception) -> bool | ||
from poetry.puzzle.exceptions import SolverProblemError | ||
|
||
if not isinstance(exception, SolverProblemError): | ||
return False | ||
|
||
m = re.match( | ||
"^The current project's Python requirement (.+) is not compatible " | ||
"with some of the required packages Python requirement", | ||
str(exception), | ||
) | ||
|
||
if not m: | ||
return False | ||
|
||
return True | ||
|
||
def get_solutions(self, exception): # type: (Exception) -> List[Solution] | ||
from ..solutions.python_requirement_solution import PythonRequirementSolution | ||
|
||
return [PythonRequirementSolution(exception)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .python_requirement_solution import PythonRequirementSolution |
52 changes: 52 additions & 0 deletions
52
poetry/mixology/solutions/solutions/python_requirement_solution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from crashtest.contracts.solution import Solution | ||
|
||
|
||
class PythonRequirementSolution(Solution): | ||
def __init__(self, exception): | ||
from poetry.mixology.incompatibility_cause import PythonCause | ||
from poetry.core.semver import parse_constraint | ||
|
||
self._title = "Check your dependencies Python requirement." | ||
|
||
failure = exception.error | ||
version_solutions = [] | ||
for incompatibility in failure._incompatibility.external_incompatibilities: | ||
if isinstance(incompatibility.cause, PythonCause): | ||
root_constraint = parse_constraint( | ||
incompatibility.cause.root_python_version | ||
) | ||
constraint = parse_constraint(incompatibility.cause.python_version) | ||
|
||
version_solutions.append( | ||
"For <fg=default;options=bold>{}</>, a possible solution would be " | ||
'to set the `<fg=default;options=bold>python</>` property to <fg=yellow>"{}"</>'.format( | ||
incompatibility.terms[0].dependency.name, | ||
root_constraint.intersect(constraint), | ||
) | ||
) | ||
|
||
description = ( | ||
"The Python requirement can be specified via the `<fg=default;options=bold>python</>` " | ||
"or `<fg=default;options=bold>markers</>` properties" | ||
) | ||
if version_solutions: | ||
description += "\n\n" + "\n".join(version_solutions) | ||
|
||
description += "\n" | ||
|
||
self._description = description | ||
|
||
@property | ||
def solution_title(self) -> str: | ||
return self._title | ||
|
||
@property | ||
def solution_description(self): | ||
return self._description | ||
|
||
@property | ||
def documentation_links(self): | ||
return [ | ||
"https://python-poetry.org/docs/dependency-specification/#python-restricted-dependencies", | ||
"https://python-poetry.org/docs/dependency-specification/#using-environment-markers", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
42 changes: 42 additions & 0 deletions
42
tests/mixology/solutions/providers/test_python_requirement_solution_provider.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import pytest | ||
|
||
from poetry.core.packages.dependency import Dependency | ||
from poetry.mixology.failure import SolveFailure | ||
from poetry.mixology.incompatibility import Incompatibility | ||
from poetry.mixology.incompatibility_cause import NoVersionsCause | ||
from poetry.mixology.incompatibility_cause import PythonCause | ||
from poetry.mixology.term import Term | ||
from poetry.puzzle.exceptions import SolverProblemError | ||
from poetry.utils._compat import PY36 | ||
|
||
|
||
@pytest.mark.skipif( | ||
not PY36, reason="Error solutions are only available for Python ^3.6" | ||
) | ||
def test_it_can_solve_python_incompatibility_solver_errors(): | ||
from poetry.mixology.solutions.providers import PythonRequirementSolutionProvider | ||
from poetry.mixology.solutions.solutions import PythonRequirementSolution | ||
|
||
incompatibility = Incompatibility( | ||
[Term(Dependency("foo", "^1.0"), True)], PythonCause("^3.5", ">=3.6") | ||
) | ||
exception = SolverProblemError(SolveFailure(incompatibility)) | ||
provider = PythonRequirementSolutionProvider() | ||
|
||
assert provider.can_solve(exception) | ||
assert isinstance(provider.get_solutions(exception)[0], PythonRequirementSolution) | ||
|
||
|
||
@pytest.mark.skipif( | ||
not PY36, reason="Error solutions are only available for Python ^3.6" | ||
) | ||
def test_it_cannot_solve_other_solver_errors(): | ||
from poetry.mixology.solutions.providers import PythonRequirementSolutionProvider | ||
|
||
incompatibility = Incompatibility( | ||
[Term(Dependency("foo", "^1.0"), True)], NoVersionsCause() | ||
) | ||
exception = SolverProblemError(SolveFailure(incompatibility)) | ||
provider = PythonRequirementSolutionProvider() | ||
|
||
assert not provider.can_solve(exception) |
Empty file.
41 changes: 41 additions & 0 deletions
41
tests/mixology/solutions/solutions/test_python_requirement_solution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import pytest | ||
|
||
from clikit.io.buffered_io import BufferedIO | ||
|
||
from poetry.core.packages.dependency import Dependency | ||
from poetry.mixology.failure import SolveFailure | ||
from poetry.mixology.incompatibility import Incompatibility | ||
from poetry.mixology.incompatibility_cause import PythonCause | ||
from poetry.mixology.term import Term | ||
from poetry.puzzle.exceptions import SolverProblemError | ||
from poetry.utils._compat import PY36 | ||
|
||
|
||
@pytest.mark.skipif( | ||
not PY36, reason="Error solutions are only available for Python ^3.6" | ||
) | ||
def test_it_provides_the_correct_solution(): | ||
from poetry.mixology.solutions.solutions import PythonRequirementSolution | ||
|
||
incompatibility = Incompatibility( | ||
[Term(Dependency("foo", "^1.0"), True)], PythonCause("^3.5", ">=3.6") | ||
) | ||
exception = SolverProblemError(SolveFailure(incompatibility)) | ||
solution = PythonRequirementSolution(exception) | ||
|
||
title = "Check your dependencies Python requirement." | ||
description = """\ | ||
The Python requirement can be specified via the `python` or `markers` properties | ||
For foo, a possible solution would be to set the `python` property to ">=3.6,<4.0"\ | ||
""" | ||
links = [ | ||
"https://python-poetry.org/docs/dependency-specification/#python-restricted-dependencies", | ||
"https://python-poetry.org/docs/dependency-specification/#using-environment-markers", | ||
] | ||
|
||
assert title == solution.solution_title | ||
assert ( | ||
description == BufferedIO().remove_format(solution.solution_description).strip() | ||
) | ||
assert links == solution.documentation_links |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters