-
-
Notifications
You must be signed in to change notification settings - Fork 49
Defer tempfile import to sites of use
#328
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,6 @@ | |
| import itertools | ||
| import os | ||
| import pathlib | ||
| import tempfile | ||
| import types | ||
| import warnings | ||
| from typing import Optional, Union, cast | ||
|
|
@@ -127,6 +126,9 @@ def _tempfile( | |
| *, | ||
| _os_remove=os.remove, | ||
| ): | ||
| # Deferred for performance. | ||
| import tempfile | ||
|
|
||
| # Not using tempfile.NamedTemporaryFile as it leads to deeper 'try' | ||
| # blocks due to the need to close the temporary file to work on Windows | ||
| # properly. | ||
|
|
@@ -180,24 +182,18 @@ def _(path): | |
| yield path | ||
|
|
||
|
|
||
| @contextlib.contextmanager | ||
| def _temp_path(dir: tempfile.TemporaryDirectory): | ||
| """ | ||
| Wrap tempfile.TemporaryDirectory to return a pathlib object. | ||
| """ | ||
| with dir as result: | ||
| yield pathlib.Path(result) | ||
|
|
||
|
|
||
| @contextlib.contextmanager | ||
| def _temp_dir(path): | ||
| """ | ||
| Given a traversable dir, recursively replicate the whole tree | ||
| to the file system in a context manager. | ||
| """ | ||
| # Deferred for performance. | ||
| import tempfile | ||
|
|
||
| assert path.is_dir() | ||
| with _temp_path(tempfile.TemporaryDirectory()) as temp_dir: | ||
| yield _write_contents(temp_dir, path) | ||
| with tempfile.TemporaryDirectory() as temp_dir: | ||
| yield _write_contents(pathlib.Path(temp_dir), path) | ||
|
Comment on lines
+195
to
+196
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't care for this refactor. The reason _temp_path exists is to decouple the |
||
|
|
||
|
|
||
| def _write_contents(target, source): | ||
|
|
||
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.
I'm not a fan of repeating ourselves. Now we have 6 lines of code where previous 1 was sufficient... and moreover, these lines of code are entangled (their comments both refer to the same motivation and should be kept in sync. Better would be to do:
And then in
_import_tempfile, document the motivation, which should not only include the basic motivation ("deferred for performance"), but also details on what performance is saved. For example, it should link to the PR ("#328") and that PR should have examples of how much performance is saved.