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

Commit

Permalink
Merge pull request #884 from patiences/add-setitem
Browse files Browse the repository at this point in the history
Fix error messages for calling __setitem__ on an immutable type
  • Loading branch information
freakboy3742 authored Aug 2, 2018
2 parents f1243c6 + 1dbc585 commit d26e35d
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 0 deletions.
10 changes: 10 additions & 0 deletions python/common/org/python/types/Bool.java
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit d26e35d

Please sign in to comment.