Skip to content

Commit

Permalink
Adding test for default value immutability
Browse files Browse the repository at this point in the history
  • Loading branch information
swansonk14 committed Jun 28, 2023
1 parent c48ef74 commit 7f12646
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tap/tap.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ def parse_args(self: TapType,
elif var_type in (Tuple, tuple):
value = tuple(value)

# Set variable in self (and deepcopy)
# Set variable in self (and deepcopy to prevent mutation of default values)
setattr(self, variable, deepcopy(value))

# Process args
Expand Down
16 changes: 16 additions & 0 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -1601,5 +1601,21 @@ class Desc(Tap):
self.assertNotIn("I do not exist", help_desc)


class TestDefaultImmutability(TestCase):
def test_default_immutability(self):
holy_hand_grenada = [1, 2, 5] # no, 3 sir!

class DefaultImmutabilityTap(Tap):
array: list[int] = holy_hand_grenada

args = DefaultImmutabilityTap().parse_args([])

self.assertEqual(args.array, holy_hand_grenada)

holy_hand_grenada[2] = 3

self.assertEqual(args.array, [1, 2, 5])


if __name__ == '__main__':
unittest.main()

0 comments on commit 7f12646

Please sign in to comment.