Commit f5ce4ee
authored
Fix joining a function against metaclass-using object constructors (#13648)
This pull request fixes #9838.
It turns out that when an object is using a metaclass, it uses that
metaclass as the fallback instead of `builtins.type`.
This caused the `if t.fallback.type.fullname != "builtins.type"` check
we were performing in `join_similar_callables` and
combine_similar_callables` to pick the wrong fallback in the case where
we were attempting to join a function against a constructor for an
object that used a metaclass.
This ended up causing a crash later for basically the exact same reason
discussed in #13576: using `abc.ABCMeta` causes `Callable.is_type_obj()`
to return true, which causes us to enter a codepath where we call
`Callable.type_object()`. But this function is not prepared to handle
the case where the return type of the callable is a Union, causing an
assert to fail.
I opted to fix this by adjusting the join algorithm so it does `if
t.fallback.type.fullname == "builtins.function"`.
One question I did punt on -- what should happen in the case where one
of the fallbacks is `builtins.type` and the other is a metaclass?
I suspect it's impossible for this case to actually occur: I think mypy
would opt to use the algorithm for joining two `Type[...]` entities
instead of these callable joining algorithms. While I'm not 100% sure of
this, the current approach of just arbitrarily picking one of the two
fallbacks seemed good enough for now.1 parent 5094460 commit f5ce4ee
2 files changed
Lines changed: 25 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
559 | 559 | | |
560 | 560 | | |
561 | 561 | | |
562 | | - | |
563 | | - | |
564 | | - | |
565 | | - | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
566 | 566 | | |
567 | 567 | | |
568 | 568 | | |
| |||
580 | 580 | | |
581 | 581 | | |
582 | 582 | | |
583 | | - | |
584 | | - | |
585 | | - | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
586 | 587 | | |
587 | 588 | | |
588 | 589 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
865 | 865 | | |
866 | 866 | | |
867 | 867 | | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
| 871 | + | |
| 872 | + | |
| 873 | + | |
| 874 | + | |
| 875 | + | |
| 876 | + | |
| 877 | + | |
| 878 | + | |
| 879 | + | |
| 880 | + | |
| 881 | + | |
| 882 | + | |
| 883 | + | |
| 884 | + | |
868 | 885 | | |
869 | 886 | | |
870 | 887 | | |
| |||
0 commit comments