@@ -620,7 +620,7 @@ def gen_candidates():
620620)
621621class RcParams (MutableMapping , dict ):
622622 """
623- A dictionary object including validation.
623+ A dict-like key-value store for config parameters, including validation.
624624
625625 Validating functions are defined and associated with rc parameters in
626626 :mod:`matplotlib.rcsetup`.
@@ -640,6 +640,47 @@ class RcParams(MutableMapping, dict):
640640 def __init__ (self , * args , ** kwargs ):
641641 self .update (* args , ** kwargs )
642642
643+ def _set (self , key , val ):
644+ """
645+ Directly write data bypassing deprecation and validation logic.
646+
647+ Notes
648+ -----
649+ As end user or downstream library you almost always should use
650+ ``rcParams[key] = val`` and not ``_set()``.
651+
652+ There are only very few special cases that need direct data access.
653+ These cases previously used ``dict.__setitem__(rcParams, key, val)``,
654+ which is now deprecated and replaced by ``rcParams._set(key, val)``.
655+
656+ Even though private, we guarantee API stability for ``rcParams._set``,
657+ i.e. it is subject to Matplotlib's API and deprecation policy.
658+
659+ :meta public:
660+ """
661+ dict .__setitem__ (self , key , val )
662+
663+ def _get (self , key ):
664+ """
665+ Directly read data bypassing deprecation, backend and validation
666+ logic.
667+
668+ Notes
669+ -----
670+ As end user or downstream library you almost always should use
671+ ``val = rcParams[key]`` and not ``_get()``.
672+
673+ There are only very few special cases that need direct data access.
674+ These cases previously used ``dict.__getitem__(rcParams, key, val)``,
675+ which is now deprecated and replaced by ``rcParams._get(key)``.
676+
677+ Even though private, we guarantee API stability for ``rcParams._get``,
678+ i.e. it is subject to Matplotlib's API and deprecation policy.
679+
680+ :meta public:
681+ """
682+ return dict .__getitem__ (self , key )
683+
643684 def __setitem__ (self , key , val ):
644685 try :
645686 if key in _deprecated_map :
@@ -664,7 +705,7 @@ def __setitem__(self, key, val):
664705 cval = self .validate [key ](val )
665706 except ValueError as ve :
666707 raise ValueError (f"Key { key } : { ve } " ) from None
667- dict . __setitem__ ( self , key , cval )
708+ self . _set ( key , cval )
668709 except KeyError as err :
669710 raise KeyError (
670711 f"{ key } is not a valid rc parameter (see rcParams.keys() for "
@@ -675,27 +716,27 @@ def __getitem__(self, key):
675716 version , alt_key , alt_val , inverse_alt = _deprecated_map [key ]
676717 _api .warn_deprecated (
677718 version , name = key , obj_type = "rcparam" , alternative = alt_key )
678- return inverse_alt (dict . __getitem__ ( self , alt_key ))
719+ return inverse_alt (self . _get ( alt_key ))
679720
680721 elif key in _deprecated_ignore_map :
681722 version , alt_key = _deprecated_ignore_map [key ]
682723 _api .warn_deprecated (
683724 version , name = key , obj_type = "rcparam" , alternative = alt_key )
684- return dict . __getitem__ ( self , alt_key ) if alt_key else None
725+ return self . _get ( alt_key ) if alt_key else None
685726
686727 # In theory, this should only ever be used after the global rcParams
687728 # has been set up, but better be safe e.g. in presence of breakpoints.
688729 elif key == "backend" and self is globals ().get ("rcParams" ):
689- val = dict . __getitem__ ( self , key )
730+ val = self . _get ( key )
690731 if val is rcsetup ._auto_backend_sentinel :
691732 from matplotlib import pyplot as plt
692733 plt .switch_backend (rcsetup ._auto_backend_sentinel )
693734
694- return dict . __getitem__ ( self , key )
735+ return self . _get ( key )
695736
696737 def _get_backend_or_none (self ):
697738 """Get the requested backend, if any, without triggering resolution."""
698- backend = dict . __getitem__ ( self , "backend" )
739+ backend = self . _get ( "backend" )
699740 return None if backend is rcsetup ._auto_backend_sentinel else backend
700741
701742 def __repr__ (self ):
@@ -738,7 +779,7 @@ def copy(self):
738779 """Copy this RcParams instance."""
739780 rccopy = RcParams ()
740781 for k in self : # Skip deprecations and revalidation.
741- dict . __setitem__ ( rccopy , k , dict . __getitem__ ( self , k ))
782+ rccopy . _set ( k , self . _get ( k ))
742783 return rccopy
743784
744785
0 commit comments