pylint-bot / test

0 stars 0 forks source link

KeyError when trying to infer what an instance's __getitem__ returns when called with a slice #200

Closed pylint-bot closed 8 years ago

pylint-bot commented 9 years ago

Originally reported by: Claudiu Popa (BitBucket: PCManticore, GitHub: @PCManticore?)


Given the following code, astroid crashes with a KeyError. This is happening because Instance.getitem always wraps its arguments in a Const object, while sometimes it gets passed a slice object. It should use objects.Slice instead and probably it shouldn't do the wrapping itself, but the caller should be responsible for this.

#!python

from astroid.test_utils import extract_node
n = extract_node('''
class A:
   def __getitem__(self, k):       
       return k

A()[:42] #@

''')
print(next(n.infer()))

pylint-bot commented 9 years ago

Original comment by Claudiu Popa (BitBucket: PCManticore, GitHub: @PCManticore?):


Don't wrap instance's arguments into a Const, instead let the original index object to be used.

This prevents a crash when the original index object is a slice object, which was wrapped inadvertendly in a Const node, leading to a crash later on if the said object was inferred. Closes issue #200.