-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit a2ffde9
Generalize subtype caches (#5474)
* Generalize subtype caches
Currently, the typestate module keeps track of two separate subtype
caches: one for regular subtypes, and another for proper subtypes.
This pattern unfortunately does not generalize well: for example, in the
upcoming overloads PR, it ends up being useful to perform subtype checks
*with promotions disabled*.
The problem is that while performing a subtype check with promotions
disabled, we cannot use or store information to the existing is_subtype
without introducing subtle bugs and inconsistencies: if is_subtype
previously decided that int is a subtype of float due to promotions,
it wouldn't make sense to return that cached result with promotions
disabled.
One potential solution is to just add two more caches to TypeState.
However, this solution does not generalize well, especially if we
were to add additional variations of subtype checks in the future.
Instead, this pull request modifies typestate so it can keep track
of an arbitrary number of subtype caches, each of which is characterized
by a 'SubtypeKind'.
The 'SubtypeKind' is an int, which we obtain by taking the hash of a
tuple containing a unique string per each subtype method, along with
whatever additional parameters are active during that subtype check.
So now, instead of hard-coding calls to 'record_subtype_cache_entry' or
'record_proper_subtype_cache_entry' and friends, we can just call
'record_subtype_cache_entry' and pass in the appropriate SubtypeKind
to store the type info in the correct cache.
Since manually juggling parameters and SubtypeKinds is a bit of a pain,
this pull request also refactors the two subtype visitors so they call
a private helper method that takes care of propagating the parameters
instead of directly calling the `is_subtype` or `is_proper_subtype`
top-level methods.
NOTE: Currently, the mypy codebase does not attempt to perform subtype
checks with promotions disabled. That means that this PR should not
change the actual behavior of mypy, which is why there are no new tests.
(My subsequent PRs will perform subtype checks w/ promotions disabled.)
* Respond to code review1 parent 7416549 commit a2ffde9Copy full SHA for a2ffde9
2 files changed
+152
-98
lines changed
0 commit comments