Skip to content

Backport PR #49736 on branch 1.5.x (Fix: Treat Generic classes as not being is_list_like) #50233

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 3 commits into from
Dec 13, 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Bug fixes
~~~~~~~~~
- Bug in :meth:`.Styler.to_excel` leading to error when unrecognized ``border-style`` (e.g. ``"hair"``) provided to Excel writers (:issue:`48649`)
- Bug when chaining several :meth:`.Styler.concat` calls, only the last styler was concatenated (:issue:`49207`)
- Fixed bug when instantiating a :class:`DataFrame` subclass inheriting from ``typing.Generic`` that triggered a ``UserWarning`` on python 3.11 (:issue:`49649`)
-

.. ---------------------------------------------------------------------------
Expand Down
8 changes: 6 additions & 2 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from collections import abc
from decimal import Decimal
from enum import Enum
from typing import Literal
from typing import (
Literal,
_GenericAlias,
)
import warnings

cimport cython
Expand Down Expand Up @@ -1136,7 +1139,8 @@ cdef inline bint c_is_list_like(object obj, bint allow_sets) except -1:
# equiv: `isinstance(obj, abc.Iterable)`
getattr(obj, "__iter__", None) is not None and not isinstance(obj, type)
# we do not count strings/unicode/bytes as list-like
and not isinstance(obj, (str, bytes))
# exclude Generic types that have __iter__
and not isinstance(obj, (str, bytes, _GenericAlias))
# exclude zero-dimensional duck-arrays, effectively scalars
and not (hasattr(obj, "ndim") and obj.ndim == 0)
# exclude sets if allow_sets is False
Expand Down
20 changes: 20 additions & 0 deletions pandas/tests/dtypes/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
from numbers import Number
import re
import sys
from typing import (
Generic,
TypeVar,
)

import numpy as np
import pytest
Expand Down Expand Up @@ -228,6 +232,22 @@ def __getitem__(self, item):
assert not inference.is_list_like(NotListLike())


def test_is_list_like_generic():
# GH 49649
# is_list_like was yielding false positives for Generic classes in python 3.11
T = TypeVar("T")

class MyDataFrame(DataFrame, Generic[T]):
...

tstc = MyDataFrame[int]
tst = MyDataFrame[int]({"x": [1, 2, 3]})

assert not inference.is_list_like(tstc)
assert isinstance(tst, DataFrame)
assert inference.is_list_like(tst)


def test_is_sequence():
is_seq = inference.is_sequence
assert is_seq((1, 2))
Expand Down