Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
python-poetry
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open
python-poetry
Commits
d9b563b3
Commit
d9b563b3
authored
Nov 26, 2022
by
finswimmer
Committed by
Randy Döring
Feb 04, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: take `virtualenvs.prefer-active-python` into account on `poetry new`
parent
8949bfdc
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
68 additions
and
10 deletions
+68
-10
src/poetry/console/commands/init.py
+1
-1
src/poetry/console/commands/new.py
+13
-5
src/poetry/utils/env.py
+4
-4
tests/console/commands/test_new.py
+50
-0
No files found.
src/poetry/console/commands/init.py
View file @
d9b563b3
...
...
@@ -164,7 +164,7 @@ The <c1>init</c1> command creates a basic <comment>pyproject.toml</> file in the
default_python
=
(
"^"
+
EnvManager
.
get_python_version
(
preci
ous
=
2
,
preci
sion
=
2
,
prefer_active_python
=
config
.
get
(
"virtualenvs.prefer-active-python"
),
io
=
self
.
io
,
)
.
to_string
()
...
...
src/poetry/console/commands/new.py
View file @
d9b563b3
from
__future__
import
annotations
import
sys
from
contextlib
import
suppress
from
cleo.helpers
import
argument
...
...
@@ -31,8 +29,9 @@ class NewCommand(Command):
from
poetry.core.vcs.git
import
GitConfig
from
poetry.config.config
import
Config
from
poetry.layouts
import
layout
from
poetry.utils.env
import
SystemEnv
from
poetry.utils.env
import
EnvManager
if
self
.
io
.
input
.
option
(
"directory"
):
self
.
line_error
(
...
...
@@ -71,8 +70,17 @@ class NewCommand(Command):
if
author_email
:
author
+=
f
" <{author_email}>"
current_env
=
SystemEnv
(
Path
(
sys
.
executable
))
default_python
=
"^"
+
"."
.
join
(
str
(
v
)
for
v
in
current_env
.
version_info
[:
2
])
poetry_config
=
Config
.
create
()
default_python
=
(
"^"
+
EnvManager
.
get_python_version
(
precision
=
2
,
prefer_active_python
=
poetry_config
.
get
(
"virtualenvs.prefer-active-python"
),
io
=
self
.
io
,
)
.
to_string
()
)
layout_
=
layout_cls
(
name
,
...
...
src/poetry/utils/env.py
View file @
d9b563b3
...
...
@@ -563,11 +563,11 @@ class EnvManager:
@staticmethod
def
get_python_version
(
preci
ous
:
int
=
3
,
preci
sion
:
int
=
3
,
prefer_active_python
:
bool
=
False
,
io
:
None
|
IO
=
None
,
)
->
Version
:
version
=
"."
.
join
(
str
(
v
)
for
v
in
sys
.
version_info
[:
preci
ous
])
version
=
"."
.
join
(
str
(
v
)
for
v
in
sys
.
version_info
[:
preci
sion
])
if
prefer_active_python
:
executable
=
EnvManager
.
_detect_active_python
(
io
)
...
...
@@ -582,7 +582,7 @@ class EnvManager:
)
.
strip
()
)
version
=
"."
.
join
(
str
(
v
)
for
v
in
python_patch
.
split
(
"."
)[:
preci
ous
])
version
=
"."
.
join
(
str
(
v
)
for
v
in
python_patch
.
split
(
"."
)[:
preci
sion
])
return
Version
.
parse
(
version
)
...
...
@@ -701,7 +701,7 @@ class EnvManager:
"virtualenvs.prefer-active-python"
)
python_minor
=
self
.
get_python_version
(
preci
ous
=
2
,
prefer_active_python
=
prefer_active_python
,
io
=
self
.
_io
preci
sion
=
2
,
prefer_active_python
=
prefer_active_python
,
io
=
self
.
_io
)
.
to_string
()
venv_path
=
self
.
_poetry
.
config
.
virtualenvs_path
...
...
tests/console/commands/test_new.py
View file @
d9b563b3
from
__future__
import
annotations
import
subprocess
import
sys
from
pathlib
import
Path
from
typing
import
TYPE_CHECKING
from
typing
import
Any
import
pytest
...
...
@@ -10,7 +14,9 @@ from poetry.factory import Factory
if
TYPE_CHECKING
:
from
cleo.testers.command_tester
import
CommandTester
from
pytest_mock
import
MockerFixture
from
poetry.config.config
import
Config
from
poetry.poetry
import
Poetry
from
tests.types
import
CommandTesterFactory
...
...
@@ -170,3 +176,47 @@ def test_command_new_with_readme(fmt: str | None, tester: CommandTester, tmp_dir
poetry
=
verify_project_directory
(
path
,
package
,
package
,
None
)
assert
poetry
.
local_config
.
get
(
"readme"
)
==
f
"README.{fmt or 'md'}"
@pytest.mark.parametrize
(
[
"prefer_active"
,
"python"
],
[
(
True
,
"1.1"
),
(
False
,
f
"{sys.version_info[0]}.{sys.version_info[1]}"
),
],
)
def
test_respect_prefer_active_on_new
(
prefer_active
:
bool
,
python
:
str
,
config
:
Config
,
mocker
:
MockerFixture
,
tester
:
CommandTester
,
tmp_dir
:
str
,
):
from
poetry.utils.env
import
GET_PYTHON_VERSION_ONELINER
orig_check_output
=
subprocess
.
check_output
def
mock_check_output
(
cmd
:
str
,
*
_
:
Any
,
**
__
:
Any
)
->
str
:
if
GET_PYTHON_VERSION_ONELINER
in
cmd
:
return
"1.1.1"
return
orig_check_output
(
cmd
,
*
_
,
**
__
)
mocker
.
patch
(
"subprocess.check_output"
,
side_effect
=
mock_check_output
)
config
.
config
[
"virtualenvs"
][
"prefer-active-python"
]
=
prefer_active
package
=
"package"
path
=
Path
(
tmp_dir
)
/
package
options
=
[
path
.
as_posix
()]
tester
.
execute
(
" "
.
join
(
options
))
pyproject_file
=
path
/
"pyproject.toml"
expected
=
f
"""
\
[tool.poetry.dependencies]
python = "^{python}"
"""
assert
expected
in
pyproject_file
.
read_text
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment