Repository navigation
refactor: share message text constraints between send and edit requests - #43
Merged
Merged
Conversation
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 #18
What changed
app/schemas/api.py: new module-level aliasMessageText = Annotated[str, pydantic.Field(min_length=1, max_length=4000)].SendMessageRequest.textandEditMessageRequest.textboth use it, so the bounds1and4000are declared once inapp/.tests/test_schemas.py: tests parametrised over both request models. Each model rejects""and a 4001-character string and accepts a 4000-character string. If the two models drift apart later, one parametrised case fails.Why
The two request models declared the same
pydantic.Field(min_length=1, max_length=4000)independently. Changing one would have left the other alone, and no test would have noticed.A plain
Annotatedassignment is used instead of a PEP 695typealias. That keeps the field inlined in the generated OpenAPI schema exactly as before.Test-first evidence
This is a pure refactor, so a correct test cannot fail first. The new tests were written and run against the unchanged code: 7 passed. To check that the parametrisation catches drift, I temporarily changed only
EditMessageRequest.texttomax_length=5000and reran:FAILED tests/test_schemas.py::test_message_request_rejects_out_of_bounds_text[over_max-EditMessageRequest](DID NOT RAISE ValidationError), 1 failed and 6 passed. Then I reverted it and applied the refactor: 7 passed.Verification
just test: 116 passed, coverage 100.00%.just lint: ruff format, ruff check and ty check all clean.{'type': 'string', 'maxLength': 4000, 'minLength': 1}fortexton bothSendMessageRequestandEditMessageRequest, the same as before.