Skip to content

ENH Remove import time warning for missing lzma #43495

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 13 commits into from
Nov 28, 2021
5 changes: 1 addition & 4 deletions pandas/_testing/_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
)
from pandas.compat import (
get_lzma_file,
import_lzma,
)

import pandas as pd
Expand All @@ -26,8 +25,6 @@

_RAISE_NETWORK_ERROR_DEFAULT = False

lzma = import_lzma()

# skip tests on exceptions with these messages
_network_error_messages = (
# 'urlopen error timed out',
Expand Down Expand Up @@ -387,7 +384,7 @@ def write_to_compressed(compression, path, data, dest="test"):
elif compression == "bz2":
compress_method = bz2.BZ2File
elif compression == "xz":
compress_method = get_lzma_file(lzma)
compress_method = get_lzma_file()
else:
raise ValueError(f"Unrecognized compression type: {compression}")

Expand Down
25 changes: 4 additions & 21 deletions pandas/compat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,27 +102,7 @@ def is_platform_arm() -> bool:
return platform.machine() in ("arm64", "aarch64")


def import_lzma():
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is considered to be public API and would need a deprecation cycle?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no its not

"""
Importing the `lzma` module.

Warns
-----
When the `lzma` module is not available.
"""
try:
import lzma

return lzma
except ImportError:
msg = (
"Could not import the lzma module. Your installed Python is incomplete. "
"Attempting to use lzma compression will result in a RuntimeError."
)
warnings.warn(msg)


def get_lzma_file(lzma):
def get_lzma_file():
"""
Importing the `LZMAFile` class from the `lzma` module.

Expand All @@ -137,6 +117,9 @@ def get_lzma_file(lzma):
If the `lzma` module was not imported correctly, or didn't exist.
"""
if lzma is None:
try:
import lzma
except ImportError:
raise RuntimeError(
"lzma module not available. "
"A Python re-install with the proper dependencies, "
Expand Down
4 changes: 1 addition & 3 deletions pandas/io/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@

from pandas.core.dtypes.common import is_file_like

lzma = import_lzma()

_VALID_URLS = set(uses_relative + uses_netloc + uses_params)
_VALID_URLS.discard("")

Expand Down Expand Up @@ -685,7 +683,7 @@ def get_handle(

# XZ Compression
elif compression == "xz":
handle = get_lzma_file(lzma)(handle, ioargs.mode)
handle = get_lzma_file()(handle, ioargs.mode)

# Unrecognized Compression
else:
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/io/test_compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,19 @@ def test_with_missing_lzma():
subprocess.check_output([sys.executable, "-c", code], stderr=subprocess.PIPE)


def test_lzma_not_imported():
"""Check that lzma is not imported by default"""
# https://github.com/pandas-dev/pandas/issues/43461
code = textwrap.dedent(
"""\
import sys
import pandas
assert "lzma" not in sys.modules
"""
)
subprocess.check_output([sys.executable, "-c", code], stderr=subprocess.PIPE)


def test_with_missing_lzma_runtime():
"""Tests if RuntimeError is hit when calling lzma without
having the module available.
Expand Down
5 changes: 1 addition & 4 deletions pandas/tests/io/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

from pandas.compat import (
get_lzma_file,
import_lzma,
is_platform_little_endian,
)
import pandas.util._test_decorators as td
Expand All @@ -51,8 +50,6 @@
MonthEnd,
)

lzma = import_lzma()


# TODO(ArrayManager) pickling
pytestmark = [
Expand Down Expand Up @@ -313,7 +310,7 @@ def compress_file(self, src_path, dest_path, compression):
with zipfile.ZipFile(dest_path, "w", compression=zipfile.ZIP_DEFLATED) as f:
f.write(src_path, os.path.basename(src_path))
elif compression == "xz":
f = get_lzma_file(lzma)(dest_path, "w")
f = get_lzma_file()(dest_path, "w")
else:
msg = f"Unrecognized compression type: {compression}"
raise ValueError(msg)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/io/test_stata.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from datetime import datetime
import gzip
import io
import lzma
import os
import struct
import warnings
Expand Down Expand Up @@ -1904,6 +1903,7 @@ def test_compression(compression, version, use_dict, infer):
with bz2.open(path, "rb") as comp:
fp = io.BytesIO(comp.read())
elif compression == "xz":
lzma = pytest.importorskip('lzma')
with lzma.open(path, "rb") as comp:
fp = io.BytesIO(comp.read())
elif compression is None:
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/io/xml/test_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -1044,12 +1044,12 @@ def test_wrong_compression_gz(parser, comp):

@pytest.mark.parametrize("comp", ["bz2", "gzip", "zip"])
def test_wrong_compression_xz(parser, comp):
from lzma import LZMAError
lzma = pytest.imortorskip('lzma')

with tm.ensure_clean() as path:
geom_df.to_xml(path, parser=parser, compression=comp)

with pytest.raises(LZMAError, match="Input format not supported by decoder"):
with pytest.raises(lzma.LZMAError, match="Input format not supported by decoder"):
read_xml(path, parser=parser, compression="xz")


Expand Down