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

add typing annotation in temporary_file #38646

Merged
merged 3 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading