-
Notifications
You must be signed in to change notification settings - Fork 3
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
fix: disallow passing empty hosts to _UriData
and fetch-libs at build time
#6
Conversation
691c717
to
9ff36a8
Compare
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.
Looks good overall. I have some questions/suggestions around how we're going about validating the arguments in _UriData
. Let me know if you have any questions!
for host in hosts: | ||
if not host: | ||
raise FilesystemInfoError(f"invalid host `{host}`") |
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 a bit confused what this is doing here? I'm inclined to believe that we're filtering out falsey conditions such as empty strings ""
and None
, however, doesn't this mean that integers, floats, and True
will be treated as valid hostnames as well? Not sure if this is behavior that we want. If we're just trying to quickly validate what's being passed, we could possibly use jsonschema
or pydantic
.
Personally pydantic
is a bit overkill imho, but jsonschema
could solve our problem nicely here:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"scheme": {"type": "string"},
"hosts": {"type": "array", "items": {"type": "string"}, "uniqueItems": True},
"user": {"type": "array", "items": {"type": "string"}, "uniqueItems": True},
"path": {"type": "string"},
"options": {"type": "object", ...},
},
"additionalProperties": False,
}
The initialization arguments should be passed as the correct type and/or structure to _UriData rather than put the onus on us to beat the passed values into the desired format that we want. It makes the constructor much simpler, and requires much less edge case handling.
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 really don't want to rely on PYDEPS
though, since we don't really know if Charmcraft will decide to deprecate it in the future...
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.
That's fair reasoning for not wanting to use jsonschema
directly, but I still believe that we shouldn't carry a bunch of code for performing custom type validation at runtime. I think it's fair for us to expect that the arguments provided when a _UriData
object is created are of the correct type. This way we potentially don't need to override the __init__
method for the dataclass. IIRC pyright
should ideally catch when a dev is passing a value of the wrong type to _UriData
.
If we do still want runtime checking, I still think jsonschema
would be useful. I've found that custom validators can get messy quick especially if you don't have tight control over the input data.
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 agree. I'll remove the validation code.
9ff36a8
to
65308a1
Compare
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.
Continuing our last bit of conversation on the runtime checking for _UriData
, but other than that I think this PR looks good!
for host in hosts: | ||
if not host: | ||
raise FilesystemInfoError(f"invalid host `{host}`") |
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.
That's fair reasoning for not wanting to use jsonschema
directly, but I still believe that we shouldn't carry a bunch of code for performing custom type validation at runtime. I think it's fair for us to expect that the arguments provided when a _UriData
object is created are of the correct type. This way we potentially don't need to override the __init__
method for the dataclass. IIRC pyright
should ideally catch when a dev is passing a value of the wrong type to _UriData
.
If we do still want runtime checking, I still think jsonschema
would be useful. I've found that custom validators can get messy quick especially if you don't have tight control over the input data.
65308a1
to
a506cd0
Compare
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.
LGTM! Nice work
Fixes a bug that allowed passing empty host names to
_UriData
from theNfsInfo
class.Fixes the charmcraft build.