Skip to content

Commit 5b4e90e

Browse files
committed
Avoid mutation of objects, return a new one
1 parent 67ab1f9 commit 5b4e90e

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/pytest_bdd/parser.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import copy
34
import os.path
45
import re
56
import textwrap
@@ -218,7 +219,7 @@ def render(self, context: Mapping[str, Any]) -> Scenario:
218219
indent=step.indent,
219220
line_number=step.line_number,
220221
keyword=step.keyword,
221-
datatable=step.render_datatable(context),
222+
datatable=step.render_datatable(step.datatable, context) if step.datatable else None,
222223
docstring=render_string(step.docstring, context) if step.docstring else None,
223224
)
224225
for step in base_steps
@@ -329,24 +330,24 @@ def params(self) -> tuple[str, ...]:
329330
"""
330331
return tuple(frozenset(STEP_PARAM_RE.findall(self.name)))
331332

332-
def render_datatable(self, context: Mapping[str, Any]) -> DataTable | None:
333+
@staticmethod
334+
def render_datatable(datatable: DataTable, context: Mapping[str, object]) -> DataTable:
333335
"""
334336
Render the datatable with the given context,
335337
but avoid replacing text inside angle brackets if context is missing.
336338
337339
Args:
340+
datatable (DataTable): The datatable to render.
338341
context (Mapping[str, Any]): The context for rendering the datatable.
339342
340343
Returns:
341344
datatable (DataTable): The rendered datatable with parameters replaced only if they exist in the context.
342345
"""
343-
if self.datatable:
344-
rendered_datatable = self.datatable
345-
for row in rendered_datatable.rows:
346-
for cell in row.cells:
347-
cell.value = render_string(cell.value, context)
348-
return rendered_datatable
349-
return None
346+
rendered_datatable = copy.deepcopy(datatable)
347+
for row in rendered_datatable.rows:
348+
for cell in row.cells:
349+
cell.value = render_string(cell.value, context)
350+
return rendered_datatable
350351

351352

352353
@dataclass(eq=False)

0 commit comments

Comments
 (0)