Python · correctness
prefer-non-nullable-collection
python:prefer-non-nullable-collection Avoid nullable list parameters when local use proves `None` and an empty list are equivalent.
- Code
- SARJ082
- Default
- error
- Fix
- none
- Languages
- python
Why
Exposing two equivalent empty states expands the function contract without preserving meaningful information.
Fix
Require the list, or accept an immutable empty default such as `Sequence[T] = ()` and materialize a list internally.
Before / after
Executed by this rule’s unit tests.
Before
Nullable list is immediately normalized
def resolve(candidates: list[str] | None = None) -> list[str]:
return candidates or []
After
Required list has one empty state
def resolve(candidates: list[str]) -> list[str]:
return candidates
Limits
- Only module functions and constructors with a nullable list defaulted to `None` are analyzed.
- Overrides, tests, generated code, nested captures, multiple reads, and uses that preserve `None` as a distinct state are excluded.