python / python/mypy

Sequences of attrs validators

Open
#20,013 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-attrs topic-inference topic-join-v-union topic-type-context
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

It's only a minor issue, but there was a recent fix on the attrs side to properly handle lists of validators. While code execution works, mypy is still giving a false positive.

To Reproduce

from attrs import define, field
from attrs.validators import deep_iterable, instance_of, min_len


@define
class A:
    x: list[str] = field(
        validator=deep_iterable(
            member_validator=[instance_of(str), min_len(1)],
            iterable_validator=[instance_of(list), min_len(1)],
        ),
    )

A(['a', 'b'])  # works

Expected Behavior

Should not raise any mypy errors.

Actual Behavior

dbg.py:9: error: Argument "member_validator" to "deep_iterable" has incompatible type "list[function]"; expected "Callable[[Any, Attribute[Never], Never], Any] | Sequence[Callable[[Any, Attribute[Never], Never], Any]]"  [arg-type]
dbg.py:10: error: Argument "iterable_validator" to "deep_iterable" has incompatible type "list[function]"; expected "Callable[[Any, Attribute[Never], Never], Any] | Sequence[Callable[[Any, Attribute[Never], Never], Any]] | None"  [arg-type]

Your Environment

  • Mypy version used: 1.18.2
  • Attrs version used: 25.4.0
  • Python version used: 3.10

Contributor guide

Open the contributing guide

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

Start by reproducing the example with mypy 1.18.2 and inspect the typing for attrs.validators.deep_iterable. Add a regression test covering lists of validators for member_validator and iterable_validator, then verify that mypy reports no errors for the example.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.