Skip to content

Commit

Permalink
Merge branch 'python-poetry:master' into python-poetrygh-4979
Browse files Browse the repository at this point in the history
  • Loading branch information
hdk5 authored Feb 8, 2022
2 parents de803c8 + 31657e7 commit 034274f
Show file tree
Hide file tree
Showing 38 changed files with 352 additions and 231 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ repos:
args: [--lines-after-imports, "-1"]

- repo: https://github.com/psf/black
rev: 21.12b0
rev: 22.1.0
hooks:
- id: black

Expand Down
2 changes: 1 addition & 1 deletion docs/managing-environments.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ to activate one explicitly, see [Switching environments](#switching-between-envi
{{% note %}}
If you use a tool like [pyenv](https://github.com/pyenv/pyenv) to manage different Python versions,
you can set the experimental `virtualenvs.prefer-active-python` option to `true`. Poetry
than will try to find the current `python` of your shell.
will then try to find the current `python` of your shell.

For instance, if your project requires a newer Python than is available with
your system, a standard workflow would be:
Expand Down
52 changes: 32 additions & 20 deletions src/poetry/console/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@

from poetry.console.commands.command import Command
from poetry.console.commands.env_command import EnvCommand
from poetry.utils.helpers import canonicalize_name


if TYPE_CHECKING:
from poetry.core.packages.package import Package
from tomlkit.items import InlineTable

from poetry.repositories import Pool
Expand Down Expand Up @@ -242,6 +244,26 @@ def handle(self) -> int:

return 0

def _generate_choice_list(
self, matches: List["Package"], canonicalized_name: str
) -> List[str]:
choices = []
matches_names = [p.name for p in matches]
exact_match = canonicalized_name in matches_names
if exact_match:
choices.append(matches[matches_names.index(canonicalized_name)].pretty_name)

for found_package in matches:
if len(choices) >= 10:
break

if found_package.name == canonicalized_name:
continue

choices.append(found_package.pretty_name)

return choices

def _determine_requirements(
self,
requires: List[str],
Expand All @@ -254,7 +276,7 @@ def _determine_requirements(
package = self.ask(
"Search for package to add (or leave blank to continue):"
)
while package is not None:
while package:
constraint = self._parse_requirements([package])[0]
if (
"git" in constraint
Expand All @@ -267,34 +289,24 @@ def _determine_requirements(
package = self.ask("\nAdd a package:")
continue

matches = self._get_pool().search(constraint["name"])

canonicalized_name = canonicalize_name(constraint["name"])
matches = self._get_pool().search(canonicalized_name)
if not matches:
self.line("<error>Unable to find package</error>")
package = False
else:
choices = []
matches_names = [p.name for p in matches]
exact_match = constraint["name"] in matches_names
if exact_match:
choices.append(
matches[matches_names.index(constraint["name"])].pretty_name
)

for found_package in matches:
if len(choices) >= 10:
break

if found_package.name.lower() == constraint["name"].lower():
continue
choices = self._generate_choice_list(matches, canonicalized_name)

choices.append(found_package.pretty_name)

self.line(
info_string = (
f"Found <info>{len(matches)}</info> packages matching"
f" <c1>{package}</c1>"
)

if len(matches) > 10:
info_string += "\nShowing the first 10 matches"

self.line(info_string)

package = self.choice(
"\nEnter package # to add, or the complete package name if it"
" is not listed",
Expand Down
3 changes: 2 additions & 1 deletion src/poetry/mixology/incompatibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ def __init__(self, terms: List["Term"], cause: "IncompatibilityCause") -> None:
# incompatibility irrelevant, since we already know that mutually
# exclusive version ranges are incompatible. We should never derive
# an irrelevant incompatibility.
assert by_ref[ref] is not None
err_msg = f"Package '{ref}' is listed as a dependency of itself."
assert by_ref[ref] is not None, err_msg
else:
by_ref[ref] = term

Expand Down
4 changes: 3 additions & 1 deletion src/poetry/utils/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ def activate(self, env: "VirtualEnv") -> Optional[int]:
bin_dir = "Scripts" if WINDOWS else "bin"
activate_path = env.path / bin_dir / activate_script

if WINDOWS:
# mypy requires using sys.platform instead of WINDOWS constant
# in if statements to properly type check on Windows
if sys.platform == "win32":
if self._name in ("powershell", "pwsh"):
args = ["-NoExit", "-File", str(activate_path)]
else:
Expand Down
6 changes: 3 additions & 3 deletions tests/console/commands/debug/test_resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_debug_resolve_gives_resolution_results(tester: "CommandTester"):
cachy 0.2.0
"""

assert expected == tester.io.fetch_output()
assert tester.io.fetch_output() == expected


def test_debug_resolve_tree_option_gives_the_dependency_tree(tester: "CommandTester"):
Expand All @@ -58,7 +58,7 @@ def test_debug_resolve_tree_option_gives_the_dependency_tree(tester: "CommandTes
└── msgpack-python >=0.5 <0.6
"""

assert expected == tester.io.fetch_output()
assert tester.io.fetch_output() == expected


def test_debug_resolve_git_dependency(tester: "CommandTester"):
Expand All @@ -73,4 +73,4 @@ def test_debug_resolve_git_dependency(tester: "CommandTester"):
demo 0.1.2
"""

assert expected == tester.io.fetch_output()
assert tester.io.fetch_output() == expected
2 changes: 1 addition & 1 deletion tests/console/commands/env/test_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def test_env_info_displays_complete_info(tester: "CommandTester"):
Executable: python
"""

assert expected == tester.io.fetch_output()
assert tester.io.fetch_output() == expected


def test_env_info_displays_path_only(tester: "CommandTester"):
Expand Down
6 changes: 3 additions & 3 deletions tests/console/commands/env/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_none_activated(
mocker.patch("poetry.utils.env.EnvManager.get", return_value=env)
tester.execute()
expected = "\n".join(venvs_in_cache_dirs).strip()
assert expected == tester.io.fetch_output().strip()
assert tester.io.fetch_output().strip() == expected


def test_activated(
Expand All @@ -52,10 +52,10 @@ def test_activated(
expected = (
"\n".join(venvs_in_cache_dirs).strip().replace("py3.7", "py3.7 (Activated)")
)
assert expected == tester.io.fetch_output().strip()
assert tester.io.fetch_output().strip() == expected


def test_in_project_venv(tester: "CommandTester", venvs_in_project_dir: List[str]):
tester.execute()
expected = ".venv (Activated)\n"
assert expected == tester.io.fetch_output()
assert tester.io.fetch_output() == expected
10 changes: 5 additions & 5 deletions tests/console/commands/env/test_remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_remove_by_python_version(
assert not (venv_cache / f"{venv_name}-py3.6").exists()

expected = f"Deleted virtualenv: {venv_cache / venv_name}-py3.6\n"
assert expected == tester.io.fetch_output()
assert tester.io.fetch_output() == expected


def test_remove_by_name(
Expand All @@ -58,7 +58,7 @@ def test_remove_by_name(

expected += f"Deleted virtualenv: {venv_cache / name}\n"

assert expected == tester.io.fetch_output()
assert tester.io.fetch_output() == expected


def test_remove_all(
Expand All @@ -72,7 +72,7 @@ def test_remove_all(
for name in venvs_in_cache_dirs:
assert not (venv_cache / name).exists()
expected.add(f"Deleted virtualenv: {venv_cache / name}")
assert expected == set(tester.io.fetch_output().split("\n"))
assert set(tester.io.fetch_output().split("\n")) == expected


def test_remove_all_and_version(
Expand All @@ -86,7 +86,7 @@ def test_remove_all_and_version(
for name in venvs_in_cache_dirs:
assert not (venv_cache / name).exists()
expected.add(f"Deleted virtualenv: {venv_cache / name}")
assert expected == set(tester.io.fetch_output().split("\n"))
assert set(tester.io.fetch_output().split("\n")) == expected


def test_remove_multiple(
Expand All @@ -104,4 +104,4 @@ def test_remove_multiple(
expected.add(f"Deleted virtualenv: {venv_cache / name}")
for name in remaining_envs:
assert (venv_cache / name).exists()
assert expected == set(tester.io.fetch_output().split("\n"))
assert set(tester.io.fetch_output().split("\n")) == expected
6 changes: 3 additions & 3 deletions tests/console/commands/env/test_use.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def test_activate_activates_non_existing_virtualenv_no_envs_file(
Using virtualenv: {venv_py37}
"""

assert expected == tester.io.fetch_output()
assert tester.io.fetch_output() == expected


def test_get_prefers_explicitly_activated_virtualenvs_over_env_var(
Expand Down Expand Up @@ -115,7 +115,7 @@ def test_get_prefers_explicitly_activated_virtualenvs_over_env_var(
Using virtualenv: {venv_dir}
"""

assert expected == tester.io.fetch_output()
assert tester.io.fetch_output() == expected


def test_get_prefers_explicitly_activated_non_existing_virtualenvs_over_env_var(
Expand Down Expand Up @@ -150,4 +150,4 @@ def test_get_prefers_explicitly_activated_non_existing_virtualenvs_over_env_var(
Using virtualenv: {venv_dir}
"""

assert expected == tester.io.fetch_output()
assert tester.io.fetch_output() == expected
2 changes: 1 addition & 1 deletion tests/console/commands/test_about.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ def test_about(tester: "CommandTester"):
See https://github.com/python-poetry/poetry for more information.
"""

assert expected == tester.io.fetch_output()
assert tester.io.fetch_output() == expected
Loading

0 comments on commit 034274f

Please sign in to comment.