Skip to content

B006: ignore when type hints suggests immutability. #362

@randolf-scholz

Description

@randolf-scholz

When passing configurations to functions, it is quite annoying to have to do the whole Optional[Mapping[str, Any]] = None dance. It would be nice if it was possible to allow mutable defaults, when the type hints indicate immutability. (possibly via some --strict/--non-strict flag.)

from collections.abc import *

def foo(
    configA: Mapping[str, Any] = {},         # needn't raise B006
    configB: MutableMapping[str, Any] = {},  # should raise B006
    configC: dict[str, Any] = {},            # should raise B006
    seqA: Sequence[str] = [],                # needn't raise B006
    seqB: MutableSequence[str] = [],         # should raise B006
    seqC: list[str] = [],                    # should raise B006
    setA: Set[str] = set(),                  # needn't raise B006
    setB: MutableSet[str] = set(),           # should raise B006
    setB: set[str] = set(),                  # should raise B006
): ...

The reasoning is that the type checker should catch mutable behavior, and the benefit is obviously shorter function bodies that avoid the configA = {} if configA is None else configA boilerplate.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions