Skip to content

Commit c4a041d

Browse files
committed
use cached_property
1 parent d37913f commit c4a041d

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

src/pytest_bdd/parser.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import typing
77
from collections import OrderedDict
88
from dataclasses import dataclass, field
9+
from functools import cached_property
910
from typing import cast
1011

1112
from . import exceptions, types
@@ -292,7 +293,6 @@ class Scenario:
292293
class Step:
293294
type: str
294295
_name: str
295-
_full_name: str | None
296296
line_number: int
297297
indent: int
298298
keyword: str
@@ -312,37 +312,40 @@ def __init__(self, name: str, type: str, indent: int, line_number: int, keyword:
312312
self.scenario = None
313313
self.background = None
314314
self.lines = []
315-
self._full_name = None
316315

317316
def add_line(self, line: str) -> None:
318317
"""Add line to the multiple step.
319318
320319
:param str line: Line of text - the continuation of the step name.
321320
"""
322321
self.lines.append(line)
323-
self._full_name = None
322+
if "full_name" in self.__dict__:
323+
del self.__dict__["full_name"]
324+
325+
@cached_property
326+
def full_name(self) -> str:
327+
multilines_content = textwrap.dedent("\n".join(self.lines)) if self.lines else ""
328+
329+
# Remove the multiline quotes, if present.
330+
multilines_content = re.sub(
331+
pattern=r'^"""\n(?P<content>.*)\n"""$',
332+
repl=r"\g<content>",
333+
string=multilines_content,
334+
flags=re.DOTALL, # Needed to make the "." match also new lines
335+
)
336+
337+
lines = [self._name] + [multilines_content]
338+
return "\n".join(lines).strip()
324339

325340
@property
326341
def name(self) -> str:
327-
if self._full_name is None:
328-
multilines_content = textwrap.dedent("\n".join(self.lines)) if self.lines else ""
329-
330-
# Remove the multiline quotes, if present.
331-
multilines_content = re.sub(
332-
pattern=r'^"""\n(?P<content>.*)\n"""$',
333-
repl=r"\g<content>",
334-
string=multilines_content,
335-
flags=re.DOTALL, # Needed to make the "." match also new lines
336-
)
337-
338-
lines = [self._name] + [multilines_content]
339-
self._full_name = "\n".join(lines).strip()
340-
return self._full_name
342+
return self.full_name
341343

342344
@name.setter
343345
def name(self, value: str) -> None:
344346
self._name = value
345-
self._full_name = None
347+
if "full_name" in self.__dict__:
348+
del self.__dict__["full_name"]
346349

347350
def __str__(self) -> str:
348351
"""Full step name including the type."""

0 commit comments

Comments
 (0)