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

is_dataclass doesn't promise DataclassInstance #4626

Closed
NeilGirdhar opened this issue Feb 12, 2023 · 1 comment
Closed

is_dataclass doesn't promise DataclassInstance #4626

NeilGirdhar opened this issue Feb 12, 2023 · 1 comment
Labels
as designed Not a bug, working as intended

Comments

@NeilGirdhar
Copy link

NeilGirdhar commented Feb 12, 2023

from dataclasses import is_dataclass

def f(X: type) -> None:
    if is_dataclass(X):
        reveal_type(X)  # information: Type of "X" is "type"

Should this be the typeshed type type[DataclassInstance]? As far as I can tell, this is

class DataclassInstance(Protocol):
    __dataclass_fields__: ClassVar[dict[str, dataclasses.Field[Any]]]

Related typing-sig post.

@erictraut
Copy link
Collaborator

erictraut commented Feb 12, 2023

I think pyright is doing the right thing here.

Here's the overloaded definition of is_dataclass from typeshed:

@overload
def is_dataclass(obj: DataclassInstance | type[DataclassInstance]) -> Literal[True]: ...
@overload
def is_dataclass(obj: type) -> TypeGuard[type[DataclassInstance]]: ...
@overload
def is_dataclass(obj: object) -> TypeGuard[DataclassInstance | type[DataclassInstance]]: ...

You are passing an argument of type type which is the equivalent of type[Any]. This matches the first overload, which does not incorporate a TypeGuard.

I recommend that the overload ordering in typeshed be swapped or the first overload be deleted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
as designed Not a bug, working as intended
Projects
None yet
Development

No branches or pull requests

2 participants