Repository navigation
Commit 658150d
committed
Make isinstance/issubclass generate ad-hoc intersections
This diff makes `isinstance(...)` and `issubclass(...)` try
generating ad-hoc intersections of Instances when possible.
For example, we previously concluded the if-branch is unreachable
in the following program. This PR makes mypy infer an ad-hoc
intersection instead.
class A: pass
class B: pass
x: A
if isinstance(x, B):
reveal_type(x) # N: Revealed type is 'test.<subclass of "A" and "B">'
If you try doing an `isinstance(...)` that legitimately is impossible
due to conflicting method signatures or MRO issues, we continue to
declare the branch unreachable. Passing in the `--warn-unreachable`
flag will now also report an error about this:
# flags: --warn-unreachable
x: str
# E: Subclass of "str" and "bytes" cannot exist: would have
# incompatible method signatures
if isinstance(x, bytes):
reveal_type(x) # E: Statement is unreachable
This error message has the same limitations as the other
`--warn-unreachable` ones: we suppress them if the isinstance check
is inside a function using TypeVars with multiple values.
However, we *do* end up always inferring an intersection type when
possible -- that logic is never suppressed.
I initially thought we might have to suppress the new logic as well
(see python#3603 (comment)),
but it turns out this is a non-issue in practice once you add in
the check that disallows impossible intersections.
For example, when I tried running this PR on the larger of our two
internal codebases, I found about 25 distinct errors, all of which
were legitimate and unrelated to the problem discussed in the PR.
(And if we don't suppress the extra error message, we get about
100-120 errors, mostly due to tests repeatdly doing `result = blah()`
followed by `assert isinstance(result, X)` where X keeps changing.)1 parent 861f01c commit 658150d
9 files changed
Lines changed: 500 additions & 40 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
41 | | - | |
| 41 | + | |
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
| |||
63 | 63 | | |
64 | 64 | | |
65 | 65 | | |
66 | | - | |
| 66 | + | |
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
| |||
1963 | 1963 | | |
1964 | 1964 | | |
1965 | 1965 | | |
1966 | | - | |
1967 | | - | |
1968 | | - | |
| 1966 | + | |
1969 | 1967 | | |
1970 | 1968 | | |
1971 | 1969 | | |
1972 | 1970 | | |
| 1971 | + | |
| 1972 | + | |
| 1973 | + | |
| 1974 | + | |
1973 | 1975 | | |
1974 | 1976 | | |
1975 | 1977 | | |
| |||
3636 | 3638 | | |
3637 | 3639 | | |
3638 | 3640 | | |
| 3641 | + | |
| 3642 | + | |
| 3643 | + | |
| 3644 | + | |
| 3645 | + | |
| 3646 | + | |
| 3647 | + | |
| 3648 | + | |
| 3649 | + | |
| 3650 | + | |
| 3651 | + | |
| 3652 | + | |
| 3653 | + | |
| 3654 | + | |
| 3655 | + | |
| 3656 | + | |
| 3657 | + | |
| 3658 | + | |
| 3659 | + | |
| 3660 | + | |
| 3661 | + | |
| 3662 | + | |
| 3663 | + | |
| 3664 | + | |
| 3665 | + | |
| 3666 | + | |
| 3667 | + | |
| 3668 | + | |
| 3669 | + | |
| 3670 | + | |
| 3671 | + | |
| 3672 | + | |
| 3673 | + | |
| 3674 | + | |
| 3675 | + | |
| 3676 | + | |
| 3677 | + | |
| 3678 | + | |
| 3679 | + | |
| 3680 | + | |
| 3681 | + | |
| 3682 | + | |
| 3683 | + | |
| 3684 | + | |
| 3685 | + | |
| 3686 | + | |
| 3687 | + | |
| 3688 | + | |
| 3689 | + | |
| 3690 | + | |
| 3691 | + | |
| 3692 | + | |
| 3693 | + | |
| 3694 | + | |
| 3695 | + | |
| 3696 | + | |
| 3697 | + | |
| 3698 | + | |
| 3699 | + | |
| 3700 | + | |
| 3701 | + | |
| 3702 | + | |
| 3703 | + | |
| 3704 | + | |
| 3705 | + | |
| 3706 | + | |
| 3707 | + | |
| 3708 | + | |
| 3709 | + | |
| 3710 | + | |
| 3711 | + | |
| 3712 | + | |
3639 | 3713 | | |
3640 | 3714 | | |
3641 | 3715 | | |
| |||
3650 | 3724 | | |
3651 | 3725 | | |
3652 | 3726 | | |
3653 | | - | |
3654 | | - | |
3655 | | - | |
3656 | | - | |
3657 | | - | |
3658 | | - | |
| 3727 | + | |
3659 | 3728 | | |
3660 | | - | |
3661 | | - | |
3662 | | - | |
3663 | | - | |
3664 | | - | |
3665 | | - | |
3666 | | - | |
| 3729 | + | |
3667 | 3730 | | |
3668 | 3731 | | |
3669 | 3732 | | |
| |||
3828 | 3891 | | |
3829 | 3892 | | |
3830 | 3893 | | |
3831 | | - | |
3832 | | - | |
3833 | | - | |
| 3894 | + | |
| 3895 | + | |
| 3896 | + | |
| 3897 | + | |
| 3898 | + | |
3834 | 3899 | | |
3835 | 3900 | | |
3836 | 3901 | | |
| |||
4309 | 4374 | | |
4310 | 4375 | | |
4311 | 4376 | | |
| 4377 | + | |
| 4378 | + | |
| 4379 | + | |
| 4380 | + | |
4312 | 4381 | | |
4313 | 4382 | | |
4314 | 4383 | | |
| |||
4726 | 4795 | | |
4727 | 4796 | | |
4728 | 4797 | | |
4729 | | - | |
| 4798 | + | |
4730 | 4799 | | |
4731 | 4800 | | |
4732 | 4801 | | |
| 4802 | + | |
| 4803 | + | |
| 4804 | + | |
| 4805 | + | |
| 4806 | + | |
| 4807 | + | |
| 4808 | + | |
| 4809 | + | |
| 4810 | + | |
| 4811 | + | |
| 4812 | + | |
| 4813 | + | |
| 4814 | + | |
| 4815 | + | |
| 4816 | + | |
| 4817 | + | |
| 4818 | + | |
| 4819 | + | |
| 4820 | + | |
| 4821 | + | |
| 4822 | + | |
| 4823 | + | |
| 4824 | + | |
| 4825 | + | |
| 4826 | + | |
| 4827 | + | |
| 4828 | + | |
| 4829 | + | |
| 4830 | + | |
| 4831 | + | |
| 4832 | + | |
| 4833 | + | |
| 4834 | + | |
| 4835 | + | |
| 4836 | + | |
| 4837 | + | |
| 4838 | + | |
| 4839 | + | |
4733 | 4840 | | |
4734 | 4841 | | |
4735 | 4842 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
290 | 290 | | |
291 | 291 | | |
292 | 292 | | |
293 | | - | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
294 | 298 | | |
295 | 299 | | |
296 | 300 | | |
| |||
623 | 627 | | |
624 | 628 | | |
625 | 629 | | |
626 | | - | |
| 630 | + | |
627 | 631 | | |
628 | 632 | | |
629 | 633 | | |
| |||
1263 | 1267 | | |
1264 | 1268 | | |
1265 | 1269 | | |
| 1270 | + | |
| 1271 | + | |
| 1272 | + | |
| 1273 | + | |
| 1274 | + | |
| 1275 | + | |
| 1276 | + | |
| 1277 | + | |
1266 | 1278 | | |
1267 | 1279 | | |
1268 | 1280 | | |
| |||
1995 | 2007 | | |
1996 | 2008 | | |
1997 | 2009 | | |
1998 | | - | |
| 2010 | + | |
1999 | 2011 | | |
2000 | 2012 | | |
2001 | 2013 | | |
2002 | 2014 | | |
2003 | | - | |
2004 | | - | |
| 2015 | + | |
| 2016 | + | |
| 2017 | + | |
2005 | 2018 | | |
2006 | 2019 | | |
2007 | 2020 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2379 | 2379 | | |
2380 | 2380 | | |
2381 | 2381 | | |
| 2382 | + | |
| 2383 | + | |
| 2384 | + | |
2382 | 2385 | | |
2383 | 2386 | | |
2384 | 2387 | | |
2385 | 2388 | | |
2386 | 2389 | | |
2387 | 2390 | | |
2388 | 2391 | | |
| 2392 | + | |
2389 | 2393 | | |
2390 | 2394 | | |
2391 | 2395 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
81 | 81 | | |
82 | 82 | | |
83 | 83 | | |
84 | | - | |
| 84 | + | |
85 | 85 | | |
86 | 86 | | |
87 | 87 | | |
| |||
1802 | 1802 | | |
1803 | 1803 | | |
1804 | 1804 | | |
1805 | | - | |
| 1805 | + | |
1806 | 1806 | | |
1807 | 1807 | | |
1808 | 1808 | | |
| |||
0 commit comments