简体   繁体   中英

Calling a generator object returns the error 'TypeError: 'dict' object is not callable'

I have a Generate function which I am using to generate some user data. Then passing it to a pytest fixture for use in a testcase.

However when I try to run it. I am getting TypeError: 'dict' object is not callable .

I am calling a function the returns the dictionary, and not the dictionary itself. Why am I receiving this error?

Generate.py :

class Generate(object):
    def __init__(self):
        fake = Faker()

        self.user = {
            "Username": fake.name().split(' ', 1)[0],
            "Email Address": fake.email(),
            "Password": fake.word(),
            "Todo": fake.word()
        }

    def user(self):
        new_user = self.user
        return new_user

conftest.py :

@pytest.fixture
def user():
    generator = Generate()
    user = generator.user()
    yield user

When writing generator.user , you are referencing the user attribute of your class (that you defined with self.user=... ). This is indeed a dictionary. This is hiding the method you also called user . You should probably rename one of the two, for example:

def GetUser(self):
    new_user = self.user
    return new_user

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM