basedmypy icon indicating copy to clipboard operation
basedmypy copied to clipboard

Support for `ReifiedGeneric`s

Open KotlinIsland opened this issue 4 years ago • 6 comments

Reified Generics in basedtyping need special casing in basedmypy.

isinstance and issubclass:

class Foo(ReifiedGeneric[T]): ...

isinstance(Foo[int](), Foo[int])

Ban TypeVars:

def foo(t: T):
    a: ReifiedList[T]  # should be an error

KotlinIsland avatar Oct 21 '21 22:10 KotlinIsland

mypy doesnt need to be changed to accommodate for the ReifiedGenerics i'm adding to basedtyping

DetachHead avatar Dec 23 '21 06:12 DetachHead

actually it does, since isinstance and issubclass have hardcoded functionality in mypy so we need to add logic to not show this error if it's a ReifiedGeneric

isinstance(Reified[int, str](), Reified[int, str])  # Parameterized generics cannot be used with class or instance checks  [misc]

DetachHead avatar Dec 28 '21 04:12 DetachHead

i was originally going to makeour own is_instance function using a TypeGuard

_T_type = TypeVar("_T_type", bound=type)


@overload
def is_instance(__obj: object, __class_or_tuple: _T_type) -> TypeGuard[_T_type]:
    ...


@overload
def is_instance(
    __obj: object,
    __class_or_tuple: UnionType | tuple[type | UnionType | tuple[object, ...], ...],
) -> bool:
    ...


def is_instance(
    __obj: object,
    __class_or_tuple: type
    | UnionType
    | tuple[type | UnionType | tuple[object, ...], ...],
) -> bool:
    return isinstance(__obj, __class_or_tuple)

but that didn't work due to https://github.com/python/mypy/issues/11428

DetachHead avatar Dec 28 '21 04:12 DetachHead

I think modifying mypy checks are much better idea than making a typeguard

KotlinIsland avatar Dec 28 '21 06:12 KotlinIsland

perhaps, but i prefer using existing functionality instead of intrinsic hidden behavior where possible. the fact that isinstance and issubclass don't even use TypeGuards seems very sus to me

DetachHead avatar Dec 28 '21 12:12 DetachHead

imo TypeGuards are really broken atm and the intrinsic behavior is much better, so until typeguards are fixed I wouldn't bother.

KotlinIsland avatar Dec 30 '21 05:12 KotlinIsland