Skip to content

Commit

Permalink
Avoid mutation of objects, return a new one
Browse files Browse the repository at this point in the history
  • Loading branch information
youtux committed Nov 30, 2024
1 parent 67ab1f9 commit 5b4e90e
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/pytest_bdd/parser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import copy
import os.path
import re
import textwrap
Expand Down Expand Up @@ -218,7 +219,7 @@ def render(self, context: Mapping[str, Any]) -> Scenario:
indent=step.indent,
line_number=step.line_number,
keyword=step.keyword,
datatable=step.render_datatable(context),
datatable=step.render_datatable(step.datatable, context) if step.datatable else None,
docstring=render_string(step.docstring, context) if step.docstring else None,
)
for step in base_steps
Expand Down Expand Up @@ -329,24 +330,24 @@ def params(self) -> tuple[str, ...]:
"""
return tuple(frozenset(STEP_PARAM_RE.findall(self.name)))

def render_datatable(self, context: Mapping[str, Any]) -> DataTable | None:
@staticmethod
def render_datatable(datatable: DataTable, context: Mapping[str, object]) -> DataTable:
"""
Render the datatable with the given context,
but avoid replacing text inside angle brackets if context is missing.
Args:
datatable (DataTable): The datatable to render.
context (Mapping[str, Any]): The context for rendering the datatable.
Returns:
datatable (DataTable): The rendered datatable with parameters replaced only if they exist in the context.
"""
if self.datatable:
rendered_datatable = self.datatable
for row in rendered_datatable.rows:
for cell in row.cells:
cell.value = render_string(cell.value, context)
return rendered_datatable
return None
rendered_datatable = copy.deepcopy(datatable)
for row in rendered_datatable.rows:
for cell in row.cells:
cell.value = render_string(cell.value, context)
return rendered_datatable


@dataclass(eq=False)
Expand Down

0 comments on commit 5b4e90e

Please sign in to comment.