@@ -605,7 +605,7 @@ def gen_candidates():
605
605
)
606
606
class RcParams (MutableMapping , dict ):
607
607
"""
608
- A dictionary object including validation.
608
+ A dict-like configuration parameter container including validation.
609
609
610
610
Validating functions are defined and associated with rc parameters in
611
611
:mod:`matplotlib.rcsetup`.
@@ -625,6 +625,43 @@ class RcParams(MutableMapping, dict):
625
625
def __init__ (self , * args , ** kwargs ):
626
626
self .update (* args , ** kwargs )
627
627
628
+ def _set (self , key , val ):
629
+ """
630
+ Directly write data bypassing deprecation and validation logic.
631
+
632
+ As end user or downstream library you almost always should use
633
+ ``rcParams[key] = val`` and not ``_set()``.
634
+
635
+ There are only very few special cases that need direct data access.
636
+ These cases previously used ``dict.__setitem__(rcParams, key, val)``,
637
+ which is now deprecated and replaced by ``rcParams._set(key, val)``.
638
+
639
+ Even though private, we guarantee API stability for ``rcParams._set``,
640
+ i.e. it is subject to Matplotlib's API and deprecation policy.
641
+
642
+ :meta public:
643
+ """
644
+ dict .__setitem__ (self , key , val )
645
+
646
+ def _get (self , key ):
647
+ """
648
+ Directly read data bypassing deprecation, backend and validation
649
+ logic.
650
+
651
+ As end user or downstream library you almost always should use
652
+ ``val = rcParams[key]`` and not ``_get()``.
653
+
654
+ There are only very few special cases that need direct data access.
655
+ These cases previously used ``dict.__getitem__(rcParams, key, val)``,
656
+ which is now deprecated and replaced by ``rcParams._get(key)``.
657
+
658
+ Even though private, we guarantee API stability for ``rcParams._get``,
659
+ i.e. it is subject to Matplotlib's API and deprecation policy.
660
+
661
+ :meta public:
662
+ """
663
+ return dict .__getitem__ (self , key )
664
+
628
665
def __setitem__ (self , key , val ):
629
666
try :
630
667
if key in _deprecated_map :
@@ -649,7 +686,7 @@ def __setitem__(self, key, val):
649
686
cval = self .validate [key ](val )
650
687
except ValueError as ve :
651
688
raise ValueError (f"Key { key } : { ve } " ) from None
652
- dict . __setitem__ ( self , key , cval )
689
+ self . _set ( key , cval )
653
690
except KeyError as err :
654
691
raise KeyError (
655
692
f"{ key } is not a valid rc parameter (see rcParams.keys() for "
@@ -660,27 +697,27 @@ def __getitem__(self, key):
660
697
version , alt_key , alt_val , inverse_alt = _deprecated_map [key ]
661
698
_api .warn_deprecated (
662
699
version , name = key , obj_type = "rcparam" , alternative = alt_key )
663
- return inverse_alt (dict . __getitem__ ( self , alt_key ))
700
+ return inverse_alt (self . _get ( alt_key ))
664
701
665
702
elif key in _deprecated_ignore_map :
666
703
version , alt_key = _deprecated_ignore_map [key ]
667
704
_api .warn_deprecated (
668
705
version , name = key , obj_type = "rcparam" , alternative = alt_key )
669
- return dict . __getitem__ ( self , alt_key ) if alt_key else None
706
+ return self . _get ( alt_key ) if alt_key else None
670
707
671
708
# In theory, this should only ever be used after the global rcParams
672
709
# has been set up, but better be safe e.g. in presence of breakpoints.
673
710
elif key == "backend" and self is globals ().get ("rcParams" ):
674
- val = dict . __getitem__ ( self , key )
711
+ val = self . _get ( key )
675
712
if val is rcsetup ._auto_backend_sentinel :
676
713
from matplotlib import pyplot as plt
677
714
plt .switch_backend (rcsetup ._auto_backend_sentinel )
678
715
679
- return dict . __getitem__ ( self , key )
716
+ return self . _get ( key )
680
717
681
718
def _get_backend_or_none (self ):
682
719
"""Get the requested backend, if any, without triggering resolution."""
683
- backend = dict . __getitem__ ( self , "backend" )
720
+ backend = self . _get ( "backend" )
684
721
return None if backend is rcsetup ._auto_backend_sentinel else backend
685
722
686
723
def __repr__ (self ):
@@ -722,7 +759,7 @@ def find_all(self, pattern):
722
759
def copy (self ):
723
760
rccopy = RcParams ()
724
761
for k in self : # Skip deprecations and revalidation.
725
- dict . __setitem__ ( rccopy , k , dict . __getitem__ ( self , k ))
762
+ rccopy . _set ( k , self . _get ( k ))
726
763
return rccopy
727
764
728
765
0 commit comments