From ae00ecfee25a394687d9104964174fe061008d72 Mon Sep 17 00:00:00 2001 From: Vincent Fazio Date: Fri, 27 Dec 2024 13:49:30 -0600 Subject: [PATCH] make re patterns private --- tatsu/infos.py | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/tatsu/infos.py b/tatsu/infos.py index 6ec898ad..45a3eb4e 100644 --- a/tatsu/infos.py +++ b/tatsu/infos.py @@ -3,9 +3,10 @@ import copy import dataclasses import re -from collections.abc import Callable, Mapping +from collections.abc import Callable, MutableMapping from itertools import starmap from typing import Any, NamedTuple +from warnings import warn from .ast import AST from .tokenizing import Tokenizer @@ -30,8 +31,8 @@ class ParserConfig: start_rule: str | None = None # FIXME rule_name: str | None = None # Backward compatibility - comments_re: re.Pattern | None = None - eol_comments_re: re.Pattern | None = None + _comments_re: re.Pattern | None = dataclasses.field(default=None, init=False, repr=False) + _eol_comments_re: re.Pattern | None = dataclasses.field(default=None, init=False, repr=False) tokenizercls: type[Tokenizer] | None = None # FIXME semantics: type | None = None @@ -64,9 +65,19 @@ def __post_init__(self): # pylint: disable=W0235 if self.ignorecase: self.keywords = [k.upper() for k in self.keywords] if self.comments: - self.comments_re = re.compile(self.comments) + self._comments_re = re.compile(self.comments) if self.eol_comments: - self.eol_comments_re = re.compile(self.eol_comments) + self._eol_comments_re = re.compile(self.eol_comments) + + @property + def comments_re(self) -> re.Pattern: + warn(f"{self.__class__.name}.comments_re is deprecated", DeprecationWarning) + return self._comments_re + + @property + def eol_comments_re(self) -> re.Pattern: + warn(f"{self.__class__.name}.eol_comments_re is deprecated. Use ", DeprecationWarning) + return self._eol_comments_re @classmethod def new( @@ -84,7 +95,7 @@ def effective_rule_name(self): # note: there are legacy reasons for this mess return self.start_rule or self.rule_name or self.start - def _find_common(self, **settings: Any) -> Mapping[str, Any]: + def _find_common(self, **settings: Any) -> MutableMapping[str, Any]: return { name: value for name, value in settings.items() @@ -103,6 +114,11 @@ def replace_config( def replace(self, **settings: Any) -> ParserConfig: overrides = self._find_common(**settings) + for field in [ + field.name for field in dataclasses.fields(self) if field.init == False + ]: + if field in overrides.keys(): + overrides.pop(field) result = dataclasses.replace(self, **overrides) if 'grammar' in overrides: result.name = result.grammar