More principled approach for callable vs callable inference by ilevkivskyi · Pull Request #15910 · python/mypy (original) (raw)
Fixes #702 (one of the oldest open issues)
The approach is quite simple, I essentially replicate the logic from subtyping check, while replacing each is_subtype()
call with infer_constraints()
call. Note that we don't have various options available in constraints.py
so I use all checks, even those that may be skipped with some strictness flags (so we can infer as many constraints as possible). Depending on the output of mypy_primer
we can try to tune this.
Note that while I was looking at subtyping code, I noticed couple inconsistencies for ParamSpecs, I added TODOs for them (and updated some existing TODOs). I also deleted some code that should be dead code after my previous cleanup.
Among inconsistencies most notably, subtyping between Parameters
uses wrong (opposite) direction. Normally, Parameters
entity behaves covariantly (w.r.t. types of individual arguments) as a single big argument, like a tuple plus a map. But then this entity appears in a contravariant position in Callable
. This is how we handle it in constraints.py
, join.py
, meet.py
etc. I tried to fix the left/right order in visit_parameters()
, but then one test failed (and btw same test would also fail if I would try to fix variance in visit_instance()
). I decided to leave this for separate PR(s).
cc @A5rocks