Skip to content

Commit 23208fc

Browse files
author
Raphael Krupinski
committed
πŸ§‘β€πŸ’» Simplify Stack by accepting string vararg.
1 parent fe9612a commit 23208fc

File tree

3 files changed

+12
-16
lines changed

3 files changed

+12
-16
lines changed

β€Žsrc/lapidary/render/model/openapi_conv.pyβ€Ž

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def process_parameter(self, value: openapi.Parameter, stack: Stack) -> python.Pa
9898
media_type, media_type_obj = next(iter(value.content.items()))
9999
# encoding = media_type_obj.encoding
100100
typ = self.schema_converter.process_schema(
101-
media_type_obj.schema_, stack.push_all('content', media_type), value.required
101+
media_type_obj.schema_, stack.push('content', media_type), value.required
102102
)
103103
else:
104104
raise TypeError(f'{stack}: schema or content is required')
@@ -125,7 +125,8 @@ def process_path(
125125
) -> None:
126126
common_params_stack = stack.push('parameters')
127127
common_params = [
128-
self.process_parameter(param, common_params_stack.push(idx)) for idx, param in enumerate(value.parameters)
128+
self.process_parameter(param, common_params_stack.push(str(idx)))
129+
for idx, param in enumerate(value.parameters)
129130
]
130131

131132
for method, operation in value.model_extra.items():
@@ -153,7 +154,7 @@ def process_content(self, value: Mapping[str, openapi.MediaType], stack: Stack)
153154
mime_parsed = parse_media_range(mime)
154155
if mime_parsed[:2] != ('application', 'json'):
155156
continue
156-
types[mime] = self.schema_converter.process_schema(media_type.schema_, stack.push_all(mime, 'schema'))
157+
types[mime] = self.schema_converter.process_schema(media_type.schema_, stack.push(mime, 'schema'))
157158
return types
158159

159160
def process_operation(
@@ -178,7 +179,7 @@ def process_operation(
178179
model = python.OperationFunction(
179180
name=value.operationId,
180181
method=stack.top(),
181-
path=cast(str, stack[-2]),
182+
path=json_pointer.decode_json_pointer(stack[-2]),
182183
request_body=request_body,
183184
params=params,
184185
responses=responses,
@@ -197,7 +198,7 @@ def _mk_params(
197198
for param in common_params:
198199
params[param.name] = param
199200
for idx, oa_param in enumerate(value):
200-
param = self.process_parameter(oa_param, stack.push(idx))
201+
param = self.process_parameter(oa_param, stack.push(str(idx)))
201202
params[param.name] = param
202203
return list(params.values())
203204

@@ -208,7 +209,7 @@ def process_security(
208209
if value is None:
209210
return None
210211

211-
security = [self.process_security_requirement(item, stack.push(idx)) for idx, item in enumerate(value)]
212+
security = [self.process_security_requirement(item, stack.push(str(idx))) for idx, item in enumerate(value)]
212213
return security or None
213214

214215
def process_security_requirement(
@@ -252,7 +253,7 @@ def _(self, value: openapi.OAuth2SecurityScheme, stack: Stack) -> None:
252253
flow = value.flows.implicit
253254

254255
if flow.refreshUrl:
255-
raise NotImplementedError(stack.push_all('flows', 'implicit', 'refreshUrl'))
256+
raise NotImplementedError(stack.push('flows', 'implicit', 'refreshUrl'))
256257

257258
self.target.security_schemes[flow_name] = python.ImplicitOAuth2Flow(
258259
name=auth_name,

β€Žsrc/lapidary/render/model/schema.pyβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def _get_one_of_type_hint(
125125
one_of: Iterable[openapi.Reference[openapi.Schema] | openapi.Schema],
126126
) -> python.TypeHint:
127127
return python.GenericTypeHint.union_of(
128-
*tuple(self.process_schema(sub_schema, stack.push(idx)) for idx, sub_schema in enumerate(one_of))
128+
*tuple(self.process_schema(sub_schema, stack.push(str(idx))) for idx, sub_schema in enumerate(one_of))
129129
)
130130

131131
def _get_composite_type_hint(

β€Žsrc/lapidary/render/model/stack.pyβ€Ž

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
from functools import lru_cache
22
from typing import Self
33

4-
from ..json_pointer import encode_json_pointer
5-
64

75
class Stack:
86
__slots__ = ('path',)
@@ -16,12 +14,9 @@ def from_str(cls, pointer: str) -> Self:
1614

1715
@lru_cache(1)
1816
def __repr__(self):
19-
return '/'.join(encode_json_pointer(str(elem)) for elem in self.path)
20-
21-
def push(self, name: str | int) -> Self:
22-
return Stack(self.path + (name,))
17+
return '/'.join(self.path)
2318

24-
def push_all(self, *names: str | int) -> Self:
19+
def push(self, *names: str) -> Self:
2520
return Stack(self.path + names)
2621

2722
def top(self) -> str:
@@ -33,5 +28,5 @@ def __hash__(self) -> int:
3328
def __eq__(self, other):
3429
return isinstance(other, Stack) and self.path == other.path
3530

36-
def __getitem__(self, item: int) -> str | int:
31+
def __getitem__(self, item: int) -> str:
3732
return self.path[item]

0 commit comments

Comments
Β (0)