Skip to content

PLR6201 should only apply to hashable types #20510

@JoElfner

Description

@JoElfner

Summary

Ruff version

ruff 0.12.12

Ruff settings

PLR6201 enabled

Problem

Fix safety description of PLR6201 is misleading:

Fix safety
This rule's fix is marked as unsafe, as the use of a set literal will error at runtime if the sequence contains unhashable elements (like lists or dictionaries). While Ruff will attempt to infer the hashability of the elements, it may not always be able to do so.

This reads as if only the hashability of the rhs-sequence was the problem. But in my opinion, the hashability of the lhs is more problematic in most use cases. F.i.

def foo(val: str | list[str] | dict[str, str]) -> bool:
    return val in {"a", "b", "c"}

Version

ruff 0.12.12

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions