Skip to content

Commit

Permalink
Fix handling of the metadata and Python resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
ofek committed Oct 26, 2023
1 parent 838b494 commit 7351b29
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/hatch/env/virtual.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
if TYPE_CHECKING:
from collections.abc import Iterable

from packaging.specifiers import SpecifierSet
from virtualenv.discovery.py_info import PythonInfo

from hatch.python.core import PythonManager
Expand Down Expand Up @@ -210,7 +211,7 @@ def get_interpreter_resolver_env(self) -> dict[str, str]:
return env

internal_path = os.pathsep.join(python_dirs)
old_path = os.environ.pop('PATH', None)
old_path = env.pop('PATH', None)
env['PATH'] = internal_path if old_path is None else f'{old_path}{os.pathsep}{internal_path}'

return env
Expand All @@ -231,7 +232,7 @@ def _interpreter_is_compatible(self, interpreter: PythonInfo) -> bool:
return (
interpreter.executable
and self._is_stable_path(interpreter.executable)
and self.metadata.core.python_constraint.contains(interpreter.version_str)
and self._python_constraint.contains(interpreter.version_str)
)

def _get_concrete_interpreter_path(self, python_version: str = '') -> str | None:
Expand Down Expand Up @@ -349,6 +350,16 @@ def _python_resolvers(self) -> dict[str, Callable[[str], str | None]]:
'internal': self._resolve_internal_interpreter_path,
}

@cached_property
def _python_constraint(self) -> SpecifierSet:
from packaging.specifiers import SpecifierSet

from hatch.utils.metadata import get_project_metadata

project_metadata = get_project_metadata(self)

return SpecifierSet(project_metadata['requires-python'])

@contextmanager
def safe_activation(self):
# Set user-defined environment variables first so ours take precedence
Expand Down
30 changes: 30 additions & 0 deletions src/hatch/utils/metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Any

if TYPE_CHECKING:
from hatch.env.plugin.interface import EnvironmentInterface


def get_project_metadata(environment: EnvironmentInterface) -> dict[str, Any]:
from hatchling.dep.core import dependencies_in_sync

if not environment.metadata.hatch.metadata.hook_config or dependencies_in_sync(
environment.metadata.build.requires_complex
):
from hatchling.metadata.utils import resolve_metadata_fields

with environment.root.as_cwd():
return resolve_metadata_fields(environment.metadata)

try:
environment.check_compatibility()
except Exception as e:
environment.app.abort(f'Environment `{environment.name}` is incompatible: {e}')

import json

with environment.root.as_cwd(), environment.build_environment(environment.metadata.build.requires):
command = ['python', '-u', '-W', 'ignore', '-m', 'hatchling', 'metadata', '--app', '--compact']
process = environment.platform.capture_process(command)
return json.loads(environment.app.read_builder(process))

0 comments on commit 7351b29

Please sign in to comment.