Skip to content
This repository has been archived by the owner on May 31, 2020. It is now read-only.

Fix error messages for calling __setitem__ on an immutable type #884

Merged
merged 1 commit into from Aug 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions python/common/org/python/types/Bool.java
Expand Up @@ -94,6 +94,16 @@ public org.python.types.Str __getitem__(org.python.Object format_string) {
throw new org.python.exceptions.TypeError("'bool' object is not subscriptable");
}

@org.python.Method(
__doc__ = "",
args = {"index", "value"}
)
public void __setitem__(org.python.Object index, org.python.Object value) {
throw new org.python.exceptions.TypeError(
"'bool' object does not support item assignment"
);
}

@org.python.Method(
__doc__ = "Return self<value.",
args = {"other"}
Expand Down
10 changes: 10 additions & 0 deletions python/common/org/python/types/FrozenSet.java
Expand Up @@ -165,6 +165,16 @@ public org.python.Object copy() {
return this;
}

@org.python.Method(
__doc__ = "",
args = {"index", "value"}
)
public void __setitem__(org.python.Object index, org.python.Object value) {
throw new org.python.exceptions.TypeError(
"'frozenset' object does not support item assignment"
);
}

@org.python.Method(
__doc__ = "Return self<value.",
args = {"other"}
Expand Down
10 changes: 10 additions & 0 deletions python/common/org/python/types/Int.java
Expand Up @@ -155,6 +155,16 @@ public org.python.types.Str __getitem__(org.python.Object format_str) {
throw new org.python.exceptions.TypeError("'int' object is not subscriptable");
}

@org.python.Method(
__doc__ = "",
args = {"index", "value"}
)
public void __setitem__(org.python.Object index, org.python.Object value) {
throw new org.python.exceptions.TypeError(
"'int' object does not support item assignment"
);
}

@org.python.Method(
__doc__ = "Return self<value.",
args = {"other"}
Expand Down
10 changes: 10 additions & 0 deletions python/common/org/python/types/Range.java
Expand Up @@ -122,6 +122,16 @@ public org.python.Object __getitem__(org.python.Object index) {
}
}

@org.python.Method(
__doc__ = "",
args = {"index", "value"}
)
public void __setitem__(org.python.Object index, org.python.Object value) {
throw new org.python.exceptions.TypeError(
"'range' object does not support item assignment"
);
}

@org.python.Method(
__doc__ = "Implement __len__(self)."
)
Expand Down
10 changes: 10 additions & 0 deletions python/common/org/python/types/Str.java
Expand Up @@ -376,6 +376,16 @@ public org.python.Object __getitem__(org.python.Object index) {
}
}

@org.python.Method(
__doc__ = "",
args = {"index", "value"}
)
public void __setitem__(org.python.Object index, org.python.Object value) {
throw new org.python.exceptions.TypeError(
"'str' object does not support item assignment"
);
}

@org.python.Method(
__doc__ = "Implement iter(self)."
)
Expand Down
9 changes: 9 additions & 0 deletions tests/datatypes/test_bool.py
Expand Up @@ -21,6 +21,15 @@ def test_getattr(self):
print(err)
""")

def test_setitem(self):
self.assertCodeExecution("""
x = True
try:
x[0] = 1
except TypeError as err:
print(err)
""")


class UnaryBoolOperationTests(UnaryOperationTestCase, TranspileTestCase):
data_type = 'bool'
Expand Down
9 changes: 9 additions & 0 deletions tests/datatypes/test_float.py
Expand Up @@ -26,6 +26,15 @@ def test_getattr(self):
print(err)
""")

def test_setitem(self):
self.assertCodeExecution("""
x = 3.14159
try:
x[0] = 2
except TypeError as err:
print(err)
""")

def test_repr(self):
self.assertCodeExecution("""
x = 350000000000000000.0
Expand Down
9 changes: 9 additions & 0 deletions tests/datatypes/test_frozenset.py
Expand Up @@ -145,6 +145,15 @@ def test_copy(self):

""")

def test_setitem(self):
self.assertCodeExecution("""
x = frozenset("hello world")
try:
x[0] = "goodbye"
except TypeError as err:
print(err)
""")

def test_isdisjoint(self):
self.assertCodeExecution("""
x = frozenset("hello world")
Expand Down
9 changes: 9 additions & 0 deletions tests/datatypes/test_int.py
Expand Up @@ -28,6 +28,15 @@ def test_getattr(self):
print(err)
""")

def test_setitem(self):
self.assertCodeExecution("""
x = 37
try:
x[0] = 1
except TypeError as err:
print(err)
""")

def test_invalid_literal(self):
self.assertCodeExecution("""
try:
Expand Down
9 changes: 9 additions & 0 deletions tests/datatypes/test_range.py
Expand Up @@ -80,6 +80,15 @@ def test_iterator_iterator(self):
print(r)
""")

def test_setitem(self):
self.assertCodeExecution("""
r = range(10)
try:
r[0] = "abc"
except TypeError as e:
print(e)
""")


class UnaryRangeOperationTests(UnaryOperationTestCase, TranspileTestCase):
data_type = 'range'
Expand Down
10 changes: 10 additions & 0 deletions tests/datatypes/test_str.py
Expand Up @@ -238,6 +238,16 @@ def test_getitem(self):
print(err)
""")

def test_setitem(self):
# Strings are immutable and do not allow item assignment
self.assertCodeExecution("""
x = "BeeWare"
try:
x[0] = "A"
except TypeError as err:
print(err)
""")

def test_slice(self):
# Full slice
self.assertCodeExecution("""
Expand Down
9 changes: 9 additions & 0 deletions tests/datatypes/test_tuple.py
Expand Up @@ -43,6 +43,15 @@ def test_const_creation_multitype(self):
print(x)
""")

def test_setitem(self):
self.assertCodeExecution("""
x = (1, 2, 3, 4, 5)
try:
x[0] = "a"
except TypeError as err:
print(err)
""")

def test_getitem(self):
# Simple positive index
self.assertCodeExecution("""
Expand Down