Skip to content

Commit 48a4064

Browse files
committed
Fix: Treat Generic classes as not being is_list_like
1 parent bf676a2 commit 48a4064

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

doc/source/whatsnew/v1.5.2.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Fixed regressions
2727
Bug fixes
2828
~~~~~~~~~
2929
- Bug in the Copy-on-Write implementation losing track of views in certain chained indexing cases (:issue:`48996`)
30-
-
30+
- Bug with python 3.11 and creating a subclass that is ``Generic`` (:issue:`49649`)
3131

3232
.. ---------------------------------------------------------------------------
3333
.. _whatsnew_152.other:

pandas/_libs/lib.pyx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
from collections import abc
22
from decimal import Decimal
33
from enum import Enum
4-
from typing import Literal
4+
from typing import (
5+
Literal,
6+
_GenericAlias,
7+
)
58

69
cimport cython
710
from cpython.datetime cimport (
@@ -1100,6 +1103,8 @@ cdef inline bint c_is_list_like(object obj, bint allow_sets) except -1:
11001103
and not (hasattr(obj, "ndim") and obj.ndim == 0)
11011104
# exclude sets if allow_sets is False
11021105
and not (allow_sets is False and isinstance(obj, abc.Set))
1106+
# exclude Generic types that have __iter__
1107+
and not isinstance(obj, _GenericAlias)
11031108
)
11041109

11051110

pandas/tests/dtypes/test_inference.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818
from numbers import Number
1919
import re
2020
import sys
21-
from typing import Iterator
21+
from typing import (
22+
Generic,
23+
Iterator,
24+
TypeVar,
25+
)
2226

2327
import numpy as np
2428
import pytest
@@ -229,6 +233,23 @@ def __getitem__(self, item):
229233
assert not inference.is_list_like(NotListLike())
230234

231235

236+
def test_is_list_like_generic():
237+
# GH 49649
238+
# is_list_like was yielding false positives for Generic classes in python 3.11
239+
T = TypeVar("T")
240+
241+
class Base:
242+
def __init__(self, x: int):
243+
self._x = x
244+
245+
class Gen(Base, Generic[T]):
246+
...
247+
248+
fooc = Gen[float]
249+
250+
assert not inference.is_list_like(fooc)
251+
252+
232253
def test_is_sequence():
233254
is_seq = inference.is_sequence
234255
assert is_seq((1, 2))

0 commit comments

Comments
 (0)