@@ -600,12 +600,12 @@ def gen_candidates():
600
600
_deprecated_remain_as_none = {}
601
601
602
602
603
- @ _docstring .Substitution (
603
+ _docstring .Substitution (
604
604
"\n " .join (map ("- {}" .format , sorted (rcsetup ._validators , key = str .lower )))
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,38 @@ 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
+ Direct 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
+ dict .__setitem__ (self , key , val )
643
+
644
+ def _get (self , key ):
645
+ """
646
+ Direct read data bypassing deprecation, backend and validation logic.
647
+
648
+ As end user or downstream library you almost always should use
649
+ ``val = rcParams[key]`` and not ``_get()``.
650
+
651
+ There are only very few special cases that need direct data access.
652
+ These cases previously used ``dict.__getitem__(rcParams, key, val)``,
653
+ which is now deprecated and replaced by ``rcParams._get(key)``.
654
+
655
+ Even though private, we guarantee API stability for ``rcParams._get``,
656
+ i.e. it is subject to Matplotlib's API and deprecation policy.
657
+ """
658
+ return dict .__getitem__ (self , key )
659
+
628
660
def __setitem__ (self , key , val ):
629
661
try :
630
662
if key in _deprecated_map :
@@ -649,7 +681,7 @@ def __setitem__(self, key, val):
649
681
cval = self .validate [key ](val )
650
682
except ValueError as ve :
651
683
raise ValueError (f"Key { key } : { ve } " ) from None
652
- dict . __setitem__ ( self , key , cval )
684
+ self . _set ( key , cval )
653
685
except KeyError as err :
654
686
raise KeyError (
655
687
f"{ key } is not a valid rc parameter (see rcParams.keys() for "
@@ -660,27 +692,27 @@ def __getitem__(self, key):
660
692
version , alt_key , alt_val , inverse_alt = _deprecated_map [key ]
661
693
_api .warn_deprecated (
662
694
version , name = key , obj_type = "rcparam" , alternative = alt_key )
663
- return inverse_alt (dict . __getitem__ ( self , alt_key ))
695
+ return inverse_alt (self . _get ( alt_key ))
664
696
665
697
elif key in _deprecated_ignore_map :
666
698
version , alt_key = _deprecated_ignore_map [key ]
667
699
_api .warn_deprecated (
668
700
version , name = key , obj_type = "rcparam" , alternative = alt_key )
669
- return dict . __getitem__ ( self , alt_key ) if alt_key else None
701
+ return self . _get ( alt_key ) if alt_key else None
670
702
671
703
# In theory, this should only ever be used after the global rcParams
672
704
# has been set up, but better be safe e.g. in presence of breakpoints.
673
705
elif key == "backend" and self is globals ().get ("rcParams" ):
674
- val = dict . __getitem__ ( self , key )
706
+ val = self . _get ( key )
675
707
if val is rcsetup ._auto_backend_sentinel :
676
708
from matplotlib import pyplot as plt
677
709
plt .switch_backend (rcsetup ._auto_backend_sentinel )
678
710
679
- return dict . __getitem__ ( self , key )
711
+ return self . _get ( key )
680
712
681
713
def _get_backend_or_none (self ):
682
714
"""Get the requested backend, if any, without triggering resolution."""
683
- backend = dict . __getitem__ ( self , "backend" )
715
+ backend = self . _get ( "backend" )
684
716
return None if backend is rcsetup ._auto_backend_sentinel else backend
685
717
686
718
def __repr__ (self ):
@@ -722,7 +754,7 @@ def find_all(self, pattern):
722
754
def copy (self ):
723
755
rccopy = RcParams ()
724
756
for k in self : # Skip deprecations and revalidation.
725
- dict . __setitem__ ( rccopy , k , dict . __getitem__ ( self , k ))
757
+ rccopy . _set ( k , self . _get ( k ))
726
758
return rccopy
727
759
728
760
0 commit comments