Skip to content

flake8-bugbear B903 suggestion : Do not raise error if keyword argument is present and target-python version is less or equals than 3.9 #15548

@guillaumeLepape

Description

@guillaumeLepape

Hello,

I have this piece of code in one of my repo

class RuleMetadata:
    def __init__(
        self,
        *,
        function: Union[
            Callable[[Session, UserModel, RuleComputeFinaleFromGroupRank], None],
            Callable[[Session, UserModel, RuleComputePoints], None],
        ],
        attribute: str,
        required_admin: bool = False,
    ) -> None:
        self.function = function
        self.attribute = attribute
        self.required_admin = required_admin

When I did this, I started by using dataclass, although I wanted the 3 arguments of the __init__ function to be keywords only. As dataclass does not support kw_only before python 3.10 (reference https://docs.python.org/3/library/dataclasses.html#module-contents), I ended up writing the __init__ function myself.

With the current implementation of B903, this will raise an error. My suggestion would be not to raise error if an keyword only argument and target-version<=3.9.

I'm currently raising a pull request, if you think my suggestion is ok, I'll let you review.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ruleImplementing or modifying a lint rule

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions