Skip to content
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

Merged
merged 7 commits into from
Nov 2, 2023

Conversation

ThibaultFy
Copy link
Member

@ThibaultFy ThibaultFy commented Oct 12, 2023

Related issue

See slack https://owkin.slack.com/archives/C01UWGW68N8/p1696943277910689 for context
closes FL-1243

Summary

Resolve path and paths on datasample spec.

path = pathlib.Path(values["path"])
if not path.is_absolute():
values["path"] = path.resolve()

Copy link

@jeandut jeandut Oct 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else: values["path"]=path

@linear
Copy link

linear bot commented Oct 12, 2023

FL-1243 Resolve path in DatasampleSpec

Context

See slack with Jean for context: https://owkin.slack.com/archives/C01UWGW68N8/p1696943277910689

Specification

Relative path raise weird error if given to a datasample spec. We want to resolve the paths to allow relative path.

Acceptance criteria

@ThibaultFy ThibaultFy force-pushed the feat/resolve-relative-datasample-path branch from 7bbb126 to 34ea495 Compare October 12, 2023 09:23
@ThibaultFy ThibaultFy marked this pull request as ready for review October 12, 2023 09:25
@ThibaultFy ThibaultFy requested a review from a team as a code owner October 12, 2023 09:25
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No typing on values?

Copy link
Member Author

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 ?

Copy link
Contributor

@guilhem-barthes guilhem-barthes Oct 23, 2023

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:

Copy link
Contributor

@guilhem-barthes guilhem-barthes left a 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!

paths = []
for path in values["paths"]:
path = pathlib.Path(path)
paths.append(path.resolve()) if not path.is_absolute() else paths.append(path)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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?

Copy link
Member Author

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

@@ -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
Copy link
Contributor

@guilhem-barthes guilhem-barthes Oct 23, 2023

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>
@ThibaultFy ThibaultFy force-pushed the feat/resolve-relative-datasample-path branch from 34ea495 to dcdbb7a Compare October 24, 2023 07:47
@@ -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
Copy link
Contributor

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?

Copy link
Member Author

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" :/

Copy link
Contributor

@guilhem-barthes guilhem-barthes Nov 2, 2023

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)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice thx :)

"""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
Copy link
Contributor

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

Comment on lines 152 to 155
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.")
Copy link
Contributor

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?

Copy link
Member Author

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 ?

Copy link
Contributor

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).

Copy link
Member Author

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 ?

Copy link
Contributor

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?

Copy link
Member Author

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>
Copy link
Contributor

@guilhem-barthes guilhem-barthes left a 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!

@ThibaultFy ThibaultFy merged commit 9ba1cf9 into main Nov 2, 2023
6 checks passed
@ThibaultFy ThibaultFy deleted the feat/resolve-relative-datasample-path branch November 2, 2023 13:36
ThibaultFy added a commit that referenced this pull request Jan 11, 2024
Signed-off-by: ThibaultFy <50656860+ThibaultFy@users.noreply.github.com>
ThibaultFy added a commit that referenced this pull request Jan 11, 2024
Signed-off-by: ThibaultFy <50656860+ThibaultFy@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants