match-class-positional-attributes / R1906ΒΆ

Message emitted:

Use keyword attributes instead of positional ones (%s)

Description:

Keyword attributes are more explicit and slightly faster since CPython can skip the `__match_args__` lookup.

Problematic code:

class Book:
    __match_args__ = ("title", "year")

    def __init__(self, title, year):
        self.title = title
        self.year = year


def func(item: Book):
    match item:
        case Book("abc", 2000):  # [match-class-positional-attributes]
            ...

Correct code:

class Book:
    __match_args__ = ("title", "year")

    def __init__(self, title, year):
        self.title = title
        self.year = year


def func(item: Book):
    match item:
        case Book(title="abc", year=2000):
            ...

Related links:

Created by the match_statements checker.