autofix for flake8-use-pathlib (PTH) rules · Issue #2331 · astral-sh/ruff · GitHub
Skip to content

autofix for flake8-use-pathlib (PTH) rules #2331

Open

Description

@LefterisJP

I enabled PTH rules and there was a few hits in rotki's codebase.

This is a rule I woud like to enforce but there is quite many places it hit and I suppose at least some of the rules should be autofixable. So if possible it would be cool to have ruff do it for us.

Activity

  1. sbrugman commented on Jan 30, 2023

    @sbrugman
    Contributor

    See #2348

  2. sigma67 commented on May 21, 2024

    @sigma67

    Since the issue is still open, is this still on the table? I think most PTH rules have no autofixes yet

    It seems there are no more open PRs on this topic

  3. charliermarsh commented on May 21, 2024

    @charliermarsh
    Member

    Certainly on the table, just needs someone to work on it. It may also be limited by our ability to do program-wide analysis beyond simple fixes.

  4. JonathanPlasse commented on May 21, 2024

    @JonathanPlasse
    Contributor

    Should the tag type-inference be added?

  5. sigma67 commented on May 21, 2024

    @sigma67

    It may also be limited by our ability to do program-wide analysis beyond simple fixes.

    Most of the fixes seem simple enough to me, i.e.

    PTH102 "os.mkdir() should be replaced by Path.mkdir()"

    os.mkdir(var, kwargs)

    becomes

    Path(var).mkdir(kwargs)

    I may be missing something - not sure how it could affect a broader scope of code.

  6. charliermarsh commented on May 21, 2024

    @charliermarsh
    Member

    Yeah we can do simple stuff like tha, although I think the spirit of the rule is that you'd changed var to be of type Path more holistically in the code, then do var.mkdir(kwargs).

  7. added a commit that references this issue on Jun 24, 2025
    3220242
  8. ntBre commented on Jun 26, 2025

    @ntBre
    Contributor

    Oops, shouldn't have closed this. #18763 added a fix for PTH202, but most of the rules still don't have fixes. I'll keep track of the status here:

    Rule Code Status
    os-path-abspath PTH100 #19213
    os-chmod PTH101 #19404
    os-mkdir PTH102 #19514
    os-makedirs PTH103 #19514
    os-rename PTH104 #19404
    os-replace PTH105 #19404
    os-rmdir PTH106 #19213
    os-remove PTH107 #19213
    os-unlink PTH108 #19213
    os-getcwd PTH109 #19245
    os-path-exists PTH110 #19213
    os-path-expanduser PTH111 #19213
    os-path-isdir PTH112 #19213
    os-path-isfile PTH113 #19213
    os-path-islink PTH114 #19213
    os-readlink PTH115 #19213
    os-stat PTH116
    os-path-isabs PTH117 #19213
    os-path-join PTH118
    os-path-basename PTH119 #19213
    os-path-dirname PTH120 #19213
    os-path-samefile PTH121 #19404
    os-path-splitext PTH122
    builtin-open PTH123 #20169
    py-path PTH124
    path-constructor-current-directory PTH201 x
    os-path-getsize PTH202 #18763
    os-path-getatime PTH203 #18922
    os-path-getmtime PTH204 #18922
    os-path-getctime PTH205 #18922
    os-sep-split PTH206
    glob PTH207
    os-listdir PTH208
    invalid-pathlib-with-suffix PTH210 x
    os-symlink PTH211 #20009
  9. reopened this on Jun 26, 2025
  10. 15 remaining items

  11. ntBre commented on Jan 29, 2026

    @ntBre
    Contributor

    The table in #2331 (comment) should now be updated. There are only 6 rules left to cover, possibly 5 if we exclude py-path. I'd have to check whether all of its APIs are straightforwardly covered by pathlib.

    Most of the fixes are also still in preview, but I'm looking into stabilizing them as part of the 0.15 release.

  12. WhiteFox0-0 commented on Aug 16, 2026

    @WhiteFox0-0
    Contributor

    Hey @ntBre,
    #2331 (comment) seems to have marked os-path-join(PTH118) as done in PR #19213 but it seems to me that it is mistakenly marked.

  13. WhiteFox0-0 commented on Aug 16, 2026

    @WhiteFox0-0
    Contributor

    I was looking at os-listdir(PTH208)

    The example suggests a fix as

    p = "."
    for d in os.listdir(p):
        ...
    
    # Use instead:
    p = Path(".")
    for d in p.iterdir(): 
        ...

    which i believe cannot be correct because:
    os.listdir( ) returns str
    Path( ).iterdir( ) returns PosixPath(' ')
    so where ever the os.listdir is used assumes the return type to be str and it might break the internal logic

    The autofix cant be a simple os.listdir( ) -> Path( ).iterdir( ) like other
    os.something(“file.txt”) => pathlib.Path(“file.txt”).{something_in_pathlib}

    So can we add autofix for os-listdir to be [p.name for p in Path( ).iterdir( )]

    Not sure if this is the correct path :)

  14. ntBre commented on Aug 17, 2026

    @ntBre
    Contributor

    Hey @ntBre, (earlier comment) seems to have marked os-path-join(PTH118) as done in PR #19213 but it seems to me that it is mistakenly marked.

    Thanks, updated!

    Thanks for looking into PTH208 too. I think we could offer a DisplayOnly fix in this case. As you said, replacing with Path.iterdir is very likely to break downstream code, but I think that's the idiomatic fix if you want to enable the rule. The comprehension fix seems too complicated/inelegant to me just to get the types to line up.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    fixesRelated to suggested fixes for violationshelp wantedContributions especially welcome

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions