Skip to content

Commit

Permalink
Merge branch 'release/v0.15.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
wpk committed Dec 17, 2024
2 parents 8615d1a + 201732a commit 0a01e2c
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 24 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ See the fragment files in [changelog.d]

<!-- scriv-insert-here -->

## v0.15.0 — 2024-12-17

### Added

- Can now pass requirements for package with `--req/-r "-e ."` for example.

## v0.14.0 — 2024-12-16

### Added
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ $ p2c project -f tests/data/test-pyproject.toml --dry
# Creating requirements base.txt
athing
bthing
cthing;python_version<"3.10"
cthing; python_version < "3.10"
# --------------------
# Creating yaml py310-test-extras.yaml
channels:
Expand Down
2 changes: 1 addition & 1 deletion requirements/lock/py311-docs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ mdit-py-plugins==0.4.2 # via myst-parser
mdurl==0.1.2 # via markdown-it-py
myst-parser==4.0.0 # via -r requirements/docs.txt
packaging==24.2 # via sphinx, -r requirements/docs.txt
pydata-sphinx-theme==0.16.0 # via sphinx-book-theme
pydata-sphinx-theme==0.16.1 # via sphinx-book-theme
pyenchant==3.2.2 # via sphinxcontrib-spelling, -r requirements/docs.txt
pygments==2.18.0 # via accessible-pygments, pydata-sphinx-theme, rich, sphinx
pyyaml==6.0.2 # via myst-parser
Expand Down
4 changes: 2 additions & 2 deletions requirements/lock/py311-uvxrun-tools.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ msgpack==1.1.0 # via cachecontrol
mypy==1.13.0 # via -r requirements/uvxrun-tools.txt
mypy-extensions==1.0.0 # via mypy
nbqa==1.9.1 # via -r requirements/uvxrun-tools.txt
nh3==0.2.19 # via readme-renderer
nh3==0.2.20 # via readme-renderer
nodeenv==1.9.1 # via pyright
packaging==24.2 # via conda-lock, ensureconda, grayskull, pyproject2conda, twine
parso==0.8.4 # via jedi
Expand All @@ -63,7 +63,7 @@ pydantic==2.10.3 # via conda-lock
pydantic-core==2.27.1 # via pydantic
pygments==2.18.0 # via ipython, readme-renderer, rich
pylev==1.4.0 # via clikit
pyproject2conda==0.13.0 # via -r requirements/uvxrun-tools.txt
pyproject2conda==0.14.0 # via -r requirements/uvxrun-tools.txt
pyright==1.1.390 # via -r requirements/uvxrun-tools.txt
python-utils==3.9.1 # via progressbar2
pyyaml==6.0.2 # via conda-lock
Expand Down
22 changes: 14 additions & 8 deletions src/pyproject2conda/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,10 @@ class Overwrite(str, Enum):
typer.Option(
"--reqs",
"-r",
help="Additional pip requirements.",
help="""
Additional pip requirements. For example, pass `-r '-e .'` to included
editable version of current package in requirements file.
""",
),
]
ENVS_CLI = Annotated[
Expand Down Expand Up @@ -397,11 +400,10 @@ class Overwrite(str, Enum):
REMOVE_WHITESPACE_OPTION = typer.Option(
"--remove-whitespace/--no-remove-whitespace",
help="""
What to do with whitespace in a dependency. The default
(`--remove-whitespace`) is to remove whitespace in a given dependency. For
example, the dependency `package >= 1.0` will be converted to
`package>=1.0`. Pass `--no-remove-whitespace` to keep the the whitespace in
the output.
What to do with whitespace in a dependency. Passing `--remove-whitespace`
will remove whitespace in a given dependency. For example, the dependency
`package >= 1.0` will be converted to `package>=1.0`. Pass
`--no-remove-whitespace` to keep the the whitespace in the output.
""",
)

Expand Down Expand Up @@ -597,9 +599,9 @@ def requirements(
verbose: VERBOSE_CLI = None,
reqs: REQS_CLI = None,
allow_empty: Annotated[bool, ALLOW_EMPTY_OPTION] = False,
remove_whitespace: Annotated[bool, REMOVE_WHITESPACE_OPTION] = True,
remove_whitespace: Annotated[bool, REMOVE_WHITESPACE_OPTION] = False,
) -> None:
"""Create requirements.txt for pip dependencies."""
"""Create requirements.txt for pip dependencies. Note that all requirements are normalized using `packaging.requirements.Requirement`"""
if not update_target(output, filename, overwrite=overwrite.value):
_log_skipping(logger, "requirements", output)
return
Expand Down Expand Up @@ -635,6 +637,8 @@ def project(
envs: ENVS_CLI = None,
template: TEMPLATE_CLI = None,
template_python: TEMPLATE_PYTHON_CLI = None,
reqs: REQS_CLI = None,
deps: DEPS_CLI = None,
reqs_ext: REQS_EXT_CLI = ".txt",
yaml_ext: YAML_EXT_CLI = ".yaml",
sort: SORT_DEPENDENCIES_CLI = True,
Expand All @@ -660,6 +664,8 @@ def project(
yaml_ext=yaml_ext,
template=template,
template_python=template_python,
reqs=reqs,
deps=deps,
sort=sort,
header=header,
overwrite=overwrite.value,
Expand Down
11 changes: 9 additions & 2 deletions src/pyproject2conda/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ def _iter_yaml(
]

data = {k: defaults.get(k, getattr(self, k)(env_name)) for k in keys}

if not pythons:
if output is None:
output = filename_from_template(
Expand Down Expand Up @@ -383,7 +384,7 @@ def _iter_yaml(
yield ("yaml", dict(data, python=python, output=output))

def _iter_reqs(
self, env_name: str, **defaults: Any
self, env_name: str, remove_whitespace: bool | None = None, **defaults: Any
) -> Iterator[tuple[str, dict[str, Any]]]:
keys = [
"extras",
Expand All @@ -396,13 +397,19 @@ def _iter_reqs(
"verbose",
"reqs",
"allow_empty",
"remove_whitespace",
]

output, template, _ = self._get_output_and_templates(env_name, **defaults)

data = {k: defaults.get(k, getattr(self, k)(env_name)) for k in keys}

# different default from yaml
data["remove_whitespace"] = (
remove_whitespace
if remove_whitespace is not None
else self.remove_whitespace(env_name, default=False)
)

output = self.output(env_name)
if not output: # pragma: no cover
output = filename_from_template(
Expand Down
13 changes: 11 additions & 2 deletions src/pyproject2conda/requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from copy import copy

from packaging.markers import Marker
from packaging.requirements import Requirement
from packaging.requirements import InvalidRequirement, Requirement
from packaging.specifiers import SpecifierSet

from pyproject2conda.utils import (
Expand All @@ -50,7 +50,16 @@ def _check_allow_empty(allow_empty: bool) -> str:


def _clean_pip_reqs(reqs: list[str]) -> list[str]:
return [str(Requirement(r)) for r in reqs]
out: list[str] = []
for req in reqs:
try:
r = str(Requirement(req))
except InvalidRequirement:
# trust that user knows what they're doing
r = req
out.append(r)

return out


def _clean_conda_requirement(
Expand Down
14 changes: 7 additions & 7 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ def test_requirements(fname, opt, runner) -> None:
expected = """\
athing
bthing
cthing;python_version<"3.10"
cthing; python_version < "3.10"
"""

for cmd in ["r", "requirements"]:
Expand All @@ -427,7 +427,7 @@ def test_requirements(fname, opt, runner) -> None:
expected = """\
athing
bthing
cthing;python_version<"3.10"
cthing; python_version < "3.10"
pandas
pytest
matplotlib
Expand All @@ -451,11 +451,11 @@ def test_requirements(fname, opt, runner) -> None:
expected = """\
athing
bthing
cthing;python_version<"3.10"
cthing; python_version < "3.10"
pandas
pytest
matplotlib
thing;python_version<"3.10"
thing; python_version < "3.10"
other
"""

Expand All @@ -477,7 +477,7 @@ def test_requirements(fname, opt, runner) -> None:
expected = """\
athing
bthing
cthing;python_version<"3.10"
cthing; python_version < "3.10"
matplotlib
pandas
pytest
Expand All @@ -492,12 +492,12 @@ def test_requirements(fname, opt, runner) -> None:
expected = """\
athing
bthing
cthing;python_version<"3.10"
cthing; python_version < "3.10"
matplotlib
other
pandas
pytest
thing;python_version<"3.10"
thing; python_version < "3.10"
"""

result = do_run(
Expand Down
27 changes: 26 additions & 1 deletion tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,31 @@ def test_option_override_base(simple_config: Config) -> None:
)


def test_option_override_base_reqs(simple_config: Config) -> None:
output = list(simple_config.iter_envs(envs=["base"], reqs=["-e ."]))

assert output[0] == (
"yaml",
{
"extras": [],
"groups": [],
"extras_or_groups": [],
"sort": True,
"skip_package": False,
"header": None,
"overwrite": "check",
"verbose": None,
"reqs": ["-e ."],
"deps": None,
"name": None,
"channels": ["conda-forge"],
"allow_empty": False,
"remove_whitespace": True,
"output": "hello-base.yaml",
},
)


def test_option_override_base2(simple_config: Config) -> None:
output = list(simple_config.iter_envs(envs=["base2"]))

Expand Down Expand Up @@ -489,7 +514,7 @@ def test_option_override_extension_txt(simple_config: Config) -> None:
"verbose": None,
"reqs": None,
"allow_empty": False,
"remove_whitespace": True,
"remove_whitespace": False,
"output": "hello-extension_txt.in",
},
)
Expand Down
14 changes: 14 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,20 @@ def test_complete(style, toml) -> None:

assert dedent(expected) == d.to_conda_yaml()

expected = """\
channels:
- conda-forge
dependencies:
- bthing-conda
- conda-forge::cthing
- pip
- pip:
- -e.
- athing
"""

assert dedent(expected) == d.to_conda_yaml(pip_deps="-e .")

# test -p option
expected = """\
channels:
Expand Down

0 comments on commit 0a01e2c

Please sign in to comment.