Skip to content

Callables like operator.call produce arg-type error with generic or overloaded functions #14337

Description

@gschaffner

Bug Report

operator.call and similarly-typed callables produce an arg-type error when used with many generic functions.

Additionally, if the arg-type error is # type: ignore'd, the inferred type is a TypeVar (removed from its scope!) rather than Any.

To Reproduce

importoperatorfromcollections.abcimportCallablefromtypingimportTypeVarfromtypingimportassert_typefromtypingimportreveal_typeT=TypeVar("T")
T0=TypeVar("T0")
U=TypeVar("U") # this is redundant, but it helps understand mypy's outputdefcall2(cb: Callable[[T0], T], arg0: T0, /) ->T:
# similar to operator.call, but only takes one posarg (doesn't use ParamSpec)returncb(arg0)
defident(x: U) ->U:
returnxassert_type(operator.call(ident, "foo"), str)
# error: Expression is of type "U", not "str" [assert-type]# error: Argument 2 to "call" has incompatible type "str"; expected "U" [arg-type]assert_type(call2(ident, "foo"), str)
# error: Expression is of type "U", not "str" [assert-type]# error: Argument 1 to "call" has incompatible type "Callable[[U], U]"; expected "Callable[[str], U]" [arg-type]x=operator.call(ident, "foo") # type: ignore[arg-type]reveal_type(x) # should be inferred as Any# note: Revealed type is "U`-1"

(https://mypy-play.net/?mypy=latest&python=3.11&gist=a66524b9632714157bd87ef422725e41)

Expected Behavior

The inferred type of operator.call(ident, "foo") should match the inferred type of ident("foo"). (Aside: should this be str or Literal["foo"]?)

Comparing to other type checkers, call2(ident, "foo") works with pyre (inferred as Literal["foo"]), sort-of-works with pyright (inferred as U@ident | str), and doesn't work with pytype (inferred as Any).

Also, if # type: ignore[arg-type] is used on the arg-type error, the inferred type should by Any, not a TypeVar removed from its scope.

Actual Behavior

See comments in the reproducer above.

Your Environment

  • Mypy version used: 0.991 (compiled) and latest master (uncompiled)
  • Mypy command-line flags: none required to reproduce
  • Mypy configuration options from mypy.ini (and other config files): none required to reproduce
  • Python version used: 3.11, 3.10, 3.9, 3.8, 3.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions