Skip to content

Support pathlib.Path objects wherever possible #14893

Open

Description

@astrofrog

This is a 'big picture' issue but we should in general make sure that anywhere that accepts filenames/paths in astropy can accept pathlib.Path objects. This is in the standard library and is a very convenient way of defining file paths. A lot of parts of astropy should already work with this, but as @nstarman found there are still places where this is not the case (astropy/astropy-iers-data#1 (comment)).

Blocked by

Activity

  1. pllim commented on Jun 1, 2023

    @pllim
    Member

    How do we know when this is done? Isn't it better to open issue for specific area of the code as we find them?

  2. astrofrog commented on Jun 1, 2023

    @astrofrog
    MemberAuthor

    Yes but I was wondering whether we might be able to auto detect cases - if it was possible to intercept all function calls (similar to sys.setprofile but with access to the args/kwargs), we could detect all strings that match files that exist with os.path.exists and replace them with Path and see what breaks. I can try and investigate a little whether something like this might exist.

  3. pllim commented on Jun 1, 2023

    @pllim
    Member

    If we want to discover via our unit tests, we can hunt down the tmpdir and tmp_path usage, and then see where we are passing in actual Path objects and where we need to recast as str first. Otherwise, I am not sure how you can smoke them out besides reading 10+ years worth of code.

  4. ManonMarchand commented on Jun 7, 2023

    @ManonMarchand
    Member
  5. nstarman commented on Jun 7, 2023

    @nstarman
    Member

    It could be done by using the Ruff rules about pathlib

    https://beta.ruff.rs/docs/rules/#flake8-use-pathlib-pth

    Unfortunately not. This isn't about replacing os.path operations but supporting Path objects where we currently only support strings.

  6. MridulS commented on Feb 18, 2024

    @MridulS
    Contributor

    I'm trying to redo the machinery for how files i/o is handled in astropy. It seems like in some places code works just fine with pathlib and some places filenames need to be strings. I would like to go over this in 3 steps. Let me know if folks have any ideas/suggestions here:

  7. pllim commented on Feb 19, 2024

    @pllim
    Member

    @saimn , what do you think about decorator option? If anything is going to break, it would probably be astropy.io.fits. 😬

  8. saimn commented on Feb 20, 2024

    @saimn
    Contributor

    Depends what the decorator is for and what problem we are trying to solve, which isn't clear to me right now.
    I guess one issue to solve would be to handle overwrite more consistently across astropy (#15350) and that may be solvable with a decorator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions