Skip to content

TST: Use IPython instance fixture to avoid file leak #44700

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 1 commit into from
Dec 1, 2021
Merged
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
27 changes: 9 additions & 18 deletions pandas/tests/io/formats/test_printing.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import numpy as np
import pytest

import pandas._config.config as cf

Expand Down Expand Up @@ -120,16 +119,8 @@ def test_ambiguous_width(self):


class TestTableSchemaRepr:
@classmethod
def setup_class(cls):
pytest.importorskip("IPython")

from IPython.core.interactiveshell import InteractiveShell

cls.display_formatter = InteractiveShell.instance().display_formatter

def test_publishes(self):

def test_publishes(self, ip):
ipython = ip.instance(config=ip.config)
df = pd.DataFrame({"A": [1, 2]})
objects = [df["A"], df, df] # dataframe / series
expected_keys = [
Expand All @@ -140,13 +131,13 @@ def test_publishes(self):
opt = pd.option_context("display.html.table_schema", True)
for obj, expected in zip(objects, expected_keys):
with opt:
formatted = self.display_formatter.format(obj)
formatted = ipython.display_formatter.format(obj)
assert set(formatted[0].keys()) == expected

with_latex = pd.option_context("display.latex.repr", True)

with opt, with_latex:
formatted = self.display_formatter.format(obj)
formatted = ipython.display_formatter.format(obj)

expected = {
"text/plain",
Expand All @@ -156,7 +147,7 @@ def test_publishes(self):
}
assert set(formatted[0].keys()) == expected

def test_publishes_not_implemented(self):
def test_publishes_not_implemented(self, ip):
# column MultiIndex
# GH 15996
midx = pd.MultiIndex.from_product([["A", "B"], ["a", "b", "c"]])
Expand All @@ -165,7 +156,7 @@ def test_publishes_not_implemented(self):
opt = pd.option_context("display.html.table_schema", True)

with opt:
formatted = self.display_formatter.format(df)
formatted = ip.instance(config=ip.config).display_formatter.format(df)

expected = {"text/plain", "text/html"}
assert set(formatted[0].keys()) == expected
Expand All @@ -184,9 +175,9 @@ def test_config_default_off(self):

assert result is None

def test_enable_data_resource_formatter(self):
def test_enable_data_resource_formatter(self, ip):
# GH 10491
formatters = self.display_formatter.formatters
formatters = ip.instance(config=ip.config).display_formatter.formatters
mimetype = "application/vnd.dataresource+json"

with pd.option_context("display.html.table_schema", True):
Expand All @@ -202,4 +193,4 @@ def test_enable_data_resource_formatter(self):
assert "application/vnd.dataresource+json" in formatters
assert formatters[mimetype].enabled
# smoke test that it works
self.display_formatter.format(cf)
ip.instance(config=ip.config).display_formatter.format(cf)