Typing of `optional` validator fails

Open
#799 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

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

Research direction

Start by reproducing the two MdParserConfig examples with the provided mypy configuration. Inspect the typing of optional and deep_iterable validators and how their inferred types interact with attr.ib. Done means both the None default and the tuple default pass mypy type checking without an incompatible validator error.

Written by the indexing model from the issue text.

Description

Bug Typing

Heya, minor issue with typing:

This passes mypy type checking:

@attr.s()
class MdParserConfig:

    url_schemes: Optional[Iterable[str]] = attr.ib(
        default=None,
        validator=optional(deep_iterable(instance_of(str), instance_of((list, tuple)))),
    )

but then weirdly changing the default to anything else fails, e.g.

@attr.s()
class MdParserConfig:

    url_schemes: Optional[Iterable[str]] = attr.ib(
        default=("http", "https"),
        validator=optional(deep_iterable(instance_of(str), instance_of((list, tuple)))),
    )

leads to:

error: Argument "validator" has incompatible type "Callable[[Any, Attribute[Union[List[Any], Tuple[Any, ...], None]], Union[List[Any], Tuple[Any, ...], None]], Any]"; expected "Union[Callable[[Any, Attribute[Tuple[str, str]], Tuple[str, str]], Any], Sequence[Callable[[Any, Attribute[Tuple[str, str]], Tuple[str, str]], Any]], None]"  [arg-type]

for reference, this is my mypy configuration

[mypy]
show_error_codes = true
check_untyped_defs = true
strict_equality = true
no_implicit_optional = true
warn_unused_ignores = true

thanks in advance!

Dominant language
Python
Stars
5.8k
Forks
480
Avg merge
2h 15m
Merged PRs (30d)
2

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.

More from python-attrs/attrs

All issues in python-attrs/attrs

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.