Skip to content

Commit

Permalink
gh-38646: add typing annotation in temporary_file
Browse files Browse the repository at this point in the history
add some typing annotation in the modified file

also remove one unused import

and some traces of python 2

### 📝 Checklist

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [ ] I have linked a relevant issue or discussion.
- [ ] I have created tests covering the changes.
- [ ] I have updated the documentation and checked the documentation
preview.

URL: #38646
Reported by: Frédéric Chapoton
Reviewer(s): Vincent Macri
  • Loading branch information
Release Manager committed Sep 14, 2024
2 parents c82c509 + 7fc7e9b commit 27fce1f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 23 deletions.
4 changes: 2 additions & 2 deletions build/pkgs/configure/checksums.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
tarball=configure-VERSION.tar.gz
sha1=0f6355fc136bb6619585863b9e4bc954cc6e0e3d
sha256=5b618581d51997afa78b5e6647584f7ef4e6d5844823dd44e607f2accd7abba5
sha1=b08e077178bfe0d44d6028e67233ab008dd8dd05
sha256=49ef0dab4287764522f7290d51ebbfe38492b2695201d87dd8452020a3b4d9d6
2 changes: 1 addition & 1 deletion build/pkgs/configure/package-version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
b9cb7bc2559cde857d84d77f0b37a3616ce1eb6c
448f7e212e38ad2bd0ed6be7ae8cad1cb3615913
4 changes: 2 additions & 2 deletions src/sage/misc/replace_dot_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,8 @@ def walkdir_replace_dot_all(dir, file_regex=r'.*[.](py|pyx|pxi)$', package_regex
finally:
# Print report also when interrupted
if verbosity:
log_messages = sorted(log_messages.rstrip().split('\n'))
for i, message in enumerate(log_messages, start=1):
log_messages_split = sorted(log_messages.rstrip().split('\n'))
for i, message in enumerate(log_messages_split, start=1):
# add index to each line
print(f'{i}. {message.rstrip()}')
report = 'REPORT:\n'
Expand Down
34 changes: 16 additions & 18 deletions src/sage/misc/temporary_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@
# https://www.gnu.org/licenses/
# ****************************************************************************

import io
import atexit
import os
import tempfile

import atexit
from typing import IO

# Until tmp_dir() and tmp_filename() are removed, we use this directory
# as the parent for all temporary files & directories created by them.
Expand All @@ -41,7 +40,7 @@
# temporary directory
#################################################################

def tmp_dir(name='dir_', ext=''):
def tmp_dir(name='dir_', ext='') -> str:
r"""
Create and return a temporary directory in
``$HOME/.sage/temp/hostname/pid/``
Expand Down Expand Up @@ -84,7 +83,7 @@ def tmp_dir(name='dir_', ext=''):
# temporary filename
#################################################################

def tmp_filename(name='tmp_', ext=''):
def tmp_filename(name='tmp_', ext='') -> str:
r"""
Create and return a temporary file in
``$HOME/.sage/temp/hostname/pid/``
Expand Down Expand Up @@ -163,8 +162,8 @@ class atomic_write:
mode bits of the file were changed manually). (Not to be confused with
the file opening mode.)
- ``binary`` -- boolean (default: ``True`` on Python 2, ``False`` on Python
3); the underlying file is opened in binary mode. If ``False`` then it is
- ``binary`` -- boolean (default: ``False``);
the underlying file is opened in binary mode. If ``False`` then it is
opened in text mode and an encoding with which to write the file may be
supplied.
Expand Down Expand Up @@ -299,7 +298,7 @@ class atomic_write:
False
"""
def __init__(self, target_filename, append=False, mode=0o666,
binary=None, **kwargs):
binary=False, **kwargs) -> None:
"""
TESTS::
Expand All @@ -320,13 +319,11 @@ def __init__(self, target_filename, append=False, mode=0o666,
os.umask(umask)
self.mode = mode & (~umask)

# 'binary' mode is the default on Python 2, whereas 'text' mode is the
# default on Python 3--this reflects consistent handling of the default
# str type on the two platforms
self.binary = False if binary is None else binary
# 'text' mode is the default on Python 3
self.binary = binary
self.kwargs = kwargs

def __enter__(self):
def __enter__(self) -> IO:
"""
Create and return a temporary file in ``self.tmpdir`` (normally
the same directory as the target file).
Expand Down Expand Up @@ -372,7 +369,7 @@ def __enter__(self):

return self.tempfile

def __exit__(self, exc_type, exc_val, exc_tb):
def __exit__(self, exc_type, exc_val, exc_tb) -> None:
"""
If the ``with`` block was successful, move the temporary file
to the target file. Otherwise, delete the temporary file.
Expand Down Expand Up @@ -457,7 +454,7 @@ class atomic_dir:
....: h.read()
'Second'
"""
def __init__(self, target_directory):
def __init__(self, target_directory) -> None:
r"""
TESTS::
Expand Down Expand Up @@ -492,7 +489,7 @@ def __enter__(self):
self.tempname = os.path.abspath(tdir.name)
return tdir

def __exit__(self, exc_type, exc_val, exc_tb):
def __exit__(self, exc_type, exc_val, exc_tb) -> None:
"""
If the ``with`` block was successful, move the temporary directory
to the target directory. Otherwise, delete the temporary directory.
Expand All @@ -518,7 +515,8 @@ def __exit__(self, exc_type, exc_val, exc_tb):
try:
os.rename(self.tempname, self.target)
except OSError:
# Race: Another thread or process must have created the directory
# Race: Another thread or process must have created
# the directory
pass
else:
# Failure: delete temporary file
Expand All @@ -528,7 +526,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
_spyx_tmp = None


def spyx_tmp():
def spyx_tmp() -> str:
r"""
The temporary directory used to store pyx files.
Expand Down

0 comments on commit 27fce1f

Please sign in to comment.