Skip to content

What is the proper way to ignore “missing attribute” errors in mixins? #5868

Closed
@kurtgn

Description

@kurtgn

I cannot get mypy to work properly with mixins: it keeps complaining that my mixins reference missing attributes. Consider this example:

class PrintValueMixin:
    """A mixin that displays values"""

    def print_value(self) -> None:
        print(self.value)


class Obj(PrintValueMixin):
    """An object that stores values. It needs a mixin to display its values"""

    def __init__(self, value):
        self.value = value


instance = Obj(1)
instance.print_value()

If I run mypy on this file, I get an error:

error: "PrintValueMixin" has no attribute "value"

Of course it has no attribute "value". It is a mixin, it should not have its own attributes.

So what is the proper way to deal with typechecking mixins?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions