[PR #3957/79fe2045 backport][3.9] Improve test suite handling of paths, temp files #8084
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a backport of PR #3957 as merged into master (79fe204).
This updates most uses of
os.path
to instead usepathlib.Path
. Relatedly, and following up from #3955 (which replaced pytest'stmpdir
fixture withtmp_path
), this removes most ad-hoc tempfile creation in favor of thetmp_path
fixture. Following conversion, unnecessaryos
andtempfile
imports were removed.Most pathlib changes involve straightforward changes from
os
functions such asos.mkdir
oros.path.abspath
to their equivalent methods inpathlib.Path
.Changing ad-hoc temporary path to
tmp_path
involved removing thetmp_dir_path
fixture and replacing its functionality withtmp_path
intest_save_load
andtest_guess_filename_with_tempfile
.On
test_static_route_user_home
function:pathlib.Path
and cut out duplication ofrelative_to()
calls. But if it's not doing anything but expanding~
, then it's testing the functionality ofpathlib.Path
, not aiohttp.On
unix_sockname
fixture:This fixture uses
tempfile.TemporaryDirectory
. Because it's a somewhat complicated fixture used across multiple test modules, I left it as-is for now.On
str(tmp_path)
and evenpathlib.Path(str(tmp_path))
:pytest uses
pathlib2
to providetmp_path
for Python 3.5 (only). This is mostly fine but it fails on a couple of corner cases, such asos.symlink()
which blocks all butstr
andPurePath
via isinstance type checking. In several cases, this requires conversion to string or conversion to string and then intopathlib.Path
to maintain code compatibility. See: pytest-dev/pytest/issues/5017Correct test_guess_filename to use file object
Update symlink in tests; more guess_filename tests
(cherry picked from commit 79fe204)
What do these changes do?
This updates most uses of
os.path
to instead usepathlib.Path
.Relatedly, and following up from #3955 (which replaced pytest's
tmpdir
fixture with
tmp_path
), this removes most ad-hoc tempfile creation infavor of the
tmp_path
fixture. Following conversion, unnecessaryos
and
tempfile
imports were removed.Most pathlib changes involve straightforward changes from
os
functionssuch as
os.mkdir
oros.path.abspath
to their equivalent methods inpathlib.Path
.Changing ad-hoc temporary path to
tmp_path
involved removing thetmp_dir_path
fixture and replacing its functionality withtmp_path
in
test_save_load
andtest_guess_filename_with_tempfile
.On
test_static_route_user_home
function:correctly expands the home path if passed in a string. I refactored it
to
pathlib.Path
and cut out duplication ofrelative_to()
calls.But if it's not doing anything but expanding
~
, then it's testing thefunctionality of
pathlib.Path
, not aiohttp.On
unix_sockname
fixture:This fixture uses
tempfile.TemporaryDirectory
. Because it's a somewhatcomplicated fixture used across multiple test modules, I left it as-is
for now.
On
str(tmp_path)
and evenpathlib.Path(str(tmp_path))
:pytest uses
pathlib2
to providetmp_path
for Python 3.5 (only).This is mostly fine but it fails on a couple of corner cases, such as
os.symlink()
which blocks all butstr
andPurePath
via isinstancetype checking. In several cases, this requires conversion to string or
conversion to string and then into
pathlib.Path
to maintain codecompatibility. See: pytest-dev/pytest/issues/5017
Are there changes in behavior for the user?
These changes only affect the test suite and have no impact on the end user.
Related issue number
This is intended to address discussion following the simplistic changes from tmpdir to tmp_path of #3955.
Checklist
CONTRIBUTORS.txt
CHANGES
folder<issue_id>.<type>
for example (588.bugfix)issue_id
change it to the pr id after creating the pr.feature
: Signifying a new feature..bugfix
: Signifying a bug fix..doc
: Signifying a documentation improvement..removal
: Signifying a deprecation or removal of public API..misc
: A ticket has been closed, but it is not of interest to users.