PyCQA / PyCQA/flake8-bugbear

B006: ignore when type hints suggests immutability.

Open
#362 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.1k
Forks
123
Avg merge
2d 5h
Merged PRs (30d)
5

Description

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No file or test is named. Start by locating the existing B006 check and reviewing how its current mutable-default cases are tested; compare the issue's immutable and mutable type-hint examples. Done means defining the strictness behavior and covering the accepted and rejected annotations with tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.