-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Apply use-pathlib rules #10060
base: main
Are you sure you want to change the base?
Apply use-pathlib rules #10060
Conversation
Reviewer's Guide by SourceryThis pull request refactors file operations in the Class diagram showing pathlib migration changesclassDiagram
class Path {
+exists()
+chmod()
+unlink()
+is_symlink()
+parent
+readlink()
+joinpath()
+open()
}
class OldImplementation {
+os.path.exists()
+os.chmod()
+os.unlink()
+os.path.islink()
+os.path.dirname()
+os.readlink()
+os.path.join()
+open()
}
note for Path "New pathlib.Path implementation"
note for OldImplementation "Previous os.path implementation"
OldImplementation ..> Path : Migrated to
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @Secrus - I've reviewed your changes and they look great!
Here's what I looked at during the review
- 🟡 General issues: 1 issue found
- 🟢 Security: all looks good
- 🟢 Testing: all looks good
- 🟢 Complexity: all looks good
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
@sourcery-ai review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @Secrus - I've reviewed your changes and they look great!
Here's what I looked at during the review
- 🟢 General issues: all looks good
- 🟢 Security: all looks good
- 🟢 Testing: all looks good
- 🟢 Complexity: all looks good
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have skipped reviewing this pull request. It looks like we've already reviewed the commit fe0df70 in this pull request.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some initial comments.
@@ -110,7 +110,7 @@ class Config: | |||
"virtualenvs": { | |||
"create": True, | |||
"in-project": None, | |||
"path": os.path.join("{cache-dir}", "virtualenvs"), | |||
"path": os.path.join("{cache-dir}", "virtualenvs"), # noqa: PTH118 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason why we can't replace this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from what I understand it would be str(Path(...))
which doesn't look like a good use of resources, while os.path
operates on strings all the way
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a comment maybe to avoid someone swapping it later.
But I reckon since it's not always a valid path (eg: template) it's okay.
@@ -493,14 +493,14 @@ def create_venv( | |||
# but others can symlink *to* the venv Python, | |||
# so we can't just use sys.executable. | |||
# So we just check every item in the symlink tree (generally <= 3) | |||
p = os.path.normcase(sys.executable) | |||
p = Path(os.path.normcase(sys.executable)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is normcase required with Path?
paths.append(p) | ||
|
||
p_venv = os.path.normcase(str(venv)) | ||
if any(p.startswith(p_venv) for p in paths): | ||
if any(str(p).startswith(p_venv) for p in paths): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this use relative?
@@ -194,7 +194,7 @@ def test_add_existing_plugin_warns_about_no_operation( | |||
installed: TestRepository, | |||
) -> None: | |||
pyproject = SelfCommand.get_default_system_pyproject_file() | |||
with open(pyproject, "w", encoding="utf-8", newline="") as f: | |||
with pyproject.open("w", encoding="utf-8", newline="") as f: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use write_text? Similar in a few other places.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only after dropping support for Python 3.9, I assume:
Changed in version 3.10: The newline parameter was added.
https://docs.python.org/3/library/pathlib.html#pathlib.Path.write_text
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly this. I can add a TODO for when we drop 3.9 to change that
Since we use
os.path.join
as a string glue method, I silenced those cases.Summary by Sourcery
Refactor file operations to use pathlib.
Enhancements:
open()
withPath.read_text()
andPath.write_text()
for file I/O.Path.mkdir()
instead ofos.mkdir
.Path.exists()
instead ofos.path.exists()
.Path.is_symlink()
instead ofos.path.islink()
.Path.unlink()
instead ofos.unlink()
.Path.name
instead ofos.path.basename()
.