From dbb42ab3fddc43a7d01245f77ae191cd9baca42b Mon Sep 17 00:00:00 2001 From: dangotbanned <125183946+dangotbanned@users.noreply.github.com> Date: Wed, 25 Jun 2025 10:55:57 +0100 Subject: [PATCH] chore(typing): More `isinstance_or_issubclass` overloads - Related (#2717) While I was looking for unreachable code, I noticed some `isinstance_or_issubclass` cases were marked as unreachable. They *are* reachable, but when I wrote (#1997) we only had up to a **3**-tuple. The current max of **7** wasn't covered, so this PR fixes that --- narwhals/_utils.py | 74 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/narwhals/_utils.py b/narwhals/_utils.py index 7c4f4202eb..1251c070d5 100644 --- a/narwhals/_utils.py +++ b/narwhals/_utils.py @@ -105,6 +105,10 @@ _T1 = TypeVar("_T1") _T2 = TypeVar("_T2") _T3 = TypeVar("_T3") + _T4 = TypeVar("_T4") + _T5 = TypeVar("_T5") + _T6 = TypeVar("_T6") + _T7 = TypeVar("_T7") _Fn = TypeVar("_Fn", bound="Callable[..., Any]") P = ParamSpec("P") R = TypeVar("R") @@ -718,6 +722,76 @@ def isinstance_or_issubclass( ) -> TypeIs[_T1 | _T2 | _T3 | type[_T1 | _T2 | _T3]]: ... +@overload +def isinstance_or_issubclass( + obj_or_cls: type, cls_or_tuple: tuple[type[_T1], type[_T2], type[_T3], type[_T4]] +) -> TypeIs[type[_T1 | _T2 | _T3 | _T4]]: ... + + +@overload +def isinstance_or_issubclass( + obj_or_cls: object | type, + cls_or_tuple: tuple[type[_T1], type[_T2], type[_T3], type[_T4]], +) -> TypeIs[_T1 | _T2 | _T3 | _T4 | type[_T1 | _T2 | _T3 | _T4]]: ... + + +@overload +def isinstance_or_issubclass( + obj_or_cls: type, + cls_or_tuple: tuple[type[_T1], type[_T2], type[_T3], type[_T4], type[_T5]], +) -> TypeIs[type[_T1 | _T2 | _T3 | _T4 | _T5]]: ... + + +@overload +def isinstance_or_issubclass( + obj_or_cls: object | type, + cls_or_tuple: tuple[type[_T1], type[_T2], type[_T3], type[_T4], type[_T5]], +) -> TypeIs[_T1 | _T2 | _T3 | _T4 | _T5 | type[_T1 | _T2 | _T3 | _T4 | _T5]]: ... + + +@overload +def isinstance_or_issubclass( + obj_or_cls: type, + cls_or_tuple: tuple[type[_T1], type[_T2], type[_T3], type[_T4], type[_T5], type[_T6]], +) -> TypeIs[type[_T1 | _T2 | _T3 | _T4 | _T5 | _T6]]: ... + + +@overload +def isinstance_or_issubclass( + obj_or_cls: object | type, + cls_or_tuple: tuple[type[_T1], type[_T2], type[_T3], type[_T4], type[_T5], type[_T6]], +) -> TypeIs[ + _T1 | _T2 | _T3 | _T4 | _T5 | _T6 | type[_T1 | _T2 | _T3 | _T4 | _T5 | _T6] +]: ... + + +@overload +def isinstance_or_issubclass( + obj_or_cls: type, + cls_or_tuple: tuple[ + type[_T1], type[_T2], type[_T3], type[_T4], type[_T5], type[_T6], type[_T7] + ], +) -> TypeIs[type[_T1 | _T2 | _T3 | _T4 | _T5 | _T6 | _T7]]: ... + + +@overload +def isinstance_or_issubclass( + obj_or_cls: object | type, + cls_or_tuple: tuple[ + type[_T1], type[_T2], type[_T3], type[_T4], type[_T5], type[_T6], type[_T7] + ], +) -> TypeIs[ + _T1 + | _T2 + | _T3 + | _T4 + | _T5 + | _T6 + | _T7 + | type[_T1 | _T2 | _T3 | _T4 | _T5 | _T6 | _T7] +]: ... + + @overload def isinstance_or_issubclass( obj_or_cls: Any, cls_or_tuple: tuple[type, ...]