Skip to content

Typing fails when list of validators where one is custom and one is built in #1197

Open
@superosku

Description

@superosku

Hi!

I am trying to add multiple validators to a field, which works fine as long as they are both from the attrs library or both written by me. When I mix them up mypy gives me an error. Am I doing something wrong here or is this an issue in the library? Here is a snippet to reproduce the issue:

from typing import Any

from attr import validators, Attribute
import attr


def is_not_empty(instance: Any, attribute: "Attribute[str]", value: str) -> Any:
    if not value.strip():
        raise ValueError("Is not empty")


@attr.s
class MyClass:
    thing1: str = attr.ib(validator=[validators.max_len(10), is_not_empty])  # Mypy complains from this
    thing2: str = attr.ib(validator=[validators.max_len(10)])  # This is fine
    thing3: str = attr.ib(validator=[is_not_empty])  # Fine as well
    thing4: str = attr.ib(validator=[is_not_empty, is_not_empty])  # Even this is fine

This is the error I get from mypy:

snippet.py:14: error: Argument "validator" has incompatible type "list[function]"; expected "Callable[[Any, Attribute[<nothing>], <nothing>], Any] | Sequence[Callable[[Any, Attribute[<nothing>], <nothing>], Any]] | None"  [arg-type]

Metadata

Metadata

Assignees

No one assigned

    Labels

    TypingTyping/stub/Mypy/PyRight related bugs.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions