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

self passed to class methods in Python 3 #22

Open
NextDesign1 opened this issue Feb 10, 2019 · 7 comments
Open

self passed to class methods in Python 3 #22

NextDesign1 opened this issue Feb 10, 2019 · 7 comments
Labels

Comments

@NextDesign1
Copy link
Collaborator

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.

@JPFrancoia
Copy link

Thanks for reporting the issue, I noticed it as well.

Just to make sure, because of this issue, does it mean I can't use Auger with python 3? Or does it just concern a few edge cases?

@NextDesign1
Copy link
Collaborator Author

NextDesign1 commented 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.

@NextDesign1
Copy link
Collaborator Author

NextDesign1 commented Feb 12, 2019

I took at look at this quickly, and unfortunately there doesn't seem to be a quick fix that I can see at the moment.

Auger under Python 2 generates the following information for your function:

call:    [({'to_up': 'hello world'}, 'HELLO WORLD')]
definer: upper.UpperObject
member:  <unbound method UpperObject.parse>
target:  upperobject_instance
name:    parse
ismod?:  False
static?: False
method?: True
func?:   False

While under Python 3, it generates:

call:    [({'self': <upper.UpperObject object at 0x10afd3358>, 'to_up': 'hello world'}, 'HELLO WORLD')]
definer: <class 'upper.UpperObject'>
member:  <function UpperObject.parse at 0x10afca598>
target:  UpperObject
name:    parse
ismod?:  False
static?: False
method?: False
func?:   True

So it looks like Python 3 handles things a bit differently when it comes to inspecting code. I'll continue to poke about.

@JPFrancoia
Copy link

Thank you for investigating this. What's the problem with what python 3 returns? I'm probably over looking that, but why not fix the code used for inspection to make sure it returns what python 2 was returning?

@NextDesign1
Copy link
Collaborator Author

That's the idea. What I posted above was to show why the behaviour between python 2 and 3 is different.

@NextDesign1
Copy link
Collaborator Author

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

@stefdworschak
Copy link

stefdworschak commented Sep 10, 2020

Hi there, just wanted to see if there are any updates on fixing this issue? I'd really like to use this in a project, but using Python2 is somewhat limiting as I mostly use Python3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants