8000 Any type causes worse type inference with higher-order function · Issue #11610 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content
Any type causes worse type inference with higher-order function #11610
Closed
@JukkaL

Description

@JukkaL

The first call to g generates an error, but the second one doesn't:

from typing import TypeVar, Callable, Iterable, Optional, List, Any

T = TypeVar("T")

def g(f: Callable[[T, T], T], seq: Iterable[Optional[T]]) -> Optional[T]: ...

a: List[Any]
# Incompatible types in assignment (expression has type "Optional[SupportsLessThanT]", variable has type "Optional[int]")
b: Optional[int] = g(min, a)  # Error!

a2: List[int]
b2: Optional[int] = g(min, a2)  # No error

The only difference between the cases is that the first one has a less precise type (List[Any] vs List[int]). Making a type less precise shouldn't generate more type errors, so this is arguably a bug. We should probably propagate the Any type from List[Any] to the return type.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongfalse-positivemypy gave an error on correct code

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0