pandas-dev / pandas

Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more
https://pandas.pydata.org
BSD 3-Clause "New" or "Revised" License
43.43k stars 17.85k forks source link

BUG: TestTableSchemaRepr::test_publishes raises AttributeError on mac #58055

Open jbrockmendel opened 6 months ago

jbrockmendel commented 6 months ago
TestTableSchemaRepr::test_publishes - AttributeError: '_GeneratorContextManager' object has no attribute 'args'

I've been seeing this for a couple weeks.

lithomas1 commented 6 months ago

Can you paste the traceback?

jbrockmendel commented 6 months ago
_________________________________________________________ TestTableSchemaRepr.test_publishes __________________________________________________________

self = <pandas.tests.io.formats.test_ipython_compat.TestTableSchemaRepr object at 0x12bb71890>
ip = <IPython.core.interactiveshell.InteractiveShell object at 0x2cc064e10>

    def test_publishes(self, ip):
        ipython = ip.instance(config=ip.config)
        df = DataFrame({"A": [1, 2]})
        objects = [df["A"], df]  # dataframe / series
        expected_keys = [
            {"text/plain", "application/vnd.dataresource+json"},
            {"text/plain", "text/html", "application/vnd.dataresource+json"},
        ]

        opt = cf.option_context("display.html.table_schema", True)
        last_obj = None
        for obj, expected in zip(objects, expected_keys):
            last_obj = obj
>           with opt:

pandas/tests/io/formats/test_ipython_compat.py:25: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <contextlib._GeneratorContextManager object at 0x2d15ac210>

    def __enter__(self):
        # do not keep args and kwds alive unnecessarily
        # they are only needed for recreation, which is not possible anymore
>       del self.args, self.kwds, self.func
E       AttributeError: '_GeneratorContextManager' object has no attribute 'args'

../../../.pyenv/versions/3.11.7/lib/python3.11/contextlib.py:135: AttributeError
lithomas1 commented 6 months ago

This diff fixes it for me, not sure why though.

Maybe it's an ipython bug?

(opt is cf.option_context("display.html.table_schema", True))

diff --git a/pandas/tests/io/formats/test_ipython_compat.py b/pandas/tests/io/formats/test_ipython_compat.py
index 8512f41396..67bb0fb9e3 100644
--- a/pandas/tests/io/formats/test_ipython_compat.py
+++ b/pandas/tests/io/formats/test_ipython_compat.py
@@ -22,7 +22,7 @@ class TestTableSchemaRepr:
         last_obj = None
         for obj, expected in zip(objects, expected_keys):
             last_obj = obj
-            with opt:
+            with cf.option_context("display.html.table_schema", True):
                 formatted = ipython.display_formatter.format(obj)
             assert set(formatted[0].keys()) == expected
jbrockmendel commented 6 months ago

That fixes it for me too

Aloqeely commented 5 months ago

Happens to me on Windows as well, and that fixes it for me My guess is that this context manager is not re-usable