Skip to content

Commit 674a0c2

Browse files
committed
Make the id and location attributes private in the gherkin parser so that they are not part of the public API if we share the objects from the parser (as we do now with the DataTable)
1 parent c954f43 commit 674a0c2

File tree

3 files changed

+237
-237
lines changed

3 files changed

+237
-237
lines changed

src/pytest_bdd/gherkin_parser.py

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -67,50 +67,50 @@ def from_dict(cls, data: dict[str, Any]) -> Self:
6767

6868
@dataclass
6969
class Comment:
70-
location: Location
70+
_location: Location
7171
text: str
7272

7373
@classmethod
7474
def from_dict(cls, data: dict[str, Any]) -> Self:
75-
return cls(location=Location.from_dict(data["location"]), text=data["text"])
75+
return cls(_location=Location.from_dict(data["location"]), text=data["text"])
7676

7777

7878
@dataclass
7979
class Cell:
80-
location: Location
80+
_location: Location
8181
value: str
8282

8383
@classmethod
8484
def from_dict(cls, data: dict[str, Any]) -> Self:
85-
return cls(location=Location.from_dict(data["location"]), value=_to_raw_string(data["value"]))
85+
return cls(_location=Location.from_dict(data["location"]), value=_to_raw_string(data["value"]))
8686

8787

8888
@dataclass
8989
class Row:
90-
id: str
91-
location: Location
90+
_id: str
91+
_location: Location
9292
cells: list[Cell]
9393

9494
@classmethod
9595
def from_dict(cls, data: dict[str, Any]) -> Self:
9696
return cls(
97-
id=data["id"],
98-
location=Location.from_dict(data["location"]),
97+
_id=data["id"],
98+
_location=Location.from_dict(data["location"]),
9999
cells=[Cell.from_dict(cell) for cell in data["cells"]],
100100
)
101101

102102

103103
@dataclass
104104
class ExamplesTable:
105-
location: Location
105+
_location: Location
106106
name: str | None = None
107107
table_header: Row | None = None
108108
table_body: list[Row] | None = field(default_factory=list)
109109

110110
@classmethod
111111
def from_dict(cls, data: dict[str, Any]) -> Self:
112112
return cls(
113-
location=Location.from_dict(data["location"]),
113+
_location=Location.from_dict(data["location"]),
114114
name=data.get("name"),
115115
table_header=Row.from_dict(data["tableHeader"]) if data.get("tableHeader") else None,
116116
table_body=[Row.from_dict(row) for row in data.get("tableBody", [])],
@@ -119,13 +119,13 @@ def from_dict(cls, data: dict[str, Any]) -> Self:
119119

120120
@dataclass
121121
class DataTable:
122-
location: Location
122+
_location: Location
123123
rows: list[Row]
124124

125125
@classmethod
126126
def from_dict(cls, data: dict[str, Any]) -> Self:
127127
return cls(
128-
location=Location.from_dict(data["location"]), rows=[Row.from_dict(row) for row in data.get("rows", [])]
128+
_location=Location.from_dict(data["location"]), rows=[Row.from_dict(row) for row in data.get("rows", [])]
129129
)
130130

131131
def transpose(self) -> DataTable:
@@ -134,11 +134,11 @@ def transpose(self) -> DataTable:
134134

135135
# Use zip to transpose the matrix and create transposed rows
136136
transposed_cells = [
137-
Row(id=str(i), location=self.location, cells=list(col)) for i, col in enumerate(zip(*cells_matrix))
137+
Row(_id=str(i), _location=self._location, cells=list(col)) for i, col in enumerate(zip(*cells_matrix))
138138
]
139139

140140
# Return a new DataTable with transposed rows
141-
return DataTable(location=self.location, rows=transposed_cells)
141+
return DataTable(_location=self._location, rows=transposed_cells)
142142

143143
def to_dict(self) -> dict[str, list[str]]:
144144
# Ensure there are at least two rows: one for the header and one for the values
@@ -157,34 +157,34 @@ def to_dict(self) -> dict[str, list[str]]:
157157
class DocString:
158158
content: str
159159
delimiter: str
160-
location: Location
160+
_location: Location
161161

162162
@classmethod
163163
def from_dict(cls, data: dict[str, Any]) -> Self:
164164
return cls(
165165
content=textwrap.dedent(data["content"]),
166166
delimiter=data["delimiter"],
167-
location=Location.from_dict(data["location"]),
167+
_location=Location.from_dict(data["location"]),
168168
)
169169

170170

171171
@dataclass
172172
class Step:
173-
id: str
173+
_id: str
174+
_location: Location
174175
keyword: str
175176
keyword_type: str
176-
location: Location
177177
text: str
178178
data_table: DataTable | None = None
179179
doc_string: DocString | None = None
180180

181181
@classmethod
182182
def from_dict(cls, data: dict[str, Any]) -> Self:
183183
return cls(
184-
id=data["id"],
184+
_id=data["id"],
185+
_location=Location.from_dict(data["location"]),
185186
keyword=data["keyword"].strip(),
186187
keyword_type=data["keywordType"],
187-
location=Location.from_dict(data["location"]),
188188
text=data["text"],
189189
data_table=DataTable.from_dict(data["dataTable"]) if data.get("dataTable") else None,
190190
doc_string=DocString.from_dict(data["docString"]) if data.get("docString") else None,
@@ -193,20 +193,20 @@ def from_dict(cls, data: dict[str, Any]) -> Self:
193193

194194
@dataclass
195195
class Tag:
196-
id: str
197-
location: Location
196+
_id: str
197+
_location: Location
198198
name: str
199199

200200
@classmethod
201201
def from_dict(cls, data: dict[str, Any]) -> Self:
202-
return cls(id=data["id"], location=Location.from_dict(data["location"]), name=data["name"])
202+
return cls(_id=data["id"], _location=Location.from_dict(data["location"]), name=data["name"])
203203

204204

205205
@dataclass
206206
class Scenario:
207-
id: str
207+
_id: str
208+
_location: Location
208209
keyword: str
209-
location: Location
210210
name: str
211211
description: str
212212
steps: list[Step]
@@ -216,9 +216,9 @@ class Scenario:
216216
@classmethod
217217
def from_dict(cls, data: dict[str, Any]) -> Self:
218218
return cls(
219-
id=data["id"],
219+
_id=data["id"],
220+
_location=Location.from_dict(data["location"]),
220221
keyword=data["keyword"],
221-
location=Location.from_dict(data["location"]),
222222
name=data["name"],
223223
description=data["description"],
224224
steps=[Step.from_dict(step) for step in data["steps"]],
@@ -229,9 +229,9 @@ def from_dict(cls, data: dict[str, Any]) -> Self:
229229

230230
@dataclass
231231
class Rule:
232-
id: str
232+
_id: str
233+
_location: Location
233234
keyword: str
234-
location: Location
235235
name: str
236236
description: str
237237
tags: list[Tag]
@@ -240,9 +240,9 @@ class Rule:
240240
@classmethod
241241
def from_dict(cls, data: dict[str, Any]) -> Self:
242242
return cls(
243-
id=data["id"],
243+
_id=data["id"],
244+
_location=Location.from_dict(data["location"]),
244245
keyword=data["keyword"],
245-
location=Location.from_dict(data["location"]),
246246
name=data["name"],
247247
description=data["description"],
248248
tags=[Tag.from_dict(tag) for tag in data["tags"]],
@@ -252,19 +252,19 @@ def from_dict(cls, data: dict[str, Any]) -> Self:
252252

253253
@dataclass
254254
class Background:
255-
id: str
255+
_id: str
256+
_location: Location
256257
keyword: str
257-
location: Location
258258
name: str
259259
description: str
260260
steps: list[Step]
261261

262262
@classmethod
263263
def from_dict(cls, data: dict[str, Any]) -> Self:
264264
return cls(
265-
id=data["id"],
265+
_id=data["id"],
266+
_location=Location.from_dict(data["location"]),
266267
keyword=data["keyword"],
267-
location=Location.from_dict(data["location"]),
268268
name=data["name"],
269269
description=data["description"],
270270
steps=[Step.from_dict(step) for step in data["steps"]],
@@ -288,8 +288,8 @@ def from_dict(cls, data: dict[str, Any]) -> Self:
288288

289289
@dataclass
290290
class Feature:
291+
_location: Location
291292
keyword: str
292-
location: Location
293293
tags: list[Tag]
294294
name: str
295295
description: str
@@ -298,8 +298,8 @@ class Feature:
298298
@classmethod
299299
def from_dict(cls, data: dict[str, Any]) -> Self:
300300
return cls(
301+
_location=Location.from_dict(data["location"]),
301302
keyword=data["keyword"],
302-
location=Location.from_dict(data["location"]),
303303
tags=[Tag.from_dict(tag) for tag in data["tags"]],
304304
name=data["name"],
305305
description=data["description"],

src/pytest_bdd/parser.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ def get_step_content(_gherkin_step: GherkinStep) -> str:
359359
if first_step.keyword.lower() not in STEP_TYPES:
360360
raise StepError(
361361
message=f"First step in a scenario or background must start with 'Given', 'When' or 'Then', but got {first_step.keyword}.",
362-
line=first_step.location.line,
362+
line=first_step._location.line,
363363
line_content=get_step_content(first_step),
364364
filename=self.abs_filename,
365365
)
@@ -375,8 +375,8 @@ def get_step_content(_gherkin_step: GherkinStep) -> str:
375375
Step(
376376
name=name,
377377
type=current_type,
378-
indent=step.location.column - 1,
379-
line_number=step.location.line,
378+
indent=step._location.column - 1,
379+
line_number=step._location.line,
380380
keyword=step.keyword.title(),
381381
data_table=step.data_table,
382382
)
@@ -397,7 +397,7 @@ def parse_scenario(self, scenario_data: GherkinScenario, feature: Feature) -> Sc
397397
scenario = ScenarioTemplate(
398398
feature=feature,
399399
name=strip_comments(scenario_data.name),
400-
line_number=scenario_data.location.line,
400+
line_number=scenario_data._location.line,
401401
templated=templated,
402402
tags=self.get_tag_names(scenario_data.tags),
403403
description=textwrap.dedent(scenario_data.description),
@@ -407,7 +407,7 @@ def parse_scenario(self, scenario_data: GherkinScenario, feature: Feature) -> Sc
407407

408408
for example_data in scenario_data.examples:
409409
examples = Examples(
410-
line_number=example_data.location.line,
410+
line_number=example_data._location.line,
411411
name=example_data.name,
412412
)
413413
if example_data.table_header is not None:
@@ -424,7 +424,7 @@ def parse_scenario(self, scenario_data: GherkinScenario, feature: Feature) -> Sc
424424
def parse_background(self, background_data: GherkinBackground, feature: Feature) -> Background:
425425
background = Background(
426426
feature=feature,
427-
line_number=background_data.location.line,
427+
line_number=background_data._location.line,
428428
)
429429
background.steps = self.parse_steps(background_data.steps)
430430
for step in background.steps:
@@ -449,7 +449,7 @@ def parse(self) -> Feature:
449449
name=strip_comments(feature_data.name),
450450
tags=self.get_tag_names(feature_data.tags),
451451
background=None,
452-
line_number=feature_data.location.line,
452+
line_number=feature_data._location.line,
453453
description=textwrap.dedent(feature_data.description),
454454
)
455455

0 commit comments

Comments
 (0)