Skip to content

BUG: ExcelWriter's engine and supported_extensions are properties #46444

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

Merged
merged 5 commits into from
Mar 22, 2022
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
24 changes: 10 additions & 14 deletions pandas/io/excel/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -945,9 +945,9 @@ class ExcelWriter(metaclass=abc.ABCMeta):
# - Mandatory
# - ``write_cells(self, cells, sheet_name=None, startrow=0, startcol=0)``
# --> called to write additional DataFrames to disk
# - ``supported_extensions`` (tuple of supported extensions), used to
# - ``_supported_extensions`` (tuple of supported extensions), used to
# check that engine supports the given extension.
# - ``engine`` - string that gives the engine name. Necessary to
# - ``_engine`` - string that gives the engine name. Necessary to
# instantiate class directly and bypass ``ExcelWriterMeta`` engine
# lookup.
# - ``save(self)`` --> called to save file to disk
Expand All @@ -961,6 +961,10 @@ class ExcelWriter(metaclass=abc.ABCMeta):
# You also need to register the class with ``register_writer()``.
# Technically, ExcelWriter implementations don't need to subclass
# ExcelWriter.

_engine: str
_supported_extensions: tuple[str, ...]

def __new__(
cls,
path: FilePath | WriteExcelBuffer | ExcelWriter,
Expand All @@ -983,7 +987,6 @@ def __new__(
)

# only switch class if generic(ExcelWriter)

if cls is ExcelWriter:
if engine is None or (isinstance(engine, str) and engine == "auto"):
if isinstance(path, str):
Expand Down Expand Up @@ -1027,16 +1030,14 @@ def __new__(
_path = None

@property
@abc.abstractmethod
def supported_extensions(self) -> tuple[str, ...] | list[str]:
def supported_extensions(self) -> tuple[str, ...]:
"""Extensions that writer engine supports."""
pass
return self._supported_extensions

@property
@abc.abstractmethod
def engine(self) -> str:
"""Name of engine."""
pass
return self._engine

@property
@abc.abstractmethod
Expand Down Expand Up @@ -1292,12 +1293,7 @@ def check_extension(cls, ext: str) -> Literal[True]:
"""
if ext.startswith("."):
ext = ext[1:]
# error: "Callable[[ExcelWriter], Any]" has no attribute "__iter__" (not
# iterable)
if not any(
ext in extension
for extension in cls.supported_extensions # type: ignore[attr-defined]
):
if not any(ext in extension for extension in cls._supported_extensions):
raise ValueError(f"Invalid extension for engine '{cls.engine}': '{ext}'")
else:
return True
Expand Down
4 changes: 2 additions & 2 deletions pandas/io/excel/_odswriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@


class ODSWriter(ExcelWriter):
engine = "odf"
supported_extensions = (".ods",)
_engine = "odf"
_supported_extensions = (".ods",)

def __init__(
self,
Expand Down
4 changes: 2 additions & 2 deletions pandas/io/excel/_openpyxl.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@


class OpenpyxlWriter(ExcelWriter):
engine = "openpyxl"
supported_extensions = (".xlsx", ".xlsm")
_engine = "openpyxl"
_supported_extensions = (".xlsx", ".xlsm")

def __init__(
self,
Expand Down
4 changes: 1 addition & 3 deletions pandas/io/excel/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ def register_writer(klass: ExcelWriter_t) -> None:
"""
if not callable(klass):
raise ValueError("Can only register callables as engines")
engine_name = klass.engine
# for mypy
assert isinstance(engine_name, str)
engine_name = klass._engine
_writers[engine_name] = klass


Expand Down
4 changes: 2 additions & 2 deletions pandas/io/excel/_xlsxwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ def convert(cls, style_dict, num_format_str=None):


class XlsxWriter(ExcelWriter):
engine = "xlsxwriter"
supported_extensions = (".xlsx",)
_engine = "xlsxwriter"
_supported_extensions = (".xlsx",)

def __init__(
self,
Expand Down
4 changes: 2 additions & 2 deletions pandas/io/excel/_xlwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@


class XlwtWriter(ExcelWriter):
engine = "xlwt"
supported_extensions = (".xls",)
_engine = "xlwt"
_supported_extensions = (".xls",)

def __init__(
self,
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/io/excel/test_writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1313,8 +1313,8 @@ class DummyClass(ExcelWriter):
called_save = False
called_write_cells = False
called_sheets = False
supported_extensions = ["xlsx", "xls"]
engine = "dummy"
_supported_extensions = ("xlsx", "xls")
_engine = "dummy"

def book(self):
pass
Expand Down