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

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

Closed
sametmax opened this Issue Nov 7, 2016 · 13 comments

Comments

Projects
None yet
6 participants
@sametmax

sametmax commented Nov 7, 2016

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.

@gvanrossum

This comment has been minimized.

Member

gvanrossum commented Nov 7, 2016

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.

@sametmax

This comment has been minimized.

sametmax commented Nov 7, 2016

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

@sametmax sametmax changed the title from "Unsupported left operand type for >>" while __rshift__ is defined to Mypy should look in metaclass for operations on class objects Nov 7, 2016

@gvanrossum

This comment has been minimized.

Member

gvanrossum commented Nov 7, 2016

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

@elazarg

This comment has been minimized.

Contributor

elazarg commented Nov 12, 2016

Related: #2392

@TRManderson

This comment has been minimized.

Contributor

TRManderson commented Nov 18, 2016

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.

@TRManderson

This comment has been minimized.

Contributor

TRManderson commented Nov 18, 2016

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)
@ilevkivskyi

This comment has been minimized.

Collaborator

ilevkivskyi commented Feb 7, 2017

It looks like this issue is now fixed by #2475

@JukkaL

This comment has been minimized.

Collaborator

JukkaL commented Feb 7, 2017

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")
@ilevkivskyi

This comment has been minimized.

Collaborator

ilevkivskyi commented Feb 7, 2017

@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).

@JukkaL

This comment has been minimized.

Collaborator

JukkaL commented Feb 7, 2017

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.

@elazarg

This comment has been minimized.

Contributor

elazarg commented Feb 7, 2017

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

@elazarg

This comment has been minimized.

Contributor

elazarg commented Feb 7, 2017

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

JukkaL added a commit that referenced this issue Feb 7, 2017

Check for member in type-fallback (#2822)
Fix subissue of #2413.

Follow-up for #2475

* check for member in fallback

* check for fallback only for type objects
@JukkaL

This comment has been minimized.

Collaborator

JukkaL commented Feb 7, 2017

#2822 fixed the remaining issue.

@JukkaL JukkaL closed this Feb 7, 2017

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