Skip to content

Typing issue (stubs) for converter with default #518

Open
@Funth0mas

Description

@Funth0mas

When using attr.ibute with both converter and default, wrong expectations is applied on default type (with mypy == 0.670). Mypy dev say it's an issue in attr stubs.

The valid code

import attr
import typing

def to_int(x: typing.Any) -> int:
    return int(x)

@attr.s
class C:
    a: int = attr.ib(converter=to_int, default="1")

results in:

main.py:9: error: Incompatible types in assignment (expression has type "str", variable has type "int")
main.py:9: error: Argument "converter" has incompatible type "Callable[[Any], int]"; expected "Optional[Callable[[Any], str]]"

The result is not right, because the converter is applied on default value as well.

Of course the hotfix is to convert the default value manually.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugTypingTyping/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