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

Errors monkey-patching a class method using super() #1167

Closed
timabbott opened this issue Jan 27, 2016 · 1 comment
Closed

Errors monkey-patching a class method using super() #1167

timabbott opened this issue Jan 27, 2016 · 1 comment

Comments

@timabbott
Copy link

mypy throws:

/home/tabbott/foo.py: note: In function "foo":
/home/tabbott/foo.py:10: error: "super" used outside class
/home/tabbott/foo.py: note: At top level:
/home/tabbott/foo.py:13: error: Cannot assign to a method

with the following code monkey-patching a class with inheritance:

class Base(object):
    def test(self):
        return self.val

class Test(Base):
    val = "yay"

def foo(self):
    print(super(Test, self).test())

Test.test = foo

x = Test()
x.test()

Example real code with the issue is here:
https://github.com/zulip/zulip/blob/master/zerver/lib/test_helpers.py#L104

I think we could fix the super() issue by allowing super inside a non-class method when it's first argument is self (could imagine having an annotation tag along the lines of SelfType that auto-any uses for such a function?); a similar approach might work for the other one too (you can only assign functions with a SelfType tag to class methods?)

@ddfisher ddfisher added this to the Future milestone Mar 1, 2016
@gvanrossum gvanrossum modified the milestones: 0.5, Future Oct 17, 2016
@gvanrossum gvanrossum removed this from the 0.5 milestone Mar 29, 2017
@msullivan
Copy link
Collaborator

We don't really try to support monkey patching at all

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

4 participants