Skip to content

Commit

Permalink
performance: cache parsed constraints and versions
Browse files Browse the repository at this point in the history
  • Loading branch information
radoering committed Feb 19, 2023
1 parent c218dde commit 4639236
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/poetry/core/constraints/generic/parser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import functools
import re

from typing import TYPE_CHECKING
Expand All @@ -17,6 +18,7 @@
BASIC_CONSTRAINT = re.compile(r"^(!?==?)?\s*([^\s]+?)\s*$")


@functools.lru_cache(maxsize=None)
def parse_constraint(constraints: str) -> BaseConstraint:
if constraints == "*":
return AnyConstraint()
Expand Down
2 changes: 2 additions & 0 deletions src/poetry/core/constraints/version/parser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import functools
import re

from typing import TYPE_CHECKING
Expand All @@ -12,6 +13,7 @@
from poetry.core.constraints.version.version_constraint import VersionConstraint


@functools.lru_cache(maxsize=None)
def parse_constraint(constraints: str) -> VersionConstraint:
if constraints == "*":
from poetry.core.constraints.version.version_range import VersionRange
Expand Down
4 changes: 3 additions & 1 deletion src/poetry/core/version/pep440/parser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import functools
import re

from typing import TYPE_CHECKING
Expand Down Expand Up @@ -63,6 +64,7 @@ def _get_local(cls, match: Match[str] | None) -> LocalSegmentType | None:
)

@classmethod
@functools.lru_cache(maxsize=None)
def parse(cls, value: str, version_class: type[T]) -> T:
match = cls._regex.search(value) if value else None
if not match:
Expand All @@ -80,4 +82,4 @@ def parse(cls, value: str, version_class: type[T]) -> T:


def parse_pep440(value: str, version_class: type[T]) -> T:
return PEP440Parser.parse(value, version_class)
return PEP440Parser.parse(value, version_class) # type: ignore[arg-type]

0 comments on commit 4639236

Please sign in to comment.