Skip to content

Commit

Permalink
fix: parse name earlier
Browse files Browse the repository at this point in the history
Signed-off-by: Frost Ming <[email protected]>
  • Loading branch information
frostming committed Jan 15, 2025
1 parent 4821fe5 commit d363210
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/pdm/models/candidates.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,6 @@ def __init__(
:param link: the file link of the candidate.
"""
self.req = req
if isinstance(req, FileRequirement):
req.check_installable()
self.name = name or self.req.project_name
self.version = version
if link is None and not req.is_named:
Expand Down Expand Up @@ -292,6 +290,8 @@ def format(self) -> str:
def prepare(self, environment: BaseEnvironment, reporter: CandidateReporter | None = None) -> PreparedCandidate:
"""Prepare the candidate for installation."""
if self._prepared is None:
if isinstance(self.req, FileRequirement):
self.req.check_installable()
self._prepared = PreparedCandidate(self, environment, reporter=reporter or CandidateReporter())
else:
self._prepared.environment = environment
Expand Down
8 changes: 3 additions & 5 deletions src/pdm/models/requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,9 @@ def _parse_url(self) -> None:
self.path = path
# For relative path, we don't resolve URL now, so the path may still contain fragments,
# it will be handled in `relocate()` method.
result = Setup.from_directory(self.absolute_path) # type: ignore[arg-type]
if result.name:
self.name = result.name
else:
url = url_without_fragments(self.url)
relpath = get_relative_path(url)
Expand Down Expand Up @@ -401,13 +404,8 @@ def check_installable(self) -> None:
if path.is_dir():
if not path.joinpath("setup.py").exists() and not path.joinpath("pyproject.toml").exists():
raise RequirementError(f"The local path '{self.path}' is not installable.")
result = Setup.from_directory(path)
if result.name:
self.name = result.name
elif self.editable:
raise RequirementError("Local file requirement must not be editable.")
elif self.editable and not self.is_vcs:
raise RequirementError("Non-VCS remote file requirement must not be editable.")


@dataclasses.dataclass(eq=False)
Expand Down
3 changes: 2 additions & 1 deletion tests/models/test_candidates.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,5 +314,6 @@ def test_parse_metadata_with_dynamic_fields(project, local_finder):

def test_get_metadata_for_non_existing_path(project):
req = parse_requirement("file:///${PROJECT_ROOT}/non-existing-path")
candidate = Candidate(req)
with pytest.raises(RequirementError, match="The local path '.+' does not exist"):
Candidate(req)
candidate.prepare(project.environment).metadata
2 changes: 0 additions & 2 deletions tests/models/test_requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ def filter_requirements_to_lines(
@pytest.mark.parametrize("req, result", REQUIREMENTS)
def test_convert_req_dict_to_req_line(req, result):
r = parse_requirement(req)
if hasattr(r, "check_installable"):
r.check_installable()
result = result or req
assert r.as_line() == result

Expand Down

0 comments on commit d363210

Please sign in to comment.