Make sure mutability of dict arguments won't cause problems

One could argue that a pattern of "arg: dict | None = None" followed by "self.param = arg or dict()" is better, but that would make for an inconsistent pattern in cases where the default argument is not None.
This commit is contained in:
Grant Sanderson 2022-12-18 09:38:28 -08:00
parent 903e140719
commit c96cdf43a1
10 changed files with 26 additions and 19 deletions

View file

@ -35,13 +35,14 @@ class TransformMatchingParts(AnimationGroup):
target_mobject: Mobject, target_mobject: Mobject,
transform_mismatches: bool = False, transform_mismatches: bool = False,
fade_transform_mismatches: bool = False, fade_transform_mismatches: bool = False,
key_map: dict = dict(), key_map: dict | None = None,
**kwargs **kwargs
): ):
assert(isinstance(mobject, self.mobject_type)) assert(isinstance(mobject, self.mobject_type))
assert(isinstance(target_mobject, self.mobject_type)) assert(isinstance(target_mobject, self.mobject_type))
source_map = self.get_shape_map(mobject) source_map = self.get_shape_map(mobject)
target_map = self.get_shape_map(target_mobject) target_map = self.get_shape_map(target_mobject)
key_map = key_map or dict()
# Create two mobjects whose submobjects all match each other # Create two mobjects whose submobjects all match each other
# according to whatever keys are used for source_map and # according to whatever keys are used for source_map and

View file

@ -399,6 +399,7 @@ class CoordinateSystem(ABC):
class Axes(VGroup, CoordinateSystem): class Axes(VGroup, CoordinateSystem):
default_x_axis_config: dict = dict()
default_y_axis_config: dict = dict(line_to_number_direction=LEFT) default_y_axis_config: dict = dict(line_to_number_direction=LEFT)
def __init__( def __init__(
@ -418,7 +419,9 @@ class Axes(VGroup, CoordinateSystem):
self.x_axis = self.create_axis( self.x_axis = self.create_axis(
self.x_range, self.x_range,
axis_config=merge_dicts_recursively( axis_config=merge_dicts_recursively(
axis_config, x_axis_config self.default_x_axis_config,
axis_config,
x_axis_config
), ),
length=width, length=width,
) )
@ -503,7 +506,10 @@ class ThreeDAxes(Axes):
self.z_range = z_range self.z_range = z_range
self.z_axis = self.create_axis( self.z_axis = self.create_axis(
self.z_range, self.z_range,
axis_config=merge_dicts_recursively(kwargs.get("axes_config", {}), z_axis_config), axis_config=merge_dicts_recursively(
kwargs.get("axes_config", {}),
z_axis_config
),
length=depth, length=depth,
) )
self.z_axis.rotate(-PI / 2, UP, about_point=ORIGIN) self.z_axis.rotate(-PI / 2, UP, about_point=ORIGIN)
@ -584,8 +590,8 @@ class NumberPlane(Axes):
y_axis_config=y_axis_config, y_axis_config=y_axis_config,
**kwargs **kwargs
) )
self.background_line_style = background_line_style self.background_line_style = dict(background_line_style)
self.faded_line_style = faded_line_style self.faded_line_style = dict(faded_line_style)
self.faded_line_ratio = faded_line_ratio self.faded_line_ratio = faded_line_ratio
self.make_smooth_after_applying_functions = make_smooth_after_applying_functions self.make_smooth_after_applying_functions = make_smooth_after_applying_functions
self.init_background_lines() self.init_background_lines()

View file

@ -54,12 +54,12 @@ class NumberLine(Line):
self.tick_size = tick_size self.tick_size = tick_size
self.longer_tick_multiple = longer_tick_multiple self.longer_tick_multiple = longer_tick_multiple
self.tick_offset = tick_offset self.tick_offset = tick_offset
self.numbers_with_elongated_ticks = numbers_with_elongated_ticks self.numbers_with_elongated_ticks = list(numbers_with_elongated_ticks)
self.line_to_number_direction = line_to_number_direction self.line_to_number_direction = line_to_number_direction
self.line_to_number_buff = line_to_number_buff self.line_to_number_buff = line_to_number_buff
self.include_tip = include_tip self.include_tip = include_tip
self.tip_config = tip_config self.tip_config = dict(tip_config)
self.decimal_number_config = decimal_number_config self.decimal_number_config = dict(decimal_number_config)
self.numbers_to_exclude = numbers_to_exclude self.numbers_to_exclude = numbers_to_exclude
self.x_min, self.x_max = x_range[:2] self.x_min, self.x_max = x_range[:2]
@ -217,5 +217,5 @@ class UnitInterval(NumberLine):
x_range=x_range, x_range=x_range,
unit_size=unit_size, unit_size=unit_size,
numbers_with_elongated_ticks=numbers_with_elongated_ticks, numbers_with_elongated_ticks=numbers_with_elongated_ticks,
decimal_number_config =decimal_number_config, decimal_number_config=decimal_number_config,
) )

View file

@ -45,7 +45,7 @@ class DecimalNumber(VMobject):
self.include_background_rectangle = include_background_rectangle self.include_background_rectangle = include_background_rectangle
self.edge_to_fix = edge_to_fix self.edge_to_fix = edge_to_fix
self.font_size = font_size self.font_size = font_size
self.text_config = text_config self.text_config = dict(text_config)
super().__init__( super().__init__(
color=color, color=color,

View file

@ -51,7 +51,7 @@ class MTex(StringMobject):
self.alignment = alignment self.alignment = alignment
self.template = template self.template = template
self.additional_preamble = additional_preamble self.additional_preamble = additional_preamble
self.tex_to_color_map = tex_to_color_map self.tex_to_color_map = dict(tex_to_color_map)
super().__init__( super().__init__(
tex_string, tex_string,

View file

@ -68,7 +68,7 @@ class StringMobject(SVGMobject, ABC):
**kwargs **kwargs
): ):
self.string = string self.string = string
self.path_string_config = path_string_config self.path_string_config = dict(path_string_config)
self.base_color = base_color or WHITE self.base_color = base_color or WHITE
self.isolate = isolate self.isolate = isolate
self.protect = protect self.protect = protect

View file

@ -64,8 +64,8 @@ class SVGMobject(VMobject):
**kwargs **kwargs
): ):
self.file_name = file_name or self.file_name self.file_name = file_name or self.file_name
self.svg_default = svg_default self.svg_default = dict(svg_default)
self.path_string_config = path_string_config self.path_string_config = dict(path_string_config)
self.height = height self.height = height
super().__init__(**kwargs ) super().__init__(**kwargs )

View file

@ -7,7 +7,7 @@ import re
from manimlib.constants import BLACK, WHITE, GREY_C from manimlib.constants import BLACK, WHITE, GREY_C
from manimlib.constants import DOWN, LEFT, RIGHT, UP from manimlib.constants import DOWN, LEFT, RIGHT, UP
from manimlib.constants import FRAME_WIDTH from manimlib.constants import FRAME_WIDTH
from manimlib.constants import MED_LARGE_BUFF, MED_SMALL_BUFF, SMALL_BUFF from manimlib.constants import MED_LARGE_BUFF, SMALL_BUFF
from manimlib.mobject.geometry import Line from manimlib.mobject.geometry import Line
from manimlib.mobject.svg.svg_mobject import SVGMobject from manimlib.mobject.svg.svg_mobject import SVGMobject
from manimlib.mobject.types.vectorized_mobject import VGroup from manimlib.mobject.types.vectorized_mobject import VGroup
@ -46,8 +46,8 @@ class SingleStringTex(SVGMobject):
**kwargs **kwargs
): ):
self.tex_string = tex_string self.tex_string = tex_string
self.svg_default = svg_default self.svg_default = dict(svg_default)
self.path_string_config = path_string_config self.path_string_config = dict(path_string_config)
self.font_size = font_size self.font_size = font_size
self.alignment = alignment self.alignment = alignment
self.math_mode = math_mode self.math_mode = math_mode

View file

@ -140,7 +140,7 @@ class VectorField(VGroup):
self.color_map = color_map self.color_map = color_map
self.length_func = length_func self.length_func = length_func
self.opacity = opacity self.opacity = opacity
self.vector_config = vector_config self.vector_config = dict(vector_config)
self.value_to_rgb = get_rgb_gradient_function( self.value_to_rgb = get_rgb_gradient_function(
*self.magnitude_range, self.color_map, *self.magnitude_range, self.color_map,

View file

@ -82,7 +82,7 @@ class Scene(object):
if self.preview: if self.preview:
from manimlib.window import Window from manimlib.window import Window
self.window = Window(scene=self, **window_config) self.window = Window(scene=self, **window_config)
self.camera_config.update(camera_config) self.camera_config= {**self.camera_config, **camera_config}
self.camera_config["ctx"] = self.window.ctx self.camera_config["ctx"] = self.window.ctx
self.camera_config["fps"] = 30 # Where's that 30 from? self.camera_config["fps"] = 30 # Where's that 30 from?
self.undo_stack = [] self.undo_stack = []