Skip to content

Commit 04f9253

Browse files
committed
add typing annotation in temporary_file
1 parent 24698e7 commit 04f9253

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

src/sage/misc/temporary_file.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,10 @@
2323
# https://www.gnu.org/licenses/
2424
# ****************************************************************************
2525

26-
import io
26+
import atexit
2727
import os
2828
import tempfile
2929

30-
import atexit
31-
3230
# Until tmp_dir() and tmp_filename() are removed, we use this directory
3331
# as the parent for all temporary files & directories created by them.
3432
# This lets us clean up after those two functions when sage exits normally
@@ -41,7 +39,7 @@
4139
# temporary directory
4240
#################################################################
4341

44-
def tmp_dir(name='dir_', ext=''):
42+
def tmp_dir(name='dir_', ext='') -> str:
4543
r"""
4644
Create and return a temporary directory in
4745
``$HOME/.sage/temp/hostname/pid/``
@@ -84,7 +82,7 @@ def tmp_dir(name='dir_', ext=''):
8482
# temporary filename
8583
#################################################################
8684

87-
def tmp_filename(name='tmp_', ext=''):
85+
def tmp_filename(name='tmp_', ext='') -> str:
8886
r"""
8987
Create and return a temporary file in
9088
``$HOME/.sage/temp/hostname/pid/``
@@ -163,8 +161,8 @@ class atomic_write:
163161
mode bits of the file were changed manually). (Not to be confused with
164162
the file opening mode.)
165163
166-
- ``binary`` -- boolean (default: ``True`` on Python 2, ``False`` on Python
167-
3); the underlying file is opened in binary mode. If ``False`` then it is
164+
- ``binary`` -- boolean (default: ``False``);
165+
the underlying file is opened in binary mode. If ``False`` then it is
168166
opened in text mode and an encoding with which to write the file may be
169167
supplied.
170168
@@ -299,7 +297,7 @@ class atomic_write:
299297
False
300298
"""
301299
def __init__(self, target_filename, append=False, mode=0o666,
302-
binary=None, **kwargs):
300+
binary=False, **kwargs) -> None:
303301
"""
304302
TESTS::
305303
@@ -320,13 +318,11 @@ def __init__(self, target_filename, append=False, mode=0o666,
320318
os.umask(umask)
321319
self.mode = mode & (~umask)
322320

323-
# 'binary' mode is the default on Python 2, whereas 'text' mode is the
324-
# default on Python 3--this reflects consistent handling of the default
325-
# str type on the two platforms
326-
self.binary = False if binary is None else binary
321+
# 'text' mode is the default on Python 3
322+
self.binary = binary
327323
self.kwargs = kwargs
328324

329-
def __enter__(self):
325+
def __enter__(self) -> str:
330326
"""
331327
Create and return a temporary file in ``self.tmpdir`` (normally
332328
the same directory as the target file).
@@ -372,7 +368,7 @@ def __enter__(self):
372368

373369
return self.tempfile
374370

375-
def __exit__(self, exc_type, exc_val, exc_tb):
371+
def __exit__(self, exc_type, exc_val, exc_tb) -> None:
376372
"""
377373
If the ``with`` block was successful, move the temporary file
378374
to the target file. Otherwise, delete the temporary file.
@@ -457,7 +453,7 @@ class atomic_dir:
457453
....: h.read()
458454
'Second'
459455
"""
460-
def __init__(self, target_directory):
456+
def __init__(self, target_directory) -> None:
461457
r"""
462458
TESTS::
463459
@@ -473,7 +469,7 @@ def __init__(self, target_directory):
473469
self.target = os.path.realpath(target_directory)
474470
self.tmpdir = os.path.dirname(self.target)
475471

476-
def __enter__(self):
472+
def __enter__(self) -> str:
477473
r"""
478474
Create and return a temporary directory in ``self.tmpdir`` (normally
479475
the same directory as the target file).
@@ -492,7 +488,7 @@ def __enter__(self):
492488
self.tempname = os.path.abspath(tdir.name)
493489
return tdir
494490

495-
def __exit__(self, exc_type, exc_val, exc_tb):
491+
def __exit__(self, exc_type, exc_val, exc_tb) -> None:
496492
"""
497493
If the ``with`` block was successful, move the temporary directory
498494
to the target directory. Otherwise, delete the temporary directory.
@@ -518,7 +514,8 @@ def __exit__(self, exc_type, exc_val, exc_tb):
518514
try:
519515
os.rename(self.tempname, self.target)
520516
except OSError:
521-
# Race: Another thread or process must have created the directory
517+
# Race: Another thread or process must have created
518+
# the directory
522519
pass
523520
else:
524521
# Failure: delete temporary file
@@ -528,7 +525,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
528525
_spyx_tmp = None
529526

530527

531-
def spyx_tmp():
528+
def spyx_tmp() -> str:
532529
r"""
533530
The temporary directory used to store pyx files.
534531

0 commit comments

Comments
 (0)