Skip to content

Commit

Permalink
perf: cache parsed requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
radoering authored and abn committed Jan 26, 2025
1 parent 6f8d54c commit afaa128
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/poetry/core/packages/dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ def create_from_pep_508(
from poetry.core.packages.vcs_dependency import VCSDependency
from poetry.core.utils.patterns import wheel_file_re
from poetry.core.vcs.git import ParsedUrl
from poetry.core.version.requirements import Requirement
from poetry.core.version.requirements import parse_requirement

# Removing comments
parts = name.split(" #", 1)
Expand All @@ -360,7 +360,7 @@ def create_from_pep_508(
if " ;" in rest:
name += " ;" + rest.split(" ;", 1)[1]

req = Requirement(name)
req = parse_requirement(name)

name = req.name
link = None
Expand Down Expand Up @@ -495,7 +495,7 @@ def _make_file_or_dir_dep(
path: Path,
base: Path | None = None,
subdirectory: str | None = None,
extras: list[str] | None = None,
extras: Iterable[str] | None = None,
) -> FileDependency | DirectoryDependency | None:
"""
Helper function to create a file or directoru dependency with the given arguments.
Expand Down
16 changes: 15 additions & 1 deletion src/poetry/core/version/requirements.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
from __future__ import annotations

import functools
import urllib.parse as urlparse

from typing import TYPE_CHECKING

from poetry.core.constraints.version import parse_constraint
from poetry.core.constraints.version.exceptions import ParseConstraintError
from poetry.core.version.grammars import GRAMMAR_PEP_508_CONSTRAINTS
from poetry.core.version.markers import _compact_markers
from poetry.core.version.parser import Parser


if TYPE_CHECKING:
from collections.abc import Sequence


class InvalidRequirementError(ValueError):
"""
An invalid requirement was found, users should refer to PEP 508.
Expand Down Expand Up @@ -61,7 +68,9 @@ def __init__(self, requirement_string: str) -> None:
else:
self.url = None

self.extras = [e.value for e in parsed.scan_values(lambda t: t.type == "EXTRA")]
self.extras: Sequence[str] = [
e.value for e in parsed.scan_values(lambda t: t.type == "EXTRA")
]
constraint = next(parsed.find_data("version_specification"), None)
constraint = ",".join(constraint.children) if constraint else "*"

Expand Down Expand Up @@ -102,3 +111,8 @@ def __str__(self) -> str:

def __repr__(self) -> str:
return f"<Requirement({str(self)!r})>"


@functools.cache
def parse_requirement(requirement_string: str) -> Requirement:
return Requirement(requirement_string)

0 comments on commit afaa128

Please sign in to comment.