99
1010import itertools
1111from collections .abc import Callable , Iterable , Sequence
12- from typing import Any , Final , TypeVar , cast
12+ from typing import Any , TypeVar , cast
1313
1414import mypy .subtypes
1515from mypy .checker_state import checker_state
7272 instance_cache ,
7373 remove_dups ,
7474)
75+ from mypy .typestate import BIND_SELF_CACHE_MAX_SIZE , type_state
7576from mypy .typetraverser import TypeTraverserVisitor
7677from mypy .typevars import fill_typevars
7778
78- BindSelfCacheKey = tuple [CallableType , Type , bool , bool ]
79- BindSelfCacheValue = tuple [CallableType , Sequence [TypeVarLikeType ]]
80-
81- BIND_SELF_CACHE : Final [dict [BindSelfCacheKey , BindSelfCacheValue ]] = {}
82- BIND_SELF_CACHE_MAX_SIZE : Final = 10000
83-
8479
8580def is_recursive_pair (s : Type , t : Type ) -> bool :
8681 """Is this a pair of recursive types?
@@ -562,8 +557,8 @@ def bind_self_inner(
562557 # Only use cache for something that in not freshened (e.g. protocol access),
563558 # otherwise it would be pointless as each fresh meta variable is unique.
564559 cache_key = (func , original_type , is_classmethod , ignore_instances )
565- if cache_key is not None and cache_key in BIND_SELF_CACHE :
566- return BIND_SELF_CACHE [cache_key ]
560+ if cache_key is not None and cache_key in type_state . bind_self_cache :
561+ return type_state . bind_self_cache [cache_key ]
567562
568563 # Solve for these type arguments using the actual class or instance type.
569564 typeargs = infer_type_arguments (
@@ -589,9 +584,9 @@ def bind_self_inner(
589584 func = expand_type (func , {tv .id : arg for tv , arg in zip (self_vars , to_apply )})
590585 variables = [v for v in func .variables if v not in self_vars ]
591586 if cache_key is not None :
592- if len (BIND_SELF_CACHE ) > BIND_SELF_CACHE_MAX_SIZE :
593- BIND_SELF_CACHE .clear ()
594- BIND_SELF_CACHE [cache_key ] = func , variables
587+ if len (type_state . bind_self_cache ) > BIND_SELF_CACHE_MAX_SIZE :
588+ type_state . bind_self_cache .clear ()
589+ type_state . bind_self_cache [cache_key ] = func , variables
595590 else :
596591 variables = func .variables
597592 return func , variables
0 commit comments