Skip to content
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

Support for ReifiedGenerics #5

Open
KotlinIsland opened this issue Oct 21, 2021 · 6 comments
Open

Support for ReifiedGenerics #5

KotlinIsland opened this issue Oct 21, 2021 · 6 comments

Comments

@KotlinIsland
Copy link
Owner

KotlinIsland commented Oct 21, 2021

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
@DetachHead
Copy link
Collaborator

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

@DetachHead
Copy link
Collaborator

DetachHead commented Dec 28, 2021

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 DetachHead reopened this Dec 28, 2021
@DetachHead
Copy link
Collaborator

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 python#11428

@KotlinIsland
Copy link
Owner Author

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

@DetachHead
Copy link
Collaborator

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

@KotlinIsland
Copy link
Owner Author

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

@DetachHead DetachHead changed the title Support ReifiedGenerics Support ReifiedGenerics (isinstance and issubclass) Jan 1, 2022
@KotlinIsland KotlinIsland changed the title Support ReifiedGenerics (isinstance and issubclass) Support for ReifiedGenerics Jan 4, 2022
@KotlinIsland KotlinIsland added this to the 1.4 milestone Mar 25, 2022
@KotlinIsland KotlinIsland removed this from the 1.4 milestone Jan 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants