From 6d41728e943db08fe40b3bf4a293b710b5c6aa52 Mon Sep 17 00:00:00 2001 From: Thomas Li Date: Wed, 6 Jan 2021 12:59:25 -0800 Subject: [PATCH 1/2] CLN: inspect_excel_format --- pandas/io/excel/_base.py | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/pandas/io/excel/_base.py b/pandas/io/excel/_base.py index b0ec8a1082a0e..259cc3eb91e94 100644 --- a/pandas/io/excel/_base.py +++ b/pandas/io/excel/_base.py @@ -906,24 +906,18 @@ def close(self): @doc(storage_options=_shared_docs["storage_options"]) def inspect_excel_format( - path: Optional[str] = None, - content: Union[None, BufferedIOBase, RawIOBase, bytes] = None, + content_or_path: Union[None, str, BufferedIOBase, RawIOBase, IO[bytes]] = None, storage_options: StorageOptions = None, ) -> str: """ Inspect the path or content of an excel file and get its format. - At least one of path or content must be not None. If both are not None, - content will take precedence. - Adopted from xlrd: https://github.com/python-excel/xlrd. Parameters ---------- - path : str, optional - Path to file to inspect. May be a URL. - content : file-like object, optional - Content of file to inspect. + content_or_path : str or file-like object + Path to file or content of file to inspect. May be a URL. {storage_options} Returns @@ -938,12 +932,9 @@ def inspect_excel_format( BadZipFile If resulting stream does not have an XLS signature and is not a valid zipfile. """ - content_or_path: Union[None, str, BufferedIOBase, RawIOBase, IO[bytes]] - if isinstance(content, bytes): - content_or_path = BytesIO(content) - else: - content_or_path = content or path assert content_or_path is not None + if isinstance(content_or_path, bytes): + content_or_path = BytesIO(content_or_path) with get_handle( content_or_path, "rb", storage_options=storage_options, is_text=False @@ -1069,7 +1060,7 @@ def __init__( ext = "xls" else: ext = inspect_excel_format( - content=path_or_buffer, storage_options=storage_options + content_or_path=path_or_buffer, storage_options=storage_options ) if engine is None: From a95952ee717702609058bd8c1a86a718b2556851 Mon Sep 17 00:00:00 2001 From: Thomas Li Date: Fri, 8 Jan 2021 07:32:36 -0800 Subject: [PATCH 2/2] Changes from code review --- pandas/io/excel/_base.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pandas/io/excel/_base.py b/pandas/io/excel/_base.py index 259cc3eb91e94..8911696230c03 100644 --- a/pandas/io/excel/_base.py +++ b/pandas/io/excel/_base.py @@ -2,10 +2,10 @@ import datetime from distutils.version import LooseVersion import inspect -from io import BufferedIOBase, BytesIO, RawIOBase +from io import BytesIO import os from textwrap import fill -from typing import IO, Any, Dict, Mapping, Optional, Union, cast +from typing import Any, Dict, Mapping, Optional, Union, cast import warnings import zipfile @@ -906,7 +906,7 @@ def close(self): @doc(storage_options=_shared_docs["storage_options"]) def inspect_excel_format( - content_or_path: Union[None, str, BufferedIOBase, RawIOBase, IO[bytes]] = None, + content_or_path: FilePathOrBuffer, storage_options: StorageOptions = None, ) -> str: """ @@ -932,7 +932,6 @@ def inspect_excel_format( BadZipFile If resulting stream does not have an XLS signature and is not a valid zipfile. """ - assert content_or_path is not None if isinstance(content_or_path, bytes): content_or_path = BytesIO(content_or_path)