Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for context formatting in ProjectConfig #1859

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/hatch/project/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
if TYPE_CHECKING:
from packaging.requirements import Requirement

from hatchling.utils.context import Context


class ProjectConfig:
def __init__(self, root, config, plugin_manager=None):
def __init__(self, root, config, plugin_manager=None, metadata=None):
self.root = root
self.config = config
self.plugin_manager = plugin_manager
self.metadata = metadata

self._matrices = None
self._env = None
Expand All @@ -34,6 +37,18 @@ def __init__(self, root, config, plugin_manager=None):
self._publish = None
self._scripts = None
self._cached_env_overrides = {}
self._context = None

@property
def context(self) -> Context:
if self._context is None:
if self.metadata:
self._context = self.metadata.context
else:
from hatchling.utils.context import Context

self._context = Context(self.root)
return self._context

@cached_property
def build(self):
Expand Down Expand Up @@ -74,7 +89,7 @@ def env_requires_complex(self) -> list[Requirement]:
raise TypeError(message)

try:
requires_complex.append(Requirement(entry))
requires_complex.append(Requirement(self.context.format(entry)))
except InvalidRequirement as e:
message = f'Requirement #{i} in `tool.hatch.env.requires` is invalid: {e}'
raise ValueError(message) from None
Expand Down
2 changes: 1 addition & 1 deletion src/hatch/project/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def config(self):
if self._config is None:
from hatch.project.config import ProjectConfig

self._config = ProjectConfig(self.location, self.metadata.hatch.config, self.plugin_manager)
self._config = ProjectConfig(self.location, self.metadata.hatch.config, self.plugin_manager, self.metadata)

return self._config

Expand Down
9 changes: 9 additions & 0 deletions tests/project/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ def test_defined(self, isolation):

assert project_config.env_requires == ['foo', 'bar', 'baz']

def test_context_formatting(self, isolation, uri_slash_prefix):
absolute_path = str(isolation)
project_config = ProjectConfig(isolation, {'env': {'requires': ['plugin@ {root:uri}']}})

normalized_path = absolute_path.replace('\\', '/')
assert project_config.env_requires == [
f'plugin@ file:{uri_slash_prefix}{normalized_path}',
]


class TestEnvCollectors:
def test_not_table(self, isolation):
Expand Down