question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

self passed to class methods in Python 3

See original GitHub issue

The test case provided by @JPFrancoia in issue #20 demonstrates a change in behaviour between Python 2 and 3.

In Python 2, the generated test case is:

import unittest
import upper
from upper import UpperObject

class UpperTest(unittest.TestCase):
    def test_parse(self):
        upperobject_instance = UpperObject()
        self.assertEqual(
            upperobject_instance.parse(to_up='hello world'),
            'HELLO WORLD'
        )

if __name__ == "__main__":
    unittest.main()

While in Python 3, it is generated as:

from typing import Dict
from typing import List
import unittest
import upper
from upper import UpperObject

class UpperTest(unittest.TestCase):
    def test_parse(self):
        self.assertEqual(
            UpperObject.parse(self=<upper.UpperObject object at 0x105b29240>,to_up='hello world'),
            'HELLO WORLD'
        )

if __name__ == "__main__":
    unittest.main()

Notice that self has been passed into UpperObject.parse in the test case generated by Python 3. This should be investigated, as this unit test will not run.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:7

github_iconTop GitHub Comments

2reactions
NextDesign1commented, Feb 11, 2019

At the moment, you would have to post-process the output so that you instantiate the object first. You could try find & replacing “UpperObject.” with “UpperObject()”, and then “self=<[^>]+>” with nothing.

Another option would be moving the method out of a class. This may or may not be possible with your existing code base. Traditional functions shouldn’t be affected by this bug. See sample/gentests.py and sample/functions.py for an example on how you would set this up.

I’ll take a look at this this afternoon. Hopefully we’ll be able to get this sorted out for you quickly.

1reaction
NextDesign1commented, Feb 12, 2019

Ok, I think I know what the underlying issue is. Python 3 removed unbound methods, which our code was expecting.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can I pass self as the first argument for class methods in python
I have seen some usage where they have defined the class as a class method but they still pass self as the first...
Read more >
self in Python class - GeeksforGeeks
Self is the first argument to be passed in Constructor and Instance Method. Self must be provided as a First parameter to the...
Read more >
How to use self in Python – explained with examples
This article helps you acquire knowledge on the self variable in Python and its internal working in Python.
Read more >
Self in Python Class | What is the Use of Python Self? - Edureka
The self is used to represent the instance of the class. With this keyword, you can access the attributes and methods of the...
Read more >
self in Python, Demystified - Programiz
The self keyword is used to represent an instance (object) of the given class. In this case, the two Cat objects cat1 and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found