Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expected 'collections.Iterable', got Optional[list] instead

I received this error in Pycharm Community Edition 5.0.3 today and was wondering whether it was just something I'm doing wrong/not realizing, or if it is a PyCharm lint issue. The code to reproduce the error is

mylist = list()

# fill mylist, or do nothing here, either way the error persists

if mylist:
    # if something in the list...
    mylist.append(2)
else:
    # list is empty, add something
    mylist.append(1)

# warning at the loop here
for val in mylist:
    print val

Is this because it's thinking that mylist is a union of type ?

like image 960
Muckle_ewe Avatar asked Oct 21 '25 11:10

Muckle_ewe


1 Answers

Optional[list] is a typing annotation that signals PyCharm found mylist to be either None or a list object.

This appears to be caused by the if mylist: test; I'd say this is PyCharm making an incorrect inference here, since you clearly set mylist to a list instance only one line before. if tests for emptiness, not if the object is None.

This is a bug, filed with the PyCharm project as issue PY-21897, and fixed in PyCharm verson 2017.1.

like image 190
Martijn Pieters Avatar answered Oct 24 '25 05:10

Martijn Pieters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!