Skip to content

Mypy should look in metaclass for operations on class objects #2413

Closed

Description

@sametmax

Mypy complains about this line:

StringWrapper >> "foo"

With:

Unsupported left operand type for >> ("StringWrapper")

However, __rshift__ is defined on StringWrapper AND on StringWrapper's metaclass.

I understand the notation seems strange, calling shift on a class, but I'm making a library in which this API makes sense. The related code works, and applying >> does call __rshift__ on the metaclass. The only problem is mypy mentioning it. I can force ignore it, but I wish that dev using my lib won't have to do so.

Activity

  1. gvanrossum commented on Nov 7, 2016

    @gvanrossum
    Member

    Does this work as expected when the left arg is not a class? I suspect mypy is treating classes specially somehow and not looking at the metaclass.

  2. sametmax commented on Nov 7, 2016

    @sametmax
    Author

    Just tried, indeed with StringWrapper() >> "foo", no more warning.

  3. changed the title [-]"Unsupported left operand type for >>" while __rshift__ is defined[/-] [+]Mypy should look in metaclass for operations on class objects[/+] on Nov 7, 2016
  4. gvanrossum commented on Nov 7, 2016

    @gvanrossum
    Member

    OK, I've changed the title accordingly (I don't think it's specific to >>).

  5. elazarg commented on Nov 12, 2016

    @elazarg
    Contributor

    Related: #2392

  6. TRManderson commented on Nov 18, 2016

    @TRManderson
    Contributor

    A large source of "errors" for me is using metaclasses for class properties, here's a minimal example:

    class Meta(type):
        @property
        def prop(cls):
            return "Hi!"
    
    class Concrete(object, metaclass=Meta):
        pass
    
    print(Concrete.prop)
    

    I suspect it'll apply for anything to do with metaclasses though (as Guido suggested), given the two examples here are pretty disparate.

  7. TRManderson commented on Nov 18, 2016

    @TRManderson
    Contributor

    Yep it's just metaclass stuff in general

    class Meta(type):
        attr = "attr"
    
        def fn(cls):
            return "fn"
    
    class Concrete(object, metaclass=Meta):
        pass
    
    print(Concrete.fn())
    print(Concrete.attr)
    
  8. ilevkivskyi commented on Feb 7, 2017

    @ilevkivskyi
    Member

    It looks like this issue is now fixed by #2475

  9. JukkaL commented on Feb 7, 2017

    @JukkaL
    Collaborator

    The property, attribute and method examples now work.

    __rshift__ still doesn't seem to work:

    class Meta(type):
        def __rshift__(self, x):
            return 'foo'
    
    class Concrete(object, metaclass=Meta):
        pass
    
    print(Concrete >> 1)  # Unsupported operand types for >> ("Concrete" and "int")
    
  10. ilevkivskyi commented on Feb 7, 2017

    @ilevkivskyi
    Member

    @JukkaL Hm, this is very strange, I tried this:

    class Meta(type):
        def __rshift__(self, x):
            ...
    
    class Concrete(metaclass=Meta):
        pass
    
    print(Concrete >> 'foo')
    

    And it works (note that I use a string, like in OP example, not an integer).

  11. JukkaL commented on Feb 7, 2017

    @JukkaL
    Collaborator

    I can confirm that this behaves differently for int and str operands. It may have something to do with integers also defining __rshift__ and friends.

  12. elazarg commented on Feb 7, 2017

    @elazarg
    Contributor

    Yes, it's failing for all types but the reverse method hides it for int. Maybe the lookup in has_member should be different.

  13. elazarg commented on Feb 7, 2017

    @elazarg
    Contributor

    (Sorry, scratch the above: more likely is that the reverse method prohibits the lookup)

  14. added a commit that references this issue on Feb 7, 2017
  15. JukkaL commented on Feb 7, 2017

    @JukkaL
    Collaborator

    #2822 fixed the remaining issue.

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions