Skip to content

Commit 74d80c1

Browse files
authored
Merge pull request #580 from robthew/immutability-issue
Added documentation about immutability issues
2 parents c416190 + da19639 commit 74d80c1

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

docs/guide/creating.ipynb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,42 @@
11131113
"\n",
11141114
"print(ip4_valid_refs.serialize(pretty=True))"
11151115
]
1116+
},
1117+
{
1118+
"cell_type": "markdown",
1119+
"metadata": {},
1120+
"source": [
1121+
"## Immutability Issues\n",
1122+
"\n",
1123+
"It's been shown above that changing an object's properties will trigger an ImmutableError, but because List and Dictionary properties retain their built-in content-modification functions, it is possible to modify some of an object's data. ImmutableError's are triggered when the object ID of a field is changed. Strings are inherently immutable, so any attempt to modify their content results in a new object being created - with a new ID - triggering the ImmutableError. But using the content-modification functions of Lists and Dictionaries does not change the underlying object ID, and therefore, doesn't trigger the error.\n",
1124+
"\n",
1125+
"This does violate the design principle of STIX 2 being immutable by default, but it is something to be aware off, if only to avoid doing it by mistake. It also allows the possibility of complex objects being built up by appending to fields, rather than creating objects all in one action.\n",
1126+
"\n",
1127+
"Here an Identity object is created. Fields like 'name' and 'identity_class' are immutable, but the 'roles' data can be modified using the List operations 'clear' and 'append'."
1128+
]
1129+
},
1130+
{
1131+
"cell_type": "code",
1132+
"execution_count": null,
1133+
"metadata": {},
1134+
"outputs": [],
1135+
"source": [
1136+
"from stix2 import Identity\n",
1137+
"\n",
1138+
"identity1 = Identity(\n",
1139+
" x_custom=\"IRF system\",\n",
1140+
" name=\"IRF\",\n",
1141+
" description=\"Incident Reporting Form (IRF) system\",\n",
1142+
" identity_class=\"system\",\n",
1143+
" roles=[\"director\"],\n",
1144+
" sectors=[\"commercial\", \"defense\"],\n",
1145+
")\n",
1146+
"\n",
1147+
"identity1.roles.clear()\n",
1148+
"identity1.roles.append(\"cleared-dod-contractor\")\n",
1149+
"\n",
1150+
"print(identity1.serialize(pretty=True))"
1151+
]
11161152
}
11171153
],
11181154
"metadata": {

docs/overview.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@ To accomplish these goals, and to incorporate lessons learned while developing
2020
``python-stix`` (for STIX 1.x), several decisions influenced the design of the
2121
``stix2`` library:
2222

23-
1. All data structures are immutable by default. In contrast to python-stix,
23+
1. All data structures are meant to be immutable by default. In contrast to python-stix,
2424
where users would create an object and then assign attributes to it, in
25-
``stix2`` all properties must be provided when creating the object.
25+
``stix2`` all properties should be provided when creating the object. Changing a property
26+
after creation will trigger an Immutability error, but some data structures like
27+
Lists and Dictionaries retain functions that allow for content modification without
28+
triggering an Immutability error. Data can be added to or removed from these structures
29+
after creation, which does allow for some flexibility in building an object.
2630
2. Where necessary, library objects should act like ``dict``'s. When treated as
2731
a ``str``, the JSON representation of the object should be used.
2832
3. Core Python data types (including numeric types, ``datetime``) should be used

0 commit comments

Comments
 (0)