Skip to content
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

New flake8-pathlib rule: os.listdir (PTH208) #14490

Open
sbrugman opened this issue Nov 20, 2024 · 2 comments · May be fixed by #14509
Open

New flake8-pathlib rule: os.listdir (PTH208) #14490

sbrugman opened this issue Nov 20, 2024 · 2 comments · May be fixed by #14509
Labels
rule Implementing or modifying a lint rule

Comments

@sbrugman
Copy link
Contributor

sbrugman commented Nov 20, 2024

The pathlib rules supported by ruff could be extended with detection with os.listdir:

import os
from pathlib import Path

p = Path(".")
print(os.listdir(p))

use instead:

from pathlib import Path

p = Path(".")
print(list(p.iterdir()))

The plugin has not been updated for two years. We've extended the plugin rules before using the PTH2XX code.

@AlexWaygood AlexWaygood added the rule Implementing or modifying a lint rule label Nov 20, 2024
@MichaReiser
Copy link
Member

MichaReiser commented Nov 20, 2024

What's the motivation for the rule? The os.listdir code is more concise :)

@sbrugman
Copy link
Contributor Author

sbrugman commented Nov 20, 2024

In isolation there is not much benefit. Using methods on pathlib.Path return paths, which have properties and methods for writing concise path operations (stem, suffix, parent etc.)

Best illustrated with real-world use cases: https://github.com/pypa/pipenv/blob/main/tasks/vendoring/__init__.py#L162 or https://github.com/princeton-vl/infinigen/blob/main/infinigen/tools/datarelease_toolkit.py#L354

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants