Skip to content

Adding variable annotation causes incompatible type when Optional and TypeVars are used #4554

Open
@euresti

Description

@euresti
Contributor

Found while playing around with attrs (now in the typeshed) and using --strict-optional:

$ mypy --version
mypy 0.570-dev-f3491a320ffa36e6574c4370489d9f4819d7fa83 (master)

$ python --version
Python 3.6.3
# Run with --strict-optional
from typing import TypeVar, Callable, Optional, List
_T = TypeVar('_T')
def make(typ: Callable[[], _T]) -> _T:
    ...
def accept(arg: Optional[_T]) -> _T:
    ...
accept(make(list))
accept(None)
foo = accept(make(list))                   # This works
bar: List[str] = accept(make(list))    # Argument 1 to "accept" has incompatible type "List[_T]"; expected "Optional[List[str]]"

Note: The real code has more arguments and an overload that handles the None case but I cut it down to the shortest repro.

The attrs version of this code is:

import attr
x: List[str] = attr.ib(attr.Factory(list))

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @euresti@ilevkivskyi

        Issue actions

          Adding variable annotation causes incompatible type when Optional and TypeVars are used · Issue #4554 · python/mypy