← index #1710Issue #6096
Related · high · value 3.318
QUERY · ISSUE

bytearray slice assignment fails for derived types

openby mbueschopened 2015-12-11updated 2024-08-28
docs

The type check for slice assignment to bytearray derived types seems to be a bit too strict:

class MyBytearray(bytearray): pass

a = MyBytearray(4)
b = MyBytearray(4)
a[0:4] = b

results in this error:

Traceback (most recent call last):
  File "test.py", line 5, in <module>
NotImplementedError: array/bytes required on right side
CANDIDATE · ISSUE

Bytearray slice assignement throwing exception on ESP32 idf v4.

closedby tionebrropened 2020-05-30updated 2020-05-31

Following the example in the documentation:
https://docs.micropython.org/en/latest/genrst/builtin_types.html#bytearray

>>> b = bytearray(4)
>>> b[0:1] = [1, 2]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NotImplementedError: array/bytes required on right 
>>> print(b)
bytearray(b'\x00\x00\x00\x00')

I noticed that assigning b'\x01\x02' works.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied