Skip to content

Commit

Permalink
Convert a PyPI name to conda iff it's a conda dep
Browse files Browse the repository at this point in the history
Otherwise when we parse non-Poetry pyproject.toml, it always converts
PyPI names to conda names, even if the dep stays as PyPI.
  • Loading branch information
maresb committed Sep 14, 2024
1 parent 14a25bd commit 6bad717
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion conda_lock/src_parser/environment_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def _parse_environment_file_for_platform(
continue

dependency = parse_python_requirement(
spec, manager="pip", category=category, normalize_name=False
spec, manager="pip", category=category
)
if evaluate_marker(dependency.markers, platform):
# The above condition will skip adding the dependency if a
Expand Down
13 changes: 11 additions & 2 deletions conda_lock/src_parser/pyproject_toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,14 +466,23 @@ def parse_python_requirement(
requirement: str,
manager: Literal["conda", "pip"] = "conda",
category: str = "main",
normalize_name: bool = True,
) -> Dependency:
"""Parse a requirements.txt like requirement to a conda spec.
>>> parse_python_requirement("my_package") # doctest: +NORMALIZE_WHITESPACE
VersionedDependency(name='my-package', manager='conda', category='main', extras=[],
markers=None, version='*', build=None, conda_channel=None, hash=None)
The PyPI name `build` will be translated to `python-build` for conda.
>>> parse_python_requirement("build") # doctest: +NORMALIZE_WHITESPACE
VersionedDependency(name='python-build', manager='conda', category='main',
extras=[], markers=None, version='*', build=None, conda_channel=None, hash=None)
No translation is done for `manager="pip"`.
>>> parse_python_requirement("build", manager="pip") # doctest: +NORMALIZE_WHITESPACE
VersionedDependency(name='build', manager='pip', category='main',
extras=[], markers=None, version='*', build=None, conda_channel=None, hash=None)
>>> parse_python_requirement(
... "My_Package[extra]==1.23"
... ) # doctest: +NORMALIZE_WHITESPACE
Expand Down Expand Up @@ -513,7 +522,7 @@ def parse_python_requirement(
if conda_version:
conda_version = ",".join(sorted(conda_version.split(",")))

if normalize_name:
if manager == "conda":
conda_dep_name = pypi_name_to_conda_name(name)
else:
conda_dep_name = name
Expand Down

0 comments on commit 6bad717

Please sign in to comment.