-
-
Notifications
You must be signed in to change notification settings - Fork 581
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
Enhancing by_relevance / best_match (PR for issue 728) #753
Conversation
…ize the type of the error of the instance
…he same type of the instance for const
…he same type of the instance for enum
for. Modifying the system to lookup the 'type' property of the rule to match the type of the instance.
@@ -301,8 +341,31 @@ def by_relevance(weak=WEAK_MATCHES, strong=STRONG_MATCHES): | |||
a collection of validator names to consider to be "strong" | |||
""" | |||
def relevance(error): | |||
validator = error.validator | |||
return -len(error.path), validator not in weak, validator in strong | |||
same_type = False |
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.
So! Thanks again for trying to tackle this.
I still want to go for something a lot simpler than the code here. Maybe this will help as a start -- I think the changes to this file and this function probably should be just something like:
same_type = False
if "type" in error.schema:
same_type = error._type_checker.is_type(error.instance, error.schema["type"])
validator = error.validator
return (
-len(error.path),
error.validator not in weak,
error.validator in strong,
not same_type,
)
The missing piece there is that the type checker that's used is not in fact carried onto Errors at the minute, but that should be 1-2 lines to add.
If you want to see what that does, you can try instead starting with:
same_type = False
if "type" in error.schema:
same_type = _types.draft7_type_checker.is_type(error.instance, error.schema["type"])
validator = error.validator
return (
-len(error.path),
error.validator not in weak,
error.validator in strong,
not same_type,
)
And looking to see what it does to your test cases. From here it looks like it passes your first new one (which was the one that #728 was mainly targeting).
It fails the other two, but I'm not 100% convinced on the other two personally. If we do want to support them though, which yeah we should think about, there are other ways to handle them -- e.g. perhaps errors from non-container types (simple value types, e.g. strings, bools, etc.) should prioritize enum and const validators.
Hopefully the above is a breadcrumb to move this further a bit?
Let me know either way, and probably also worth having a look at CI to make sure it passes with whatever change, it'll be needed before we merge.
Appreciated again for staying on top of this!
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.
Left a comment with some next steps!
@miquelougoogle did the comments I left make sense here? Just making sure you saw them. |
Hi Julian, I am not sure of the consequences of prioritizing const and enum over other types of error. (If I understood you right, which I am not sure I did) Given the following example : {
"anyOf": [
{"type" : "string", "pattern" : "-?\\d+"},
{"const" : "NA"}
]
} Instance :
(This is provided as a number, not as a string, as the example schema specifies) The error reported if const / enum would be prioritized without looking at the type contained, would be something like :
Instead of the error telling the user that the I think
In that case, the error that makes the most sense is that it is not So I believe knowing if the instance has the same_type of the const / enum. Another argument, would be to keep the behaviour of Thanks for taking the time, |
Hi Julian, I won't have the time to work on this cl, and it looks like I won't have any for the near future. I'll close the PR until I'm ready to spend more time on it. Thanks for your comprehension. |
No worries, definitely understandable. Hope all's well and thanks for your
efforts.
…On Thu, Dec 3, 2020, 05:04 miquelougoogle ***@***.***> wrote:
Closed #753 <#753>.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#753 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AACQQXQIQGRJIESNSEM4Z2TSS5PBFANCNFSM4S4SOXGQ>
.
|
No description provided.