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

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

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

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

sametmax opened this issue Nov 7, 2016 · 13 comments

Comments

@sametmax
Copy link

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
Copy link
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.

@sametmax
Copy link
Author

sametmax commented Nov 7, 2016

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

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

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

@elazarg
Copy link
Contributor

elazarg commented Nov 12, 2016

Related: #2392

@TRManderson
Copy link
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
Copy link
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)

@ilevkivskyi
Copy link
Member

It looks like this issue is now fixed by #2475

@JukkaL
Copy link
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
Copy link
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).

@JukkaL
Copy link
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
Copy link
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
Copy link
Contributor

elazarg commented Feb 7, 2017

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

JukkaL pushed a commit that referenced this issue Feb 7, 2017
Fix subissue of #2413.

Follow-up for #2475

* check for member in fallback

* check for fallback only for type objects
@JukkaL
Copy link
Collaborator

JukkaL commented Feb 7, 2017

#2822 fixed the remaining issue.

@JukkaL JukkaL closed this as completed Feb 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants