You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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).
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.
p="."fordinos.listdir(p):
...
# Use instead:p=Path(".")
fordinp.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( )]
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.
Activity
sbrugman commentedon Jan 30, 2023
See #2348
sigma67 commentedon May 21, 2024
Since the issue is still open, is this still on the table? I think most
PTHrules have no autofixes yetIt seems there are no more open PRs on this topic
charliermarsh commentedon May 21, 2024
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.
JonathanPlasse commentedon May 21, 2024
Should the tag
type-inferencebe added?sigma67 commentedon May 21, 2024
Most of the fixes seem simple enough to me, i.e.
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.
charliermarsh commentedon May 21, 2024
Yeah we can do simple stuff like tha, although I think the spirit of the rule is that you'd changed
varto be of typePathmore holistically in the code, then dovar.mkdir(kwargs).flake8-use-pathlib] Autofixes forPTHrules #18752 as a duplicate of this issue[`flake8-use-pathlib`] Add autofix for `PTH202` (#18763)
ntBre commentedon Jun 26, 2025
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:
15 remaining items
[`flake8-use-pathlib`] Add autofix for `PTH211` (#20009)
[`flake8-use-pathlib`] Add autofix for `PTH211` (astral-sh#20009)
[`flake8-use-pathlib`] Add fix for `PTH123` (#20169)
ntBre commentedon Jan 29, 2026
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 bypathlib.Most of the fixes are also still in preview, but I'm looking into stabilizing them as part of the 0.15 release.
[`flake8-use-pathlib`] Add autofix for `PTH101`, `PTH104`, `PTH105`, …
[`flake8-use-pathlib`] Add fixes for `PTH102` and `PTH103` (#19514)
[`flake8-use-pathlib`] Add autofix for `PTH211` (#20009)
[`flake8-use-pathlib`] Add fix for `PTH123` (#20169)
[`flake8-use-pathlib`] Add autofixes for `PTH116` (#26460)
WhiteFox0-0 commentedon Aug 16, 2026
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.WhiteFox0-0 commentedon Aug 16, 2026
I was looking at os-listdir(
PTH208)The example suggests a fix as
which i believe cannot be correct because:
os.listdir( )returnsstrPath( ).iterdir( )returnsPosixPath(' ')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 otheros.something(“file.txt”)=>pathlib.Path(“file.txt”).{something_in_pathlib}So can we add autofix for
os-listdirto be[p.name for p in Path( ).iterdir( )]Not sure if this is the correct path :)
[`flake8-use-pathlib`] Add autofixes for `PTH116` (astral-sh#26460)
ntBre commentedon Aug 17, 2026
Thanks, updated!
Thanks for looking into PTH208 too. I think we could offer a
DisplayOnlyfix in this case. As you said, replacing withPath.iterdiris 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.[`flake8-use-pathlib`] Add display-only fix for `os.listdir` (`PTH208…