Skip to content

Commit 66da403

Browse files
vdusekclaude
andcommitted
feat: generate Literal type aliases instead of StrEnum classes
Rewrite every generated `class X(StrEnum)` into a reusable `X: TypeAlias = Literal[...]` so users can pass plain strings and type aliases can be shared across resource-client method signatures. Closes #576. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 2f09830 commit 66da403

17 files changed

Lines changed: 792 additions & 587 deletions

‎scripts/postprocess_generated_models.py‎

Lines changed: 84 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
- Fix discriminator field names that use camelCase instead of snake_case (known issue with
55
discriminators on schemas referenced from array items).
66
- Deduplicate the inlined `Type(StrEnum)` that comes from ErrorResponse.yaml; rewire to `ErrorType`.
7+
- Rewrite every `class X(StrEnum)` as `X: TypeAlias = Literal[...]` so downstream code can pass
8+
plain strings (and reuse the named alias in resource-client signatures) instead of enum members.
79
- Add `@docs_group('Models')` to every model class (plus the required import).
810
911
Applied to `_typeddicts_generated.py`:
@@ -51,6 +53,27 @@
5153
)
5254

5355

56+
def _collapse_blank_lines(content: str) -> str:
57+
"""Collapse runs of 3+ blank lines down to exactly 3, leaving at most 2 blank lines between symbols."""
58+
return re.sub(r'\n{3,}', '\n\n\n', content)
59+
60+
61+
def _ensure_typing_import(content: str, name: str) -> str:
62+
"""Append `name` to the `from typing import ...` line if not already imported.
63+
64+
Assumes the single-line import form datamodel-codegen emits; ruff re-wraps afterwards.
65+
"""
66+
typing_import = re.search(r'from typing import[^\n]+', content)
67+
if typing_import is None or name in typing_import.group(0):
68+
return content
69+
return re.sub(
70+
r'(from typing import )([^\n]+)',
71+
lambda m: f'{m.group(1)}{m.group(2)}, {name}',
72+
content,
73+
count=1,
74+
)
75+
76+
5477
def fix_discriminators(content: str) -> str:
5578
"""Replace camelCase discriminator values with their snake_case equivalents."""
5679
for camel, snake in DISCRIMINATOR_FIXES.items():
@@ -73,8 +96,64 @@ def deduplicate_error_type_enum(content: str) -> str:
7396
)
7497
# Replace standalone `Type` references in annotation contexts (`: Type`, `| Type`, `[Type`).
7598
content = re.sub(r'(?<=: )Type\b|(?<=\| )Type\b|(?<=\[)Type\b', 'ErrorType', content)
76-
# Collapse triple+ blank lines left by the removal.
77-
return re.sub(r'\n{3,}', '\n\n\n', content)
99+
return _collapse_blank_lines(content)
100+
101+
102+
def convert_enums_to_literals(content: str) -> str:
103+
"""Rewrite every `class X(StrEnum): ...` into an `X: TypeAlias = Literal[...]` alias.
104+
105+
Each member assignment (`NAME = 'value'`) contributes its string value to the literal in
106+
declaration order. The class docstring, if present, is preserved as a trailing bare-string
107+
docstring after the alias — matching the field-doc convention datamodel-codegen already uses
108+
elsewhere in the generated file.
109+
110+
Runs before `add_docs_group_decorators`, so the enum classes have no `@docs_group` decorator
111+
to strip. The `from enum import StrEnum` import is left alone and removed by ruff's F401 fix.
112+
"""
113+
tree = ast.parse(content)
114+
lines = content.split('\n')
115+
replacements: list[tuple[int, int, list[str]]] = []
116+
117+
for node in tree.body:
118+
if not isinstance(node, ast.ClassDef):
119+
continue
120+
base_names = {b.id for b in node.bases if isinstance(b, ast.Name)}
121+
if 'StrEnum' not in base_names:
122+
continue
123+
124+
values: list[str] = [
125+
stmt.value.value
126+
for stmt in node.body
127+
if isinstance(stmt, ast.Assign)
128+
and len(stmt.targets) == 1
129+
and isinstance(stmt.targets[0], ast.Name)
130+
and isinstance(stmt.value, ast.Constant)
131+
and isinstance(stmt.value.value, str)
132+
]
133+
docstring = ast.get_docstring(node)
134+
135+
new_lines: list[str] = [f'{node.name}: TypeAlias = Literal[']
136+
new_lines.extend(f' {v!r},' for v in values)
137+
new_lines.append(']')
138+
if docstring is not None:
139+
if '\n' in docstring:
140+
new_lines.append('"""')
141+
new_lines.extend(docstring.splitlines())
142+
new_lines.append('"""')
143+
else:
144+
new_lines.append(f'"""{docstring}"""')
145+
146+
assert node.end_lineno is not None # noqa: S101
147+
replacements.append((node.lineno - 1, node.end_lineno, new_lines))
148+
149+
if not replacements:
150+
return content
151+
152+
# Replace in reverse order so earlier slice indices stay valid after each splice.
153+
for start, end, new in sorted(replacements, key=lambda r: r[0], reverse=True):
154+
lines[start:end] = new
155+
156+
return _ensure_typing_import(_collapse_blank_lines('\n'.join(lines)), 'TypeAlias')
78157

79158

80159
def add_docs_group_decorators(content: str, group_name: GroupName) -> str:
@@ -136,17 +215,7 @@ def flatten_empty_typeddicts(content: str) -> str:
136215
if not replaced:
137216
return content
138217

139-
output = re.sub(r'\n{3,}', '\n\n\n', '\n'.join(lines))
140-
# Flattening introduces new `TypeAlias` uses; make sure it's imported from typing.
141-
typing_import = re.search(r'from typing import[^\n]+', output)
142-
if typing_import is not None and 'TypeAlias' not in typing_import.group(0):
143-
output = re.sub(
144-
r'(from typing import )([^\n]+)',
145-
lambda m: f'{m.group(1)}{m.group(2)}, TypeAlias',
146-
output,
147-
count=1,
148-
)
149-
return output
218+
return _ensure_typing_import(_collapse_blank_lines('\n'.join(lines)), 'TypeAlias')
150219

151220

152221
def _is_string_expr(node: ast.stmt) -> bool:
@@ -237,10 +306,7 @@ def prune_typeddicts(content: str, seeds: frozenset[str]) -> tuple[str, set[str]
237306
drop_line_indices.add(line_no)
238307

239308
pruned = [line for i, line in enumerate(lines) if i not in drop_line_indices]
240-
output = '\n'.join(pruned)
241-
# Collapse runs of blank lines left behind by deletions.
242-
output = re.sub(r'\n{3,}', '\n\n\n', output)
243-
return output, kept
309+
return _collapse_blank_lines('\n'.join(pruned)), kept
244310

245311

246312
def rename_with_dict_suffix(content: str, names: set[str]) -> str:
@@ -258,6 +324,7 @@ def postprocess_models(path: Path) -> bool:
258324
original = path.read_text()
259325
fixed = fix_discriminators(original)
260326
fixed = deduplicate_error_type_enum(fixed)
327+
fixed = convert_enums_to_literals(fixed)
261328
fixed = add_docs_group_decorators(fixed, 'Models')
262329
if fixed == original:
263330
return False

‎src/apify_client/_consts.py‎

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

33
from datetime import timedelta
4+
from typing import TYPE_CHECKING
45

5-
from apify_client._models_generated import ActorJobStatus
6+
if TYPE_CHECKING:
7+
from apify_client._models_generated import ActorJobStatus
68

79
DEFAULT_API_URL = 'https://api.apify.com'
810
"""Default base URL for the Apify API."""
@@ -34,14 +36,7 @@
3436
DEFAULT_WAIT_WHEN_JOB_NOT_EXIST = timedelta(seconds=3)
3537
"""How long to wait for a job to exist before giving up."""
3638

37-
TERMINAL_STATUSES = frozenset(
38-
{
39-
ActorJobStatus.SUCCEEDED,
40-
ActorJobStatus.FAILED,
41-
ActorJobStatus.TIMED_OUT,
42-
ActorJobStatus.ABORTED,
43-
}
44-
)
39+
TERMINAL_STATUSES: frozenset[ActorJobStatus] = frozenset({'SUCCEEDED', 'FAILED', 'TIMED-OUT', 'ABORTED'})
4540
"""Set of terminal Actor job statuses that indicate the job has finished."""
4641

4742
OVERRIDABLE_DEFAULT_HEADERS = {'Accept', 'Authorization', 'Accept-Encoding', 'User-Agent'}

0 commit comments

Comments
 (0)