Skip to content

Commit 520fdb8

Browse files
Fix dangerous default argument
1 parent 1bfc966 commit 520fdb8

4 files changed

Lines changed: 12 additions & 4 deletions

File tree

examples/components/pdf_component.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ def data(self):
4040
"""Return a 1D data trace."""
4141
return [self.noisy_pdf(x) for x in self.x_range]
4242

43-
def average_data(self, n: int = 10, optlist: List[int] = [1, 2, 3]):
43+
def average_data(self, n: int = 10, optlist: List[int] = None):
4444
"""Average n-sets of data. Emulates a measurement that may take a while."""
45+
if optlist is None:
46+
optlist = [1, 2, 3]
4547
summed_data = self.data
4648

4749
for i in range(n):

labthings/server/labthing.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@ def __init__(
3838
prefix: str = "",
3939
title: str = "",
4040
description: str = "",
41-
types: list = [],
41+
types: list = None,
4242
version: str = "0.0.0",
4343
format_flask_exceptions: bool = True,
4444
):
45+
if types is None:
46+
types = []
4547
self.app = app # Becomes a Flask app
4648
self.sockets = None # Becomes a Socket(app) websocket handler
4749

labthings/server/quick.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def create_app(
1111
prefix: str = "",
1212
title: str = "",
1313
description: str = "",
14-
types: list = [],
14+
types: list = None,
1515
version: str = "0.0.0",
1616
handle_errors: bool = True,
1717
handle_cors: bool = True,
@@ -34,6 +34,8 @@ def create_app(
3434
Returns:
3535
(Flask app object, LabThings object)
3636
"""
37+
if types is None:
38+
types = []
3739
# Handle arguments
3840
if flask_kwargs is None:
3941
flask_kwargs = {}

tests/test_server_types.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,10 @@ def test_function_signature_to_schema():
8686
from marshmallow import Schema
8787

8888
def test_func(
89-
positional: int, n: int = 10, optlist: List[int] = [1, 2, 3], untyped="untyped"
89+
positional: int, n: int = 10, optlist: List[int] = None, untyped="untyped"
9090
):
91+
if optlist is None:
92+
optlist = [1, 2, 3]
9193
pass
9294

9395
gen_schema_dict = types.function_signature_to_schema(test_func)

0 commit comments

Comments
 (0)