-
Notifications
You must be signed in to change notification settings - Fork 33
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
feat: resolve relative datasample spec path #392
Conversation
path = pathlib.Path(values["path"]) | ||
if not path.is_absolute(): | ||
values["path"] = path.resolve() | ||
|
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.
else: values["path"]=path
FL-1243 Resolve path in DatasampleSpec
ContextSee slack with Jean for context: https://owkin.slack.com/archives/C01UWGW68N8/p1696943277910689 SpecificationRelative path raise weird error if given to a datasample spec. We want to resolve the paths to allow relative path. Acceptance criteria |
7bbb126
to
34ea495
Compare
substra/sdk/schemas.py
Outdated
@@ -155,6 +155,23 @@ def exclusive_paths(cls, values): # noqa: N805 | |||
raise ValueError("'path' or 'paths' field must be set.") | |||
return values | |||
|
|||
@pydantic.model_validator(mode="before") | |||
def resolve_paths(cls, values): # noqa: N805 |
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.
No typing on values
?
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 just copy paste it from abose. We might add it though, but it's values
is provided by pydantic so it's not that bad no ? Wdyt ?
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 would rather use values: Any): -> Any
or create a custom type (e.g. ValueType
) and use values: ValueType) -> ValueType:
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.
Thanks for your work!
substra/sdk/schemas.py
Outdated
paths = [] | ||
for path in values["paths"]: | ||
path = pathlib.Path(path) | ||
paths.append(path.resolve()) if not path.is_absolute() else paths.append(path) |
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.
paths.append(path.resolve()) if not path.is_absolute() else paths.append(path) | |
paths.append(path.resolve()) |
Do we need to check is_absolute()
aspath.resolve()
will return itself if already a absolute path?
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.
Very good point ahah :D
substra/sdk/schemas.py
Outdated
@@ -155,6 +155,23 @@ def exclusive_paths(cls, values): # noqa: N805 | |||
raise ValueError("'path' or 'paths' field must be set.") | |||
return values | |||
|
|||
@pydantic.model_validator(mode="before") | |||
def resolve_paths(cls, values): # noqa: N805 |
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 would rather use values: Any): -> Any
or create a custom type (e.g. ValueType
) and use values: ValueType) -> ValueType:
Signed-off-by: ThibaultFy <50656860+ThibaultFy@users.noreply.github.com>
Signed-off-by: ThibaultFy <50656860+ThibaultFy@users.noreply.github.com>
Signed-off-by: ThibaultFy <50656860+ThibaultFy@users.noreply.github.com>
Signed-off-by: ThibaultFy <50656860+ThibaultFy@users.noreply.github.com>
34ea495
to
dcdbb7a
Compare
substra/sdk/schemas.py
Outdated
@@ -147,14 +147,31 @@ def is_many(self): | |||
return self.paths and len(self.paths) > 0 | |||
|
|||
@pydantic.model_validator(mode="before") | |||
def exclusive_paths(cls, values): # noqa: N805 | |||
def exclusive_paths(cls, values: typing.Any) -> typing.Any: # noqa: N805 |
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.
can you remove the# noqa: N805
?
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.
Nop, its because it does not like that the first arg is not "self" :/
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.
If this method receive a class as first argument, pydantic recommends decorate with @classmethod
for a proper type checking (model_validator)
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.
nice thx :)
substra/sdk/schemas.py
Outdated
"""Check that one and only one path(s) field is defined.""" | ||
if "paths" in values and "path" in values: | ||
raise ValueError("'path' and 'paths' fields are exclusive.") | ||
if "paths" not in values and "path" not in values: | ||
raise ValueError("'path' or 'paths' field must be set.") | ||
return values | ||
|
||
@pydantic.model_validator(mode="before") | ||
def resolve_paths(cls, values: typing.Any) -> typing.Any: # noqa: N805 |
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.
can you remove the # noqa: N805
? It should be handled by the typing
if "paths" in values and "path" in values: | ||
raise ValueError("'path' and 'paths' fields are exclusive.") | ||
if "paths" not in values and "path" not in values: | ||
raise ValueError("'path' or 'paths' field must be set.") |
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.
Could they define both keys but with value 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.
I don't get what you're refering too. Could you provide an example plz ?
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.
It looks like we allow None
as value on these fields, therefore if a user explicitely define only one of this key to None
(e.g. DataSampleSpec(path=None, data_manager_keys=[])
, the check "paths" not in values and "path" not in values
will return False (as the key is present, but the value is 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.
Ok I see. I added some tests, and it appears that this case is dealts with in the resolve path, as None
will raise a TypeError
. This is not an explicit solution but I added the tests to ensure that this behavior is taken into account. What do you think ?
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 know if TypeError
is the clearest for the user, but maybe it is worth opening its own PR to review how we manage this edge case?
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.
Yep True... I rm the tests, and +1 to deal with this in a dedicated PR
Signed-off-by: ThibaultFy <50656860+ThibaultFy@users.noreply.github.com>
Signed-off-by: ThibaultFy <50656860+ThibaultFy@users.noreply.github.com>
Signed-off-by: ThibaultFy <50656860+ThibaultFy@users.noreply.github.com>
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.
Thanks for your work!
Signed-off-by: ThibaultFy <50656860+ThibaultFy@users.noreply.github.com>
Signed-off-by: ThibaultFy <50656860+ThibaultFy@users.noreply.github.com>
Related issue
See slack https://owkin.slack.com/archives/C01UWGW68N8/p1696943277910689 for context
closes FL-1243
Summary
Resolve
path
andpaths
on datasample spec.