Skip to content

Fix incorrect Path(path).exists usage in config loaders #143

@Agarwalchetan

Description

@Agarwalchetan

The config loading logic in both Config.assign_config and ShipConfig.assign_config checks file existence using Path(path).exists without calling it. Since exists is a method, not calling it results in a truthy method object, which makes the condition always evaluate to True.

This can silently accept invalid paths and later fail during file operations with less clear errors.


Affected Locations

  • WeatherRoutingTool/config.py
  • WeatherRoutingTool/ship/ship_config.py

Current pattern:

if Path(path).exists:
    with path.open("r") as f:
        ...

This should call the method:

if Path(path).exists():

Why This Matters

  • Invalid paths may not be detected early.
  • The code may fail later with FileNotFoundError or AttributeError, making debugging harder.
  • The logic does not reliably validate configuration file existence.

Proposed Fix

  • Normalize input using path = Path(path) to support both str and Path.

  • Replace Path(path).exists with path.exists().

  • Raise a clear ValueError if:

    • path is None when init_mode == "from_json"
    • The file does not exist

This keeps behavior unchanged for valid inputs while improving robustness and clarity of error handling.


I am currently working on this fix and will raise a PR shortly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions