Skip to content

Conversation

@dlax
Copy link
Contributor

@dlax dlax commented Oct 28, 2021

The type declaration is:

def __setitem__(index: slice, value: Iterable[Range[T]]) -> None:
    ...

so passing a non-iterable value should not be allowed. However, the
method implementation makes a special-case for non-iterable values with
a slice.

In accordance with MutableSequence behavior, e.g.:

  >>> x = [1, 2, 3]
  >>> x[1:3] = 0
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
  TypeError: can only assign an iterable

it seems more correct to raise a TypeError.

(Alternatively, we could change the type declaration of MultiRange__setitem__() but that would make it incompatible with MutableSequence, which seems wrong.)

@dvarrazzo
Copy link
Member

Ahhhh... I was sure I had tested it and that it was the expected behaviour...

>>> L = list('danie')
>>> L
['d', 'a', 'n', 'i', 'e']

>>> L[2:4] = 'x'
>>> L
['d', 'a', 'x', 'e']

except that a string is iterable... 😅

… value

The type declaration is:

    def __setitem__(index: slice, value: Iterable[Range[T]]) -> None:
        ...

so passing a non-iterable value should not be allowed. However, the
method implementation makes a special-case for non-iterable values with
a slice.

In accordance with MutableSequence behavior, e.g.:

  >>> x = [1, 2, 3]
  >>> x[1:3] = 0
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
  TypeError: can only assign an iterable

it seems more correct to raise a TypeError.
@dlax dlax force-pushed the multirange-setitem branch from 3e56b40 to 14767b6 Compare October 28, 2021 12:03
@dvarrazzo dvarrazzo merged commit 10ee2e9 into psycopg:master Oct 28, 2021
@dlax dlax deleted the multirange-setitem branch October 30, 2021 06:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants