Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions esp_bool_parser/bool_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from ast import (
literal_eval,
)
from functools import lru_cache

from packaging.version import (
Version,
Expand Down Expand Up @@ -66,6 +67,7 @@ class ChipAttr(Stmt):
def __init__(self, t: ParseResults):
self.attr: str = t[0]

@lru_cache(None)
Copy link

Copilot AI May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using lru_cache on instance methods can lead to unexpected behavior if the instance (self) is mutable or not hashable. Consider ensuring that instances remain immutable or refactoring the caching mechanism to operate on a function that receives only hashable parameters.

Copilot uses AI. Check for mistakes.
def get_value(self, target: str, config_name: str) -> t.Any:
if self.attr in self.addition_attr:
return self.addition_attr[self.attr](target=target, config_name=config_name)
Expand Down Expand Up @@ -110,6 +112,7 @@ class Integer(Stmt):
def __init__(self, t: ParseResults):
self.expr: str = t[0]

@lru_cache(None)
def get_value(self, target: str, config_name: str) -> t.Any: # noqa: ARG002
return literal_eval(self.expr)

Expand All @@ -118,6 +121,7 @@ class String(Stmt):
def __init__(self, t: ParseResults):
self.expr: str = t[0]

@lru_cache(None)
def get_value(self, target: str, config_name: str) -> t.Any: # noqa: ARG002
return literal_eval(f'"{self.expr}"') # double quotes is swallowed by QuotedString

Expand All @@ -126,6 +130,7 @@ class List_(Stmt):
def __init__(self, t: ParseResults):
self.expr = t

@lru_cache(None)
def get_value(self, target: str, config_name: str) -> t.Any:
return [item.get_value(target, config_name) for item in self.expr]

Expand All @@ -147,6 +152,7 @@ def __init__(self, t: ParseResults):
self.comparison: str = t[1]
self.right: Stmt = t[2]

@lru_cache(None)
def get_value(self, target: str, config_name: str) -> t.Any:
_l = self.left.get_value(target, config_name)
_r = self.right.get_value(target, config_name)
Expand Down Expand Up @@ -183,30 +189,20 @@ def _or(_l, _r):

class BoolOr(BoolExpr):
def __init__(self, res: ParseResults):
self.bool_stmts: t.List[BoolStmt] = []
for stmt in res[0]:
if stmt != 'or':
self.bool_stmts.append(stmt)
self.bool_stmts: t.List[BoolStmt] = [stmt for stmt in res[0] if stmt != 'or']

@lru_cache(None)
def get_value(self, target: str, config_name: str) -> t.Any:
for stmt in self.bool_stmts:
if stmt.get_value(target, config_name):
return True
return False
return any(stmt.get_value(target, config_name) for stmt in self.bool_stmts)


class BoolAnd(BoolExpr):
def __init__(self, res: ParseResults):
self.bool_stmts: t.List[BoolStmt] = []
for stmt in res[0]:
if stmt != 'and':
self.bool_stmts.append(stmt)
self.bool_stmts: t.List[BoolStmt] = [stmt for stmt in res[0] if stmt != 'and']

@lru_cache(None)
def get_value(self, target: str, config_name: str) -> t.Any:
for stmt in self.bool_stmts:
if not stmt.get_value(target, config_name):
return False
return True
return all(stmt.get_value(target, config_name) for stmt in self.bool_stmts)


CAP_WORD = Word(alphas.upper(), nums + alphas.upper() + '_').setParseAction(ChipAttr)
Expand Down