-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[flake8-use-pathlib
] Recommend Path.iterdir()
over os.scandir()
(PTH209
)
#14623
Conversation
|
code | total | + violation | - violation | + fix | - fix |
---|---|---|---|---|---|
PTH209 | 4 | 4 | 0 | 0 | 0 |
How does
It might at least be worth mentioning in the docs for this rule, if |
@AlexWaygood Quite bad, in fact: # s.py
from pathlib import Path
d = Path(__file__).parent / 'd'
d.mkdir(exist_ok = True)
for i in range(10_000):
(d / f'd{i}').mkdir()
with (d / f'f{i}.txt').open('w'):
pass # t.py
from timeit import timeit
N = 1000
SETUP = '''
from pathlib import Path
import os
d = '/workspaces/test/d'
p = Path(d)
'''
a = timeit(stmt = '''for _ in p.iterdir(): ...''', setup = SETUP, number = N)
print(a)
b = timeit(stmt = '''for _ in os.listdir(d): ...''', setup = SETUP, number = N)
print(b) Results on a 4-core, 16GB RAM GitHub Codespace: @InSyncWithFoo ➜ /workspaces/test (main) $ uv run -p 3.13 python s.py
@InSyncWithFoo ➜ /workspaces/test (main) $ uv run -p 3.13 python t.py
28.847444689999975
10.691104289999885 That's a ~180% increase. |
@InSyncWithFoo it looks like you're comparing If #14632 is accepted, we can also have this rule suggest |
I'm concerned about adding a rule that is a considerable foot gun for the majority of users when its only benefit is consistency. Especially because the use of I suggest we wait to add this rule until Python 3.14 is closer to stabilizing and only then suggest using |
@MichaReiser and I discussed this offline. There's a general issue with our Our decision is that we'll keep When Python 3.14 enters the beta period, we can consider adding a rule suggesting to replace Sorry about this @InSyncWithFoo -- thanks for the PR anyway! Cc. @sbrugman also, since you were involved in the discussion in #14509 |
I guess I'll just have to wait for 3.14.0 beta 1, to be released sometime in May. Hopefully I won't forget. |
Summary
Per discussion at #14509.
Test Plan
cargo nextest run
andcargo insta test
.