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

Empty path gets upgraded to pathlib.Path(".") by FMModel #703

Open
veenstrajelmer opened this issue Sep 24, 2024 · 1 comment
Open

Empty path gets upgraded to pathlib.Path(".") by FMModel #703

veenstrajelmer opened this issue Sep 24, 2024 · 1 comment

Comments

@veenstrajelmer
Copy link
Collaborator

veenstrajelmer commented Sep 24, 2024

Describe the bug
When initializing an FMModel with empty paths (""), these are upgraded to pathlib.Path("."), which results in a . in the mdu file instead of an empty value.

To Reproduce
The following code fails with AssertionError since mdu.output.outputdir=WindowsPath("."). When generating a mdu file from scratch (mdu_clean), the value is according to expectations.

from hydrolib.core.dflowfm import FMModel

# this succeeds
mdu_clean = FMModel()
assert mdu_clean.output.outputdir == ""

# this fails
mdu_clean = FMModel()
mdu_clean.output.outputdir = ""
assert mdu_clean.output.outputdir == "" # AssertionError, since it is a pathlib.Path(".")

# this fails (comparable as above, but the hydromt_delft3dfm approach for initializing an FMModel)
cf_dict = {"output": {"outputdir":""}}
mdu = FMModel(**cf_dict)
assert mdu.output.outputdir == "" # AssertionError, since it is a pathlib.Path(".")

Related issues:

Expected behavior
mdu.output.outputdir to be an emtpy string ("")

Version info (please complete the following information):

  • OS: Windows 10
  • Version 0.8.0
@veenstrajelmer
Copy link
Collaborator Author

Investigation by @tim-vd-aardweg:
Volgens mij is het met Path alléén niet mogelijk om onderscheid te maken tussen een lege string en een ., want Path("") == Path("."). Ik heb naar de internals gekeken van Path en kon ook niets vinden.. Dus ik denk dat er iets van een custom class gemaakt moet worden hiervoor. Iets als:

class CustomPath:
    def __init__(self, path_str):
        self.path_str = path_str
        if path_str and path_str != '.':
            self.path = Path(path_str)
        elif path_str == '.':
            self.path = Path('.')
        else:
            self.path = None  # Represents an empty path
    def __str__(self):
        return self.path_str

En daar moeten dan weer allemaal validators voor gemaakt worden voor pydantic en natuurlijk testjes toegevoegd worden.

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

No branches or pull requests

1 participant