Skip to content

Commit f9c4fdf

Browse files
authored
use more precise return type for gzip.open() (#2792)
1 parent 831c0df commit f9c4fdf

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

torchvision/datasets/mnist.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import torch
88
import codecs
99
import string
10+
import gzip
11+
import lzma
1012
from typing import Any, Callable, Dict, IO, List, Optional, Tuple, Union
1113
from .utils import download_url, download_and_extract_archive, extract_archive, \
1214
verify_str_arg
@@ -435,17 +437,15 @@ def get_int(b: bytes) -> int:
435437
return int(codecs.encode(b, 'hex'), 16)
436438

437439

438-
def open_maybe_compressed_file(path: Union[str, IO]) -> IO:
440+
def open_maybe_compressed_file(path: Union[str, IO]) -> Union[IO, gzip.GzipFile]:
439441
"""Return a file object that possibly decompresses 'path' on the fly.
440442
Decompression occurs when argument `path` is a string and ends with '.gz' or '.xz'.
441443
"""
442444
if not isinstance(path, torch._six.string_classes):
443445
return path
444446
if path.endswith('.gz'):
445-
import gzip
446447
return gzip.open(path, 'rb')
447448
if path.endswith('.xz'):
448-
import lzma
449449
return lzma.open(path, 'rb')
450450
return open(path, 'rb')
451451

0 commit comments

Comments
 (0)