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 use of str(pathlib.Path(...)) in codebase #599

Merged
merged 9 commits into from
Sep 5, 2021

Conversation

iamzili
Copy link
Contributor

@iamzili iamzili commented Jul 12, 2021

I removed str() functions from str(pathlib.Path(...)) or replaced with os.fsdecode. str() for Pathlib objects was introduced for python 3.5 compatibility.

Refs #592

PR Checklist:

  • All new features have been tested
  • All new features have been documented
  • I have read the CONTRIBUTING.md file
  • I will abide by the code of conduct

@iamzili iamzili marked this pull request as draft July 12, 2021 08:24
@iamzili iamzili marked this pull request as ready for review July 12, 2021 08:43
Copy link
Member

@freakboy3742 freakboy3742 left a comment

Choose a reason for hiding this comment

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

This looks really good - nice work!

I've flagged a few things for follow up; they almost all fall into one of three camps:

  1. Calls to stdlib functions where I'm almost certain explicit fsdecode conversion isn't required - this is calls like shutil.rmtree()
  2. Calls to stdlib functions where I suspect explicit fsdecode conversion isn't required, but I could be wrong - this is mostly in calls to subprocess et al, where my gut tells me that Path objects should be OK for cwd, and might be OK for arguments... but I also wouldn't be surprised if they're not. Investigate; if I'm wrong, let me know and move on.
  3. Calls where we are in control of the external API surface. Wherever possible, the API exposed by Briefcase should be the same as stdlib - "string or path like object". We should be managing any fsdecode conversion so that the end-user doesn't need to worry about that detail (to the extent possible).

To be clear - what you've done is great, and works fine (as evidenced by the tests passing). The issues I've flagged in group 1 and 2 are subtle edge cases where the conversions you've done aren't wrong - they're just a little over-enthusiastic.

The problems in group 3 are mostly problems you've inherited, and the conversions you've done are doing exactly the right thing, but since we're doubling down on making our usage of Path correct, we should take the extra time and clean up those uses, too.

self.os.makedirs(str(Path(lib_path).parent))
self.shutil.move(str(Path(tmpdir) / 'lib'), lib_path)
self.os.makedirs(os.fsdecode(Path(lib_path).parent))
self.shutil.move(os.fsdecode(Path(tmpdir) / 'lib'), lib_path)
Copy link
Member

Choose a reason for hiding this comment

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

Are these 4 conversions necessary? AFAICT, they're all being used in calls to stdlib functions, so they should be auto converted

Copy link
Contributor Author

Choose a reason for hiding this comment

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

shutil.move only accepts path-like objects from 3.9, I think we should use there os.fsdecode() link

Copy link
Member

Choose a reason for hiding this comment

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

Well that's annoying :-)

In that case, you're definitely correct including the conversion here. However, it would be worth adding a docstring so future editors know (a) this isn't redundant, and (b) when we (eventually) drop 3.8 support, dropping the conversion is an option as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

docstring added

str(support_filename),
extract_dir=str(support_path)
os.fsdecode(support_filename),
extract_dir=os.fsdecode(support_path)
Copy link
Member

Choose a reason for hiding this comment

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

Are you sure about this one? On first inspection, it looks like it should be a straight removal of str(), rather than conversion.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

according to docs it should be os.fsdecode or str(): Changed in version 3.7: Accepts a path-like object for filename and extract_dir. link

Copy link
Member

Choose a reason for hiding this comment

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

As above; definitely correct to include the conversion, but we should document it for future editors.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

comment added

Copy link
Contributor Author

@iamzili iamzili Aug 7, 2021

Choose a reason for hiding this comment

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

docstring added

@@ -31,7 +32,7 @@ def test_simulator_is_missing(tmp_path):
simulators = get_simulators(
command,
'iOS',
simulator_location=str(tmp_path / 'CoreSimulator.framework'),
simulator_location=os.fsdecode(tmp_path / 'CoreSimulator.framework'),
Copy link
Member

Choose a reason for hiding this comment

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

Similarly; this method should be modified so it can accept path objects.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

os.fsdecode removed

Copy link
Member

@freakboy3742 freakboy3742 left a comment

Choose a reason for hiding this comment

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

I think we're almost there. All the changes you've made look great (or you've given a good explanation why my comment was wrong).

The handful of places where we've got "Path objects are legal, but not in Python 3.6/3.7" can be flagged with docstrings - something like:

# TODO: Py3.6 compatibility; os.fsencode not required in Py3.7

will give us something to easily search for when the time comes. If you make those handful of changes, I think we'll be good to go!

(As an aside - those docstring updates are sufficiently minor that I would normally make them myself; however, because you've made this PR off your master branch, I can't submit edits. As a general rule, it's a good idea to always do PR work in a branch - firstly, because I allows maintainers to make minor edits like these; but also because it means you can have multiple PRs in flight at the same time. Not a big deal, but if you work on another PR - and I hope you will! - it's worth keeping in mind).

@iamzili
Copy link
Contributor Author

iamzili commented Aug 7, 2021

@freakboy3742 You can check again the PR, if something is needed please let me know.

@codecov
Copy link

codecov bot commented Aug 31, 2021

Codecov Report

Merging #599 (05c50b9) into master (eea908c) will increase coverage by 0.01%.
The diff coverage is 96.15%.

Impacted Files Coverage Δ
src/briefcase/platforms/android/gradle.py 92.70% <ø> (ø)
src/briefcase/commands/create.py 97.04% <83.33%> (+0.01%) ⬆️
src/briefcase/commands/new.py 93.98% <100.00%> (+0.04%) ⬆️
src/briefcase/integrations/android_sdk.py 98.85% <100.00%> (+<0.01%) ⬆️
src/briefcase/integrations/docker.py 91.35% <100.00%> (+0.10%) ⬆️
src/briefcase/integrations/java.py 100.00% <100.00%> (ø)
src/briefcase/integrations/linuxdeploy.py 100.00% <100.00%> (ø)
src/briefcase/integrations/wix.py 100.00% <100.00%> (ø)
src/briefcase/platforms/linux/appimage.py 96.22% <100.00%> (+0.03%) ⬆️
src/briefcase/platforms/macOS/app.py 100.00% <100.00%> (ø)
... and 1 more

Copy link
Member

@freakboy3742 freakboy3742 left a comment

Choose a reason for hiding this comment

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

🎉 Awesome! This looks great! Thanks for all the hard work on this - and apologies for the delay on the final review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants