Skip to content

Constructor defined after classmethod is considered to have no args in the classmethod #1727

Closed

Description

@AlexOnAPeak

The type of cls within bar() in the following code is def () -> Foo unless the __init__ is moved to before the classmethod:

class Foo(object):

  @classmethod
  def bar(cls):
    # type: () -> Foo
    reveal_type(cls)
    return cls("bar")

  def __init__(self, baz):
    # type: (str) -> None
    self.baz = baz

Activity

  1. added this to the milestone on Jun 23, 2016
  2. rzimmerman commented on Sep 5, 2016

    @rzimmerman

    Probably related:

    class Example:
    
        @classmethod
        def example(cls, arg1: float, arg2: str) -> Example:
            return cls(arg1=arg1, arg2=arg2)
    
        def __init__(self, arg1: float, arg2: str) -> None:
            pass

    Reports:

    example3.py: note: In member "example" of class "Example":
    example3.py:7: error: Unexpected keyword argument "arg1" for "Example"
    example3.py:7: error: Unexpected keyword argument "arg2" for "Example"
    

    But passes checks if __init__ is moved before the classmethod.

    Adding reveal_type with the __init__ method last shows:
    example3.py:7: error: Revealed type is 'def () -> example3.Example'

    And with __init__ first:
    example3.py:11: error: Revealed type is 'def (arg1: builtins.float, arg2: builtins.str) -> example3.Example'

  3. removed this from the milestone on Mar 29, 2017
  4. ilevkivskyi commented on Aug 7, 2017

    @ilevkivskyi
    Member

    (Raising priority to normal, since this have been reported twice after the original issue.)

  5. Lordnibbler commented on Aug 7, 2017

    @Lordnibbler

    +1 on this, thanks @ilevkivskyi

  6. matthewgrossman commented on Aug 9, 2017

    @matthewgrossman

    +1, this is also affecting my work

  7. mckaysalisbury commented on Nov 30, 2017

    @mckaysalisbury

    Yeah, my behavior shows that it sometimes works in the wrong order. (see #4290)

  8. JelleZijlstra commented on Dec 5, 2017

    @JelleZijlstra
    Member

    Bumping to high-pri because this is happening frequently and the workaround is non-obvious (see #2324).

  9. self-assigned this
    on Feb 21, 2018
  10. 2 remaining items

  11. JukkaL commented on Aug 16, 2018

    @JukkaL
    Collaborator

    There probably isn't any particular reason why this hasn't been fixed -- other than there being a lot of other high-priority work as well.

    This doesn't sound like it would be very hard to fix (but probably not entirely trivial either). The core team is planning to allocate a block of time focused on fixing high-priority bugs in the next couple of months, and hopefully this gets fixed then.

  12. added a commit that references this issue on Aug 18, 2018
    b39eb0f
  13. VadimPushtaev commented on Aug 18, 2018

    @VadimPushtaev

    I've spent some time trying to find a solution, and here is my attempt: #5501

  14. added 5 commits that reference this issue on Aug 19, 2018
    d5ed26a
    05b8dff
    b0f7a24
    9b07239
    60ca471
  15. self-assigned this
    on Sep 19, 2018
  16. msullivan commented on Sep 19, 2018

    @msullivan
    Collaborator

    I've got a PR about to go up that fixes this as a side effect of changing the type of classmethod first arguments to be a TypeType

  17. ilevkivskyi commented on Sep 19, 2018

    @ilevkivskyi
    Member

    @msullivan There is an existing PR. Please take a look at the questions that appeared there (interactions with fine-grained mode, plugins, forward references, generics, and overloads).

  18. ilevkivskyi commented on Sep 19, 2018

    @ilevkivskyi
    Member

    @msullivan Forgot to add the PR number, here it is #5501

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

Metadata

Metadata

Labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions