-
-
Notifications
You must be signed in to change notification settings - Fork 449
Fix pydantic constraint rendering #3289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
92db9d4
Fix pydantic constraint rendering
koxudaxi d5d2773
Address pydantic review comments
koxudaxi 9882d92
Address pydantic CodeQL import
koxudaxi de5c8ee
Fix regex literal string handling
koxudaxi 70dda57
Fix constrained value imports
koxudaxi e427a31
Update protobuf non-finite expectations
koxudaxi 54bc4f9
Handle black 23 decimal expectations
koxudaxi 916a1ae
Handle decimal return analysis
koxudaxi 4fcd19f
Fix constrained union builtin formatting
koxudaxi f05c37c
Cover literal constraint helpers
koxudaxi 39c7779
Keep non-integer constraint values for unrecognized numeric types
koxudaxi fb9c309
Use e2e golden tests for constraint rendering fixes
koxudaxi ad204e4
Merge origin/main into fix-pydantic-constraint-rendering
koxudaxi ac3494b
Keep strongest bound when normalized constraints collide
koxudaxi 7702501
Merge origin/main into fix-pydantic-constraint-rendering
koxudaxi 318897b
Use match statements for literal rendering and constraint merging
koxudaxi c336673
Return fallback values after match blocks
koxudaxi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| """Helpers for rendering Python source literals.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from math import isfinite, isnan | ||
| from typing import Any | ||
|
|
||
| from typing_extensions import Self | ||
|
|
||
|
|
||
| class PythonCode(str): # noqa: FURB189 - must behave as str for regex consumers. | ||
| """Python expression rendered without extra quoting.""" | ||
|
|
||
| code: str | ||
| __slots__ = ("code",) | ||
|
|
||
| def __new__(cls, code: str, value: str | None = None) -> Self: | ||
| """Initialize with a raw Python expression and optional string value.""" | ||
| obj = super().__new__(cls, code if value is None else value) | ||
| obj.code = code | ||
| return obj | ||
|
|
||
| def __repr__(self) -> str: | ||
| """Render the wrapped expression.""" | ||
| return self.code | ||
|
|
||
|
|
||
| def represent_python_value(value: Any) -> str: # noqa: PLR0911 | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| """Render a value as a Python expression safe for generated source.""" | ||
| match value: | ||
| case PythonCode(): | ||
| return value.code | ||
| case float() if isnan(value): | ||
| return "float('nan')" | ||
| case float() if not isfinite(value): | ||
| return "float('inf')" if value > 0 else "float('-inf')" | ||
| case dict(): | ||
| rendered_items = ", ".join( | ||
| f"{represent_python_value(key)}: {represent_python_value(item)}" for key, item in value.items() | ||
| ) | ||
| return f"{{{rendered_items}}}" | ||
| case list(): | ||
| return "[" + ", ".join(represent_python_value(item) for item in value) + "]" | ||
| case tuple(): | ||
| rendered_items = ", ".join(represent_python_value(item) for item in value) | ||
| trailing_comma = "," if len(value) == 1 else "" | ||
| return f"({rendered_items}{trailing_comma})" | ||
| case set() if not value: | ||
| return "set()" | ||
| case set(): | ||
| sorted_items = sorted(value, key=lambda item: (type(item).__name__, repr(item))) | ||
| return "{" + ", ".join(represent_python_value(item) for item in sorted_items) + "}" | ||
| return repr(value) | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.