refactor: use enums instead of string comparison across the codebase - #689
Open
Santhu32144 wants to merge 1 commit into
Open
Santhu32144 wants to merge 1 commit into
Santhu32144 wants to merge 1 commit into
Conversation
Status values were compared and assigned as bare strings across routes, services, workers, tasks and schemas, so a typo like "complete" passed unchecked. Replace them with the enums in app/api/schemas/enums.py. Add JobType, HealthState and IncidentSort to contracts/schemas/enums.yaml and point the inline copies in common.yaml, system.yaml and path/incidents.yaml at them, so each set of values is defined once. Literal status fields now use enum members; the generated JSON schema is unchanged. Job.status and Job.job_type keep their str column type, only their defaults change, so no migration is needed. Add a test that every enum in enums.yaml matches enums.py: the generator's sync check only covers the enums the incident contract pulls in, which left health, job type and sort order unguarded.
This branch has not been deployed
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.
Closes #684
What
app/api/schemas/enums.pyacross routes, services, workers, tasks, and schemas. This includes job status/type, extraction status, batch status, health status, and incident sort order.JobType,HealthState, andIncidentSorttocontracts/schemas/enums.yaml.common.yaml,system.yaml, andpath/incidents.yamlto reference those enums instead of defining the same values again. This keeps each set of values in one place.Literal["..."]status fields to use enum members, for exampleLiteral[ExtractionStatus.completed]. The generated JSON schema remains the same.enums.yamlalso exists inenums.py. The existing generator sync check only covers the enums used by the incident contract, so health, job type, and incident sort were not covered before.Wire format
All of these are
strenums, so the response values are unchanged. Existing tests that check the JSON values continue to pass without any changes.The generated OpenAPI schema is only more precise in a few places:
statusnow referencesHealthState.ExtractionJobResponse.job_typenow referencesJobType.sortquery parameter now referencesIncidentSortinstead of using a regex pattern.An invalid/unknown sort value still returns
422.Job.statusandJob.job_typestill usestras their database column type. Only the Python-side defaults were changed, so no database migration is required.Notes
On Python 3.11, using a
strenum inside an f-string or%slog call displays it asJobStatus.failedinstead of justfailed. I checked the changed code and none of the replaced values are used in those situations.I intentionally left
"missing"in the extraction worker's result summary unchanged because it is not an extraction status.I also left the enums that currently exist only in
enums.pyand are not yet present inenums.yaml:InputTypeDetectionStatusTemplateStatusTextAlignTemplateFieldTypeThese can be added to the contract in a follow-up if needed.
Testing
pytest tests/— 559 passed (544 before + 15 new tests)python3 scripts/generate_contract_models.py— sync check passes andincident_contract.pyis unchangedruff checkon the changed files — clean