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.

Peewee calls __init__ on model unexpectedly

See original GitHub issue

We’ve run into some trouble when upgrading to peewee 2.8.0, when we have a custom __init__ defined on our models. This behaviour worked fine when on earlier versions (2.7.3 is the other version I tested).

import peewee as pw

db = pw.SqliteDatabase(':memory:')


class TestTable1(pw.Model):

    a = pw.IntegerField()
    b = pw.IntegerField(default=2)

    class Meta:
        database = db


class TestTable2(pw.Model):

    def __init__(self, a=2):
        pw.Model.__init__(self, a=a)

    a = pw.IntegerField()
    b = pw.IntegerField(default=2)

    class Meta:
        database = db


db.connect()

db.create_tables([TestTable1, TestTable2])

# This works fine
TestTable1.create(a=2)
t1 = TestTable1.select().get()

# This fails
TestTable2.create(a=2)
t2 = TestTable2.select().get()  # Failure with: TypeError: __init__() got an unexpected keyword argument 'b'

In the code above, the failure occurs in the final select, it seems that when the model instance is created, the code in playhouse._speedups._ModelQueryResultWrapper.process_row uses __init__ to create an instance of the row with the full results of the query passed as kwargs (i.e. a=2, b=2). However, the TestTable2.__init__ does not support b as a keyword argument.

It’s not clear from the documentation how well I should expect custom __init__ methods to work. However, I would imagine that this could be fixed by making playhouse._speedups._ModelQueryResultWrapper.process_row explicitly call pw.Model.__init__ on an instance created via __new__.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
coleifercommented, Aug 12, 2018

You can still put it in __init__() with the caveat that __init__() is going to be called not just when instantiating objects yourself, but also every time a Car instance is read from a database cursor. I think you probably could make a classmethod on your Car object and use that as a factory for complex logic?

2reactions
lezcommented, Feb 17, 2016

Try modifying your __init__ to:

def __init__(self, a=2, **kwargs):
    pw.Model.__init__(self, a=a, **kwargs)
Read more comments on GitHub >

github_iconTop Results From Across the Web

how to initialise a peewee.Model object? __init__() doesn't work
I am not able to initialise the fields of a "peewee.Model" descendant object with the usual init() ...
Read more >
Database — peewee 3.15.4 documentation
Sometimes the database connection settings are not known until run-time, when these values may be loaded from a configuration file or the environment....
Read more >
Unexpected key error from peewee (Example) - Treehouse
Getting an error with peewee and not sure what is going on. Here is my terminal log. (api) Is-iMac:Flask_Social hellspawn$ python app.py /Users/ ......
Read more >
Source code for peewee
This check ensures that we drop the # table alias into the query instead of trying to parameterize a # model (for instance,...
Read more >
"connection already closed", how to re-establish connection?
I don't know about the Bottle-Peewee plugin, but the problem is most likely due to your application not properly managing connections. ... __init__.py...
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