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

Replace os.path with pathlib.Path #46

Closed
wants to merge 5 commits into from

Conversation

JasonGrace2282
Copy link
Member

@JasonGrace2282 JasonGrace2282 commented Jun 6, 2024

See https://treyhunner.com/2018/12/why-you-should-be-using-pathlib/

Progress

  • Replace os.path.dirname, os.path.join, and os.chmod with pathlib.Path equivalents
  • Rewrite code to take advantage of Path objects

@krishnans2006 krishnans2006 force-pushed the master branch 13 times, most recently from c03ce92 to 090d035 Compare June 9, 2024 20:47
@JasonGrace2282 JasonGrace2282 added the maintenance Dependencies, deprecation warnings, etc. label Jun 16, 2024
@JasonGrace2282 JasonGrace2282 marked this pull request as ready for review June 17, 2024 20:47
@JasonGrace2282 JasonGrace2282 requested a review from a team as a code owner June 17, 2024 20:47
Copy link
Member

@krishnans2006 krishnans2006 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing work - I appreciate this PR so much. As a side note, the many error-handling improvements and sanity checks are great - thank you so much for those.

I haven't tested these changes yet (there's a lot to test), but here are a few comments from a quick code review. Anything that involves testing is a note to myself (so I'll know what I should take a closer look at) - don't worry about those comments for now.

@@ -170,14 +170,14 @@ def save_grader_file(self, grader_text: str) -> None:
self.grader_file.name = fname
self.save()

fpath = os.path.join(settings.MEDIA_ROOT, self.grader_file.name)
fpath = settings.MEDIA_ROOT / self.grader_file.name
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to test - this is slightly concerning.

Copy link
Member Author

@JasonGrace2282 JasonGrace2282 Jun 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what is concerning here (again, I'm not against you testing it) :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not the change itself - I'm just a bit sus of how this code block works.

tin/apps/assignments/models.py Show resolved Hide resolved
tin/apps/assignments/models.py Outdated Show resolved Hide resolved
tin/apps/assignments/tasks.py Outdated Show resolved Hide resolved
@@ -239,12 +234,15 @@ def save_file(self, submission_text: str) -> None:

fpath = self.file_path

os.makedirs(os.path.dirname(fpath), exist_ok=True)
if fpath is None:
raise ValueError("Cannot save file if file_path is not found")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is also a bit concerning and should be tested.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it works right now, it shouldn't make a difference (however, I don't discourage some testing!). The problem occurs because the file_path can be None if self.file is None. I just added this because pyright was complaining about fpath being None.
Same with similar exceptions being raised below.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I see that. Just want to make sure fpath is never actually None to begin with.

tin/apps/submissions/models.py Outdated Show resolved Hide resolved
submission_path = submission.file_path

submission_wrapper_path = submission.wrapper_file_path

if submission_wrapper_path is None:
raise ValueError("File not provided")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, test this.

@JasonGrace2282 JasonGrace2282 added enhancement New feature or request ! INTERNAL and removed maintenance Dependencies, deprecation warnings, etc. labels Aug 15, 2024
@JasonGrace2282
Copy link
Member Author

I think the approach taken here (replace os.path everywhere at once with pathlib.Path) is a little bit too drastic. I'll close this PR, and we can tackle each change in a separate PR, so that it's easier to test and review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request ! INTERNAL
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants