python / python/mypy

False positive with attrs converter

Open
#8,625 4 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-attrs
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Looks like mypy can't infer generic parameter properly when used in attrs converter.

from attr import attrs, attrib
from typing import FrozenSet, Text


@attrs
class Test(object):
    values = attrib(type=FrozenSet[Text], converter=frozenset)


Test(values={'a'})

Actual: reporting error

error: Argument 1 to <set> has incompatible type "str"; expected "_T"

Expected: no errors.

mypy version 0.770, python 3.5.2, no flags are used.
Latest master has basically the same error message except generic param has a different name:

error: Argument 1 to <set> has incompatible type "str"; expected "_T_co"

Possibly related: https://github.com/python/mypy/issues/5313

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 running the supplied Python reproducer with the reported mypy version or latest master, focusing on the attrs converter and generic-parameter inference. The work is done when the example produces no type-checking errors while preserving the declared FrozenSet[Text] type.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.