Skip to content

Commit 6676692

Browse files
committed
Use dataclasses and not attr
1 parent fec8270 commit 6676692

File tree

3 files changed

+81
-102
lines changed

3 files changed

+81
-102
lines changed

poetry.lock

Lines changed: 1 addition & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ pytest = ">=6.2.0"
4343
typing-extensions = "*"
4444
packaging = "*"
4545
gherkin-official = "^29.0.0"
46-
attrs = "^24.2.0"
4746

4847
[tool.poetry.group.dev.dependencies]
4948
tox = ">=4.11.3"

src/pytest_bdd/gherkin_parser.py

Lines changed: 80 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import linecache
22
import textwrap
3+
from dataclasses import dataclass, field
34
from typing import Any, Dict, List, Optional
45

5-
import attr
66
from gherkin.errors import CompositeParserException
77
from gherkin.parser import Parser
88
from gherkin.token_scanner import TokenScanner
@@ -11,41 +11,41 @@
1111
from .types import STEP_TYPES
1212

1313

14-
@attr.s
14+
@dataclass
1515
class Location:
16-
column = attr.ib(type=int)
17-
line = attr.ib(type=int)
16+
column: int
17+
line: int
1818

1919
@classmethod
2020
def from_dict(cls, data: Dict[str, Any]) -> "Location":
2121
return cls(column=data["column"], line=data["line"])
2222

2323

24-
@attr.s
24+
@dataclass
2525
class Comment:
26-
location = attr.ib(type=Location)
27-
text = attr.ib(type=str)
26+
location: Location
27+
text: str
2828

2929
@classmethod
3030
def from_dict(cls, data: Dict[str, Any]) -> "Comment":
3131
return cls(location=Location.from_dict(data["location"]), text=data["text"])
3232

3333

34-
@attr.s
34+
@dataclass
3535
class Cell:
36-
location = attr.ib(type=Location)
37-
value = attr.ib(type=str)
36+
location: Location
37+
value: str
3838

3939
@classmethod
4040
def from_dict(cls, data: Dict[str, Any]) -> "Cell":
4141
return cls(location=Location.from_dict(data["location"]), value=_convert_to_raw_string(data["value"]))
4242

4343

44-
@attr.s
44+
@dataclass
4545
class Row:
46-
id = attr.ib(type=str)
47-
location = attr.ib(type=Location)
48-
cells = attr.ib(type=List[Cell])
46+
id: str
47+
location: Location
48+
cells: List[Cell]
4949

5050
@classmethod
5151
def from_dict(cls, data: Dict[str, Any]) -> "Row":
@@ -56,12 +56,12 @@ def from_dict(cls, data: Dict[str, Any]) -> "Row":
5656
)
5757

5858

59-
@attr.s
59+
@dataclass
6060
class DataTable:
61-
location = attr.ib(type=Location)
62-
name = attr.ib(type=Optional[str], default=None)
63-
tableHeader = attr.ib(type=Optional[Row], default=None)
64-
tableBody = attr.ib(type=Optional[List[Row]], factory=list)
61+
location: Location
62+
name: Optional[str] = None
63+
tableHeader: Optional[Row] = None
64+
tableBody: Optional[List[Row]] = field(default_factory=list)
6565

6666
@classmethod
6767
def from_dict(cls, data: Dict[str, Any]) -> "DataTable":
@@ -73,31 +73,31 @@ def from_dict(cls, data: Dict[str, Any]) -> "DataTable":
7373
)
7474

7575

76-
@attr.s
76+
@dataclass
7777
class DocString:
78-
content = attr.ib(type=str)
79-
delimiter = attr.ib(type=str)
80-
location = attr.ib(type=Location)
78+
content: str
79+
delimiter: str
80+
location: Location
8181

82-
def __attrs_post_init__(self):
82+
def __post_init__(self):
8383
self.content = textwrap.dedent(self.content)
8484

8585
@classmethod
8686
def from_dict(cls, data: Dict[str, Any]) -> "DocString":
8787
return cls(content=data["content"], delimiter=data["delimiter"], location=Location.from_dict(data["location"]))
8888

8989

90-
@attr.s
90+
@dataclass
9191
class Step:
92-
id = attr.ib(type=str)
93-
keyword = attr.ib(type=str)
94-
keywordType = attr.ib(type=str)
95-
location = attr.ib(type=Location)
96-
text = attr.ib(type=str)
97-
dataTable = attr.ib(type=Optional[DataTable], default=None)
98-
docString = attr.ib(type=Optional[DocString], default=None)
99-
100-
def __attrs_post_init__(self):
92+
id: str
93+
keyword: str
94+
keywordType: str
95+
location: Location
96+
text: str
97+
dataTable: Optional[DataTable] = None
98+
docString: Optional[DocString] = None
99+
100+
def __post_init__(self):
101101
self.keyword = self.keyword.lower().strip()
102102

103103
@property
@@ -121,29 +121,29 @@ def from_dict(cls, data: Dict[str, Any]) -> "Step":
121121
)
122122

123123

124-
@attr.s
124+
@dataclass
125125
class Tag:
126-
id = attr.ib(type=str)
127-
location = attr.ib(type=Location)
128-
name = attr.ib(type=str)
126+
id: str
127+
location: Location
128+
name: str
129129

130130
@classmethod
131131
def from_dict(cls, data: Dict[str, Any]) -> "Tag":
132132
return cls(id=data["id"], location=Location.from_dict(data["location"]), name=data["name"])
133133

134134

135-
@attr.s
135+
@dataclass
136136
class Scenario:
137-
id = attr.ib(type=str)
138-
keyword = attr.ib(type=str)
139-
location = attr.ib(type=Location)
140-
name = attr.ib(type=str)
141-
description = attr.ib(type=str)
142-
steps = attr.ib(type=List[Step])
143-
tags = attr.ib(type=List[Tag])
144-
examples = attr.ib(type=Optional[List[DataTable]], factory=list)
145-
146-
def __attrs_post_init__(self):
137+
id: str
138+
keyword: str
139+
location: Location
140+
name: str
141+
description: str
142+
steps: List[Step]
143+
tags: List[Tag]
144+
examples: Optional[List[DataTable]] = field(default_factory=list)
145+
146+
def __post_init__(self):
147147
self.steps = _compute_given_when_then(self.steps)
148148

149149
@classmethod
@@ -160,15 +160,15 @@ def from_dict(cls, data: Dict[str, Any]) -> "Scenario":
160160
)
161161

162162

163-
@attr.s
163+
@dataclass
164164
class Rule:
165-
id = attr.ib(type=str)
166-
keyword = attr.ib(type=str)
167-
location = attr.ib(type=Location)
168-
name = attr.ib(type=str)
169-
description = attr.ib(type=str)
170-
tags = attr.ib(type=List[Tag])
171-
children = attr.ib(type=List[Scenario])
165+
id: str
166+
keyword: str
167+
location: Location
168+
name: str
169+
description: str
170+
tags: List[Tag]
171+
children: List[Scenario]
172172

173173
@classmethod
174174
def from_dict(cls, data: Dict[str, Any]) -> "Rule":
@@ -183,16 +183,16 @@ def from_dict(cls, data: Dict[str, Any]) -> "Rule":
183183
)
184184

185185

186-
@attr.s
186+
@dataclass
187187
class Background:
188-
id = attr.ib(type=str)
189-
keyword = attr.ib(type=str)
190-
location = attr.ib(type=Location)
191-
name = attr.ib(type=str)
192-
description = attr.ib(type=str)
193-
steps = attr.ib(type=List[Step])
194-
195-
def __attrs_post_init__(self):
188+
id: str
189+
keyword: str
190+
location: Location
191+
name: str
192+
description: str
193+
steps: List[Step]
194+
195+
def __post_init__(self):
196196
self.steps = _compute_given_when_then(self.steps)
197197

198198
@classmethod
@@ -207,11 +207,11 @@ def from_dict(cls, data: Dict[str, Any]) -> "Background":
207207
)
208208

209209

210-
@attr.s
210+
@dataclass
211211
class Child:
212-
background = attr.ib(type=Optional[Background], default=None)
213-
rule = attr.ib(type=Optional[Rule], default=None)
214-
scenario = attr.ib(type=Optional[Scenario], default=None)
212+
background: Optional[Background] = None
213+
rule: Optional[Rule] = None
214+
scenario: Optional[Scenario] = None
215215

216216
@classmethod
217217
def from_dict(cls, data: Dict[str, Any]) -> "Child":
@@ -222,14 +222,14 @@ def from_dict(cls, data: Dict[str, Any]) -> "Child":
222222
)
223223

224224

225-
@attr.s
225+
@dataclass
226226
class Feature:
227-
keyword = attr.ib(type=str)
228-
location = attr.ib(type=Location)
229-
tags = attr.ib(type=List[Tag])
230-
name = attr.ib(type=str)
231-
description = attr.ib(type=str)
232-
children = attr.ib(type=List[Child])
227+
keyword: str
228+
location: Location
229+
tags: List[Tag]
230+
name: str
231+
description: str
232+
children: List[Child]
233233

234234
@classmethod
235235
def from_dict(cls, data: Dict[str, Any]) -> "Feature":
@@ -243,10 +243,10 @@ def from_dict(cls, data: Dict[str, Any]) -> "Feature":
243243
)
244244

245245

246-
@attr.s
246+
@dataclass
247247
class GherkinDocument:
248-
feature = attr.ib(type=Feature)
249-
comments = attr.ib(type=List[Comment])
248+
feature: Feature
249+
comments: List[Comment]
250250

251251
@classmethod
252252
def from_dict(cls, data: Dict[str, Any]) -> "GherkinDocument":
@@ -283,5 +283,4 @@ def get_gherkin_document(abs_filename: str = None, encoding: str = "utf-8") -> G
283283
abs_filename,
284284
) from e
285285

286-
# Assuming gherkin_data is a dictionary with the structure expected by from_dict methods
287286
return GherkinDocument.from_dict(gherkin_data)

0 commit comments

Comments
 (0)