-
Notifications
You must be signed in to change notification settings - Fork 291
Add UUID validator #584
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
Add UUID validator #584
Conversation
CodSpeed Performance ReportMerging #584 Summary
Benchmarks breakdown
|
Before we get too far on this, it would be good to see the performance difference between this and the python implementation we currently have in pydantic. |
Codecov Report
📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more Additional details and impacted files@@ Coverage Diff @@
## main #584 +/- ##
==========================================
- Coverage 94.12% 93.71% -0.42%
==========================================
Files 96 99 +3
Lines 13021 13292 +271
Branches 24 24
==========================================
+ Hits 12256 12456 +200
- Misses 759 830 +71
Partials 6 6
... and 5 files with indirect coverage changes Continue to review full report in Codecov by Sentry.
|
I implemented some benchmarks in dba435a. Here's the results:
edit: In saying that, I can't see a type for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in principle looks good.
But we need a significant change to use the standard lib UUID type, not a new rust type.
def variant(self) -> str: ... | ||
@property | ||
def version(self) -> str: ... | ||
def __init__(self, uuid: str) -> None: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
__init__
should come first.
@@ -3503,6 +3503,38 @@ def multi_host_url_schema( | |||
) | |||
|
|||
|
|||
class UuidSchema(TypedDict, total=False): | |||
type: Required[Literal['uuid']] | |||
version: int |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm guessing this should be something like Literal[3, 4, 5]
- not sure exactly what's allowed, I guess that's kind of the point of using a Literal.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should at least have Literal[1, 3, 4, 5] | None
so that it's possible to not enforce checking of the version. There are cases, e.g. the Nil UUID or Max UUID, where it may be desirable to not enforce a version.
Note that Python doesn't support generating UUID v2 and nor does the rust uuid
crate, but perhaps that should still be included as a valid instance could still be instantiated for a v2 UUID. In addition there is an update in the works to introduce v6, v7 and v8. Not sure whether it's worth adding a comment that this might need looking into in the future? Interestingly the uuid
crate does already have preliminary support for generating them, even if Python does not.
``` | ||
|
||
Args: | ||
version: The UUID version number as per RFC 4122 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
say what it defaults to if ommitted.
@@ -3503,6 +3503,38 @@ def multi_host_url_schema( | |||
) | |||
|
|||
|
|||
class UuidSchema(TypedDict, total=False): | |||
type: Required[Literal['uuid']] | |||
version: int |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm guessing this should be something like Literal[3, 4, 5]
- not sure exactly what's allowed, I guess that's kind of the point of using a Literal.
@@ -533,6 +547,9 @@ impl ErrorType { | |||
Self::UrlSyntaxViolation {..} => "Input violated strict URL syntax rules, {error}", | |||
Self::UrlTooLong {..} => "URL should have at most {max_length} characters", | |||
Self::UrlScheme {..} => "URL scheme should be {expected_schemes}", | |||
Self::UuidType => "Input should be a string", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Self::UuidType => "Input should be a string", | |
Self::UuidType => "UUID Input should be a string or UUID object", |
or something, you'll also want a custom error message for JSON, see the function below.
|
||
static SCHEMA_DEFINITION_UUID: GILOnceCell<SchemaValidator> = GILOnceCell::new(); | ||
|
||
#[pyclass(name = "Uuid", module = "pydantic_core._pydantic_core")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we want a custom type here - the standard library has an existing UUID implementation that we should leverage.
} | ||
} | ||
|
||
impl TypeSerializer for UuidSerializer { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure you need this, the existing ToStringSerializer
should probably surfice.
} | ||
} | ||
|
||
impl TypeSerializer for UuidSerializer { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure you need this, the existing ToStringSerializer
should probably surfice.
} | ||
} | ||
|
||
impl TypeSerializer for UuidSerializer { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure you need this, the existing ToStringSerializer
should probably suffice.
Closing as no response. |
To get fast UUID validation, we should do the following:
|
I think this has now been superseded by #772. |
Closes #554