Skip to content

Commit 4639236

Browse files
committed
performance: cache parsed constraints and versions
1 parent c218dde commit 4639236

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed

src/poetry/core/constraints/generic/parser.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import functools
34
import re
45

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

1920

21+
@functools.lru_cache(maxsize=None)
2022
def parse_constraint(constraints: str) -> BaseConstraint:
2123
if constraints == "*":
2224
return AnyConstraint()

src/poetry/core/constraints/version/parser.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import functools
34
import re
45

56
from typing import TYPE_CHECKING
@@ -12,6 +13,7 @@
1213
from poetry.core.constraints.version.version_constraint import VersionConstraint
1314

1415

16+
@functools.lru_cache(maxsize=None)
1517
def parse_constraint(constraints: str) -> VersionConstraint:
1618
if constraints == "*":
1719
from poetry.core.constraints.version.version_range import VersionRange

src/poetry/core/version/pep440/parser.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import functools
34
import re
45

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

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

8183

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

0 commit comments

Comments
 (0)