Skip to content

Dataclass classmethods don't work #5974

Closed

Description

@catern

On master and 0.630, the following code fails with error: Incompatible return value type (got "Foo", expected "T"):

from dataclasses import dataclass
import typing as t

T = t.TypeVar('T', bound="Foo")
@dataclass
class Foo:
    @classmethod
    def make(cls: t.Type[T]) -> T:
        return cls()

If I remove the dataclass annotation, it works fine.

Activity

  1. JelleZijlstra commented on Nov 29, 2018

    @JelleZijlstra
    Member

    cc @euresti since this seems to be due to the dataclasses plugin.

  2. euresti commented on Nov 29, 2018

    @euresti
    Contributor

    Hmm. In my code I usually annotate this as:

    @dataclass
    class Foo:
        @classmethod
        def make(cls) -> 'Foo':
            return cls()
    

    But I hardly use inheritance.

    Also I don't know why the decorator would break it. Does it happens with @attr.s?

  3. ilevkivskyi commented on Nov 29, 2018

    @ilevkivskyi
    Member

    This works on master

  4. ilevkivskyi commented on Nov 29, 2018

    @ilevkivskyi
    Member

    Also this is a duplicate of #5263 that was fixed by #5646

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions