@@ -310,23 +310,25 @@ def _no_new_attributes(self, this_class: type) -> None:
310
310
311
311
def __setattr__ (self , key : str , value : Any ) -> None :
312
312
"""Prevent adding new attributes to classes using "normal" methods."""
313
- # Check if this class froze itself. Any frozen state already set by parent classes, e.g.
314
- # by calling super().__init__(), will be ignored. This allows subclasses to set attributes
315
- # during init without being affect by a parent class init.
316
- frozen = self .__dict__ .get ('__frozen' , False )
317
- frozen_by = self .__dict__ .get ('__frozen_by_class' , None )
318
- if (
319
- frozen
320
- and frozen_by is type (self )
321
- and not (key in type (self ).__dict__ or hasattr (self , key ))
322
- ):
323
- from pyvista import PyVistaAttributeError # noqa: PLC0415
324
-
325
- msg = (
326
- f'Attribute { key !r} does not exist and cannot be added to class '
327
- f'{ self .__class__ .__name__ !r} \n Use `pv.set_new_attribute` to set new attributes.'
328
- )
329
- raise PyVistaAttributeError (msg )
313
+ if not key .startswith ('_' ):
314
+ # Check if this class froze itself. Any frozen state already set by parent classes,
315
+ # e.g. by calling super().__init__(), will be ignored. This allows subclasses to set
316
+ # attributes during init without being affect by a parent class init.
317
+ frozen = self .__dict__ .get ('__frozen' , False )
318
+ frozen_by = self .__dict__ .get ('__frozen_by_class' , None )
319
+ if (
320
+ frozen
321
+ and frozen_by is type (self )
322
+ and not (key in type (self ).__dict__ or hasattr (self , key ))
323
+ ):
324
+ from pyvista import PyVistaAttributeError # noqa: PLC0415
325
+
326
+ msg = (
327
+ f'Attribute { key !r} does not exist and cannot be added to class '
328
+ f'{ self .__class__ .__name__ !r} \n Use `pv.set_new_attribute` to set new '
329
+ f'attributes or consider setting a private variable (with `_` prefix) instead.'
330
+ )
331
+ raise PyVistaAttributeError (msg )
330
332
object .__setattr__ (self , key , value )
331
333
332
334
0 commit comments