Skip to content

Accept Python Path objects instead of only strings #4190

@davidpagnon

Description

@davidpagnon

Python pathlib paths are much cleaner and easier to use than strings, and it is the recommended Python approach for manipulating paths since Python 3.6 (introduced in Python 3.4). When using OpenSim in other libraries that work with Path objects, it is a bit of a pain and a source of errors to have to convert the path into a string.


Path does not do anything that os.path cannot do, it just makes it much easier and cleaner. Here are a few examples:

  • your_path = Path(__file__).parent / 'models' / 'model.osim' vs
    your_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'models', 'model.osim')
  • your_path.stem vs
    os.path.splitext(os.path.basename(p))[0]
  • your_path.with_suffix('.txt') vs
    good luck
  • It also allows for more precise type hints: def process(path: Path) instead of def process(path: str)

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