Skip to content

πŸŽ› πŸ”Š A Python library for audio.

Throw an error message if plugin.parameters is assigned to.

Peter Sobot92b6d1ddb842
diff --git a/pedalboard/pedalboard.py b/pedalboard/pedalboard.py
index 786037b..9ce40d6 100644
--- a/pedalboard/pedalboard.py
+++ b/pedalboard/pedalboard.py
@@ -142,6 +142,16 @@ def looks_like_float(s: str) -> bool:
return False
+class ReadOnlyDictWrapper(dict):
+ def __setitem__(self, name, value):
+ raise TypeError(
+ "The .parameters dictionary on a Plugin instance returns "
+ "a read-only dictionary of its parameters. To change a "
+ "parameter, set the parameter on the plugin directly as "
+ f"an attribute. (`my_plugin.{name} = {value}`)"
+ )
+
+
def wrap_type(base_type):
class WeakTypeWrapper(base_type):
"""
@@ -490,7 +500,9 @@ class ExternalPlugin(object):
@property
def parameters(self) -> Dict[str, AudioProcessorParameter]:
- return self._get_parameters()
+ # Return a read-only version of this dictionary,
+ # to prevent people from trying to assign to it.
+ return ReadOnlyDictWrapper(self._get_parameters())
def _get_parameters(self):
if not hasattr(self, "__python_parameter_cache__"):
diff --git a/tests/test_external_plugins.py b/tests/test_external_plugins.py
index d308b38..ebda929 100644
--- a/tests/test_external_plugins.py
+++ b/tests/test_external_plugins.py
@@ -146,6 +146,16 @@ def test_all_parameters_are_accessible_as_properties(plugin_filename: str):
assert hasattr(plugin, parameter_name)
[email protected]("plugin_filename", AVAILABLE_PLUGINS_IN_TEST_ENVIRONMENT)
+def test_parameters_cant_be_assigned_to_directly(plugin_filename: str):
+ plugin = load_test_plugin(plugin_filename)
+ assert plugin.parameters
+ for parameter_name in plugin.parameters.keys():
+ current_value = getattr(plugin, parameter_name)
+ with pytest.raises(TypeError):
+ plugin.parameters[parameter_name] = current_value
+
+
@pytest.mark.parametrize("plugin_filename", AVAILABLE_PLUGINS_IN_TEST_ENVIRONMENT)
def test_all_parameters_have_accessors(plugin_filename: str):
plugin = load_test_plugin(plugin_filename)