feat: generate Literal type aliases instead of StrEnum classes#759
Draft
feat: generate Literal type aliases instead of StrEnum classes#759
Conversation
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>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #759 +/- ##
==========================================
- Coverage 95.66% 95.41% -0.26%
==========================================
Files 48 49 +1
Lines 5218 4779 -439
==========================================
- Hits 4992 4560 -432
+ Misses 226 219 -7
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
…_case API values Split the 11 generated `Literal` type aliases out of `_models_generated.py` into a new `_literals_generated.py` so downstream code can depend on the literals without pulling in every Pydantic model. Rename the hand-maintained `_types.py` to `_literals.py` to match. Convert camelCase literal values (`'ownedByMe'`, `'sharedWithMe'`) to snake_case and emit a wire-format mapping so the API still receives the camelCase form it expects. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
`X = Literal[...]` is already recognized as a type alias by every Python type checker, so the `: TypeAlias` annotation adds noise without adding any semantic value. Drop it from the postprocess-generated aliases and from the hand-maintained `TerminalActorJobStatus`. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #576.
Rewrites every generated
class X(StrEnum)in_models_generated.pyinto a reusableX: TypeAlias = Literal[...]. Users can now pass plain strings (status='RUNNING') instead of importing enum members, and the aliases can be reused across resource-client method signatures — matching how Crawlee and the Apify SDK already model these values, and avoiding footguns like #575 where mixing enum and string values caused subtle issues.Approach
convert_enums_to_literalsstep inscripts/postprocess_generated_models.py, slotted betweendeduplicate_error_type_enumandadd_docs_group_decorators. AST-based: finds every top-levelClassDefwhose bases includeStrEnum, extracts string values in declaration order, replaces the class block withName: TypeAlias = Literal[...]. Docstrings are preserved as trailing bare-strings.TypeAliasintofrom typing import ...if missing.from enum import StrEnumbecomes unused and is removed by the existingruff check --fixpass._collapse_blank_lines(was duplicated in 4 places) and_ensure_typing_import(was duplicated in 2 places).ActorJobStatus,ActorPermissionLevel,ErrorType,GeneralAccess,HttpMethod,RunOrigin,SourceCodeFileFormat,StorageOwnership,VersionSourceType,WebhookDispatchStatus,WebhookEventType._consts.py,_status_message_watcher.py,_resource_clients/actor.py) and tests that previously accessed enum members (.MEMBER/.MEMBER.value) to use the string literal values directly._resource_clients/actor_version*.pyupdated to reference'SOURCE_FILES'etc. rather thanVersionSourceType.SOURCE_FILES.Breaking change
This is a deliberate breaking change tracked under the v3 milestone on #576.
ActorJobStatus.RUNNINGand similar enum-member access no longer exists — pass the plain string instead.