mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Fixes type inference for generic calls in `if` expr

Open sobolevn opened this issue 4 years ago • 8 comments

Closes #11049

sobolevn avatar Sep 17 '21 15:09 sobolevn

Diff from mypy_primer, showing the effect of this PR on open source code:

prefect (https://github.com/PrefectHQ/prefect.git)
+ src/prefect/client/client.py:1267: error: No overload variant of "sorted" matches argument type "object"
+ src/prefect/client/client.py:1267: note: Possible overload variant:
+ src/prefect/client/client.py:1267: note:     def [SupportsLessThanT <: SupportsLessThan] sorted(__iterable, Iterable[SupportsLessThanT], *, key: None = ..., reverse: bool = ...) -> List[SupportsLessThanT]
+ src/prefect/client/client.py:1267: note:     <1 more non-matching overload not shown>
+ src/prefect/client/client.py:1269: error: No overload variant of "sorted" matches argument type "object"
+ src/prefect/client/client.py:1269: note: Possible overload variant:
+ src/prefect/client/client.py:1269: note:     def [SupportsLessThanT <: SupportsLessThan] sorted(__iterable, Iterable[SupportsLessThanT], *, key: None = ..., reverse: bool = ...) -> List[SupportsLessThanT]
+ src/prefect/client/client.py:1269: note:     <1 more non-matching overload not shown>
+ src/prefect/core/task.py:351: error: Unsupported left operand type for | ("object")
+ src/prefect/core/flow.py:1622: error: No overload variant of "list" matches argument type "object"
+ src/prefect/core/flow.py:1622: note: Possible overload variant:
+ src/prefect/core/flow.py:1622: note:     def [_T] __init__(self, iterable: Iterable[_T]) -> List[_T]
+ src/prefect/core/flow.py:1622: note:     <1 more non-matching overload not shown>
+ src/prefect/core/flow.py:1624: error: No overload variant of "list" matches argument type "object"
+ src/prefect/core/flow.py:1624: note: Possible overload variant:
+ src/prefect/core/flow.py:1624: note:     def [_T] __init__(self, iterable: Iterable[_T]) -> List[_T]
+ src/prefect/core/flow.py:1624: note:     <1 more non-matching overload not shown>
+ src/prefect/core/flow.py:1718: error: "object" has no attribute "update"
+ src/prefect/core/flow.py:1719: error: "object" has no attribute "update"
+ src/prefect/agent/agent.py:778: error: Argument "labels" to "RunConfig" has incompatible type "object"; expected "Optional[Iterable[str]]"

mypy (https://github.com/python/mypy.git)
+ mypyc/analysis/dataflow.py:366: error: Incompatible return value type (got "Tuple[Set[Value], object]", expected "Tuple[Set[Value], Set[Value]]")  [return-value]
+ mypy/semanal_typeddict.py:167: note: (Skipping most remaining errors due to unresolved imports or missing stubs; fix these first)

aiohttp (https://github.com/aio-libs/aiohttp.git)
+ aiohttp/cookiejar.py:227: error: Incompatible types in assignment (expression has type "Union[SimpleCookie[str], BaseCookie[<nothing>]]", variable has type "Union[SimpleCookie[str], BaseCookie[str]]")  [assignment]

ibis (https://github.com/ibis-project/ibis.git)
+ ibis/expr/api.py:4255: error: Incompatible types in assignment (expression has type "Union[List[Expr], List[<nothing>]]", variable has type "Optional[List[Expr]]")
+ ibis/expr/api.py:4255: error: Argument 1 to "promote_list" has incompatible type "List[Expr]"; expected "List[<nothing>]"
+ ibis/expr/api.py:4256: error: Item "None" of "Optional[List[Expr]]" has no attribute "extend"
+ ibis/expr/api.py:4263: error: Argument 1 to "get_mutation_exprs" has incompatible type "Optional[List[Expr]]"; expected "List[Expr]"

github-actions[bot] avatar Sep 17 '21 16:09 github-actions[bot]

Diff from mypy_primer, showing the effect of this PR on open source code:

ibis (https://github.com/ibis-project/ibis.git)
+ ibis/expr/api.py:4255: error: Argument 1 to "promote_list" has incompatible type "List[Expr]"; expected "List[<nothing>]"

github-actions[bot] avatar Sep 17 '21 16:09 github-actions[bot]

Diff from mypy_primer, showing the effect of this PR on open source code:

ibis (https://github.com/ibis-project/ibis.git)
+ ibis/expr/api.py:4255: error: Argument 1 to "promote_list" has incompatible type "List[Expr]"; expected "List[<nothing>]"

github-actions[bot] avatar Sep 17 '21 17:09 github-actions[bot]

mypy_primer output seems related, but it is not directly caused by this change: this also happens on latest master. Снимок экрана 2021-09-17 в 20 13 02

sobolevn avatar Sep 17 '21 17:09 sobolevn

Ok, I will try to figure out what is going on. Looks like other commits into master do not produce this problem. 🤔

sobolevn avatar Sep 18 '21 14:09 sobolevn

Yes, this problem is reproduced on master with:

from typing import TypeVar, Union, List

T1 = TypeVar("T1")

def promote_list(item: Union[T1, List[T1]]) -> List[T1]:
    ...

exprs: List[int]
reveal_type(promote_list(exprs))

I've opened a new issue: https://github.com/python/mypy/issues/11149

So, this can be considered as "ready to be reviewed".

sobolevn avatar Sep 20 '21 14:09 sobolevn

FYI this has a merge conflict.

JelleZijlstra avatar Sep 29 '21 04:09 JelleZijlstra

@JelleZijlstra thanks! Resolved.

sobolevn avatar Sep 29 '21 07:09 sobolevn