Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
euresti opened this issue Feb 8, 2018 · 1 comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal

Comments

@euresti
Copy link
Contributor

euresti commented Feb 8, 2018

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))
@ilevkivskyi
Copy link
Member

I think this is another manifestation (if not a duplicate) of #1317

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal
Projects
None yet
Development

No branches or pull requests

2 participants