Skip to content

Commit

Permalink
performance: cache parsed markers
Browse files Browse the repository at this point in the history
  • Loading branch information
radoering committed Feb 19, 2023
1 parent b5d1934 commit 6e7f17c
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/poetry/core/version/markers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from __future__ import annotations

import functools
import itertools
import re

from abc import ABC
from abc import abstractmethod
from typing import TYPE_CHECKING
from typing import Any
from typing import Callable
Expand Down Expand Up @@ -53,10 +56,12 @@ class UndefinedEnvironmentName(ValueError):
_parser = Parser(GRAMMAR_PEP_508_MARKERS, "lalr")


class BaseMarker:
class BaseMarker(ABC):
@abstractmethod
def intersect(self, other: BaseMarker) -> BaseMarker:
raise NotImplementedError()

@abstractmethod
def union(self, other: BaseMarker) -> BaseMarker:
raise NotImplementedError()

Expand All @@ -66,24 +71,37 @@ def is_any(self) -> bool:
def is_empty(self) -> bool:
return False

@abstractmethod
def validate(self, environment: dict[str, Any] | None) -> bool:
raise NotImplementedError()

@abstractmethod
def without_extras(self) -> BaseMarker:
raise NotImplementedError()

@abstractmethod
def exclude(self, marker_name: str) -> BaseMarker:
raise NotImplementedError()

@abstractmethod
def only(self, *marker_names: str) -> BaseMarker:
raise NotImplementedError()

@abstractmethod
def invert(self) -> BaseMarker:
raise NotImplementedError()

def __repr__(self) -> str:
return f"<{self.__class__.__name__} {str(self)}>"

@abstractmethod
def __hash__(self) -> int:
raise NotImplementedError()

@abstractmethod
def __eq__(self, other: object) -> bool:
raise NotImplementedError()


class AnyMarker(BaseMarker):
def intersect(self, other: BaseMarker) -> BaseMarker:
Expand Down Expand Up @@ -725,6 +743,7 @@ def is_empty(self) -> bool:
return all(m.is_empty() for m in self._markers)


@functools.lru_cache(maxsize=None)
def parse_marker(marker: str) -> BaseMarker:
if marker == "<empty>":
return EmptyMarker()
Expand Down

0 comments on commit 6e7f17c

Please sign in to comment.