@@ -620,7 +620,7 @@ def gen_candidates():
620
620
)
621
621
class RcParams (MutableMapping , dict ):
622
622
"""
623
- A dictionary object including validation.
623
+ A dict-like key-value store for config parameters, including validation.
624
624
625
625
Validating functions are defined and associated with rc parameters in
626
626
:mod:`matplotlib.rcsetup`.
@@ -640,6 +640,47 @@ class RcParams(MutableMapping, dict):
640
640
def __init__ (self , * args , ** kwargs ):
641
641
self .update (* args , ** kwargs )
642
642
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
+
643
684
def __setitem__ (self , key , val ):
644
685
try :
645
686
if key in _deprecated_map :
@@ -664,7 +705,7 @@ def __setitem__(self, key, val):
664
705
cval = self .validate [key ](val )
665
706
except ValueError as ve :
666
707
raise ValueError (f"Key { key } : { ve } " ) from None
667
- dict . __setitem__ ( self , key , cval )
708
+ self . _set ( key , cval )
668
709
except KeyError as err :
669
710
raise KeyError (
670
711
f"{ key } is not a valid rc parameter (see rcParams.keys() for "
@@ -675,27 +716,27 @@ def __getitem__(self, key):
675
716
version , alt_key , alt_val , inverse_alt = _deprecated_map [key ]
676
717
_api .warn_deprecated (
677
718
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 ))
679
720
680
721
elif key in _deprecated_ignore_map :
681
722
version , alt_key = _deprecated_ignore_map [key ]
682
723
_api .warn_deprecated (
683
724
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
685
726
686
727
# In theory, this should only ever be used after the global rcParams
687
728
# has been set up, but better be safe e.g. in presence of breakpoints.
688
729
elif key == "backend" and self is globals ().get ("rcParams" ):
689
- val = dict . __getitem__ ( self , key )
730
+ val = self . _get ( key )
690
731
if val is rcsetup ._auto_backend_sentinel :
691
732
from matplotlib import pyplot as plt
692
733
plt .switch_backend (rcsetup ._auto_backend_sentinel )
693
734
694
- return dict . __getitem__ ( self , key )
735
+ return self . _get ( key )
695
736
696
737
def _get_backend_or_none (self ):
697
738
"""Get the requested backend, if any, without triggering resolution."""
698
- backend = dict . __getitem__ ( self , "backend" )
739
+ backend = self . _get ( "backend" )
699
740
return None if backend is rcsetup ._auto_backend_sentinel else backend
700
741
701
742
def __repr__ (self ):
@@ -738,7 +779,7 @@ def copy(self):
738
779
"""Copy this RcParams instance."""
739
780
rccopy = RcParams ()
740
781
for k in self : # Skip deprecations and revalidation.
741
- dict . __setitem__ ( rccopy , k , dict . __getitem__ ( self , k ))
782
+ rccopy . _set ( k , self . _get ( k ))
742
783
return rccopy
743
784
744
785
0 commit comments