False Warning: "Unexpected argument" when using python multiple inheritance.

Hello. My Pycharm version is 2021.1.3

An "Unexpected argument" warning occurs during Python multiple inheritance usage.

Is this Pycharm error?

class TestParentClass(object):
def __init__(self, *args, **kwargs):
pass


def ReturnClass(*args, **kwargs):
return TestParentClass()


testParentClass: TestParentClass = ReturnClass()


class TestMixin(object):
pass


class TestClass(TestParentClass, TestMixin): # class TestClass(TestMixin, TestParentClass): both
pass


b = TestClass(b=10) # There is no warning.

 

class TestParentClass(object):
def __init__(self, *args, **kwargs):
pass


def ReturnClass(*args, **kwargs) -> TestParentClass:
return TestParentClass()


testParentClass: TestParentClass = ReturnClass()


class TestMixin(object):
pass


class TestClass(testParentClass, TestMixin): # class TestClass(TestMixin, testParentClass) both
pass


b = TestClass(b=10) # Unexpected argument

 

class TestParentClass(object):
def __init__(self, *args, **kwargs):
pass


def ReturnClass(*args, **kwargs) -> TestParentClass:
return TestParentClass()


testParentClass: TestParentClass = ReturnClass()


class TestMixin(object):
def __init__(self, *args, **kwargs):
pass
pass


class TestClass(TestMixin, testParentClass): # class TestClass(testParentClass, TestMixin) both
pass


b = TestClass(b=10) # There is no warning

I understand that using Python Mixin does not define __init__. However, to remove this warning, I need to define __init__.

Is this a bug? Or does it work normally?

This issue was observed when defining a table using "from sqlalchemy.ext.declarative import descriptive_base" and mixin in SQLAlchemy.

Thank you.

2 comments
Comment actions Permalink

I'm having a similar issue as I work through "The Flask Mega-Tutorial".

In my case, I have a model that uses a mixin:

class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), index=True, unique=True)
email = db.Column(db.String(120), index=True, unique=True)
roles = db.Column(db.String(64))
password_hash = db.Column(db.String(128))

def __repr__(self):
return f'<User {self.id}, {self.username}, {self.email}, {self.roles}>'

def set_password(self, password):
self.password_hash = generate_password_hash(password)

def check_password(self, password):
return check_password_hash(self.password_hash, password)

And, when I try to instantiate this object with the following code I receive "Unexpected Argument" error on the `user = User(...` line despite the code executing perfectly well:

@app.route('/register', methods=['GET', 'POST'])
def register():
if current_user.is_authenticated:
return redirect(url_for('index'))
form = RegistrationForm()
if form.validate_on_submit():
user = User(username=form.username.data, email=form.email.data, roles=form.roles.data)
user.set_password(form.password.data)
db.session.add(user)
db.session.commit()
flash('Congratulations, you are now a registered user!')
return redirect(url_for('login'))
return render_template('register.html', title='Register', form=form)

The code above is nearly 100% pure copy of the tutorial code.

0

Please sign in to leave a comment.