loading...
Cover image for DeepCode's Top Findings #13: Writing Immutable Objects in Python
DeepCode.AI

DeepCode's Top Findings #13: Writing Immutable Objects in Python

cu_0xff profile image cu_0xff 🇪🇺 Originally published at Medium ・2 min read

DeepCode Top Findings (13 Part Series)

1) DeepCode’s Top Findings #1: Java Date (This one made me dizzy) 2) DeepCode’s Top Findings #2: Java/Python Hard-Coded Password 3 ... 11 3) DeepCode’s Top Findings#3: Java missing Close or Flush 4) DeepCode’s Top Findings#4: JavaScript Attribute Access on NULL 5) DeepCode’s Top Findings#5: JavaScript Unsanitized Input is used to build RegEx 6) DeepCode’s Top Findings#6: JavaScript Result of Typeof not compared with String 7) DeepCode’s Top Findings#7: Python Use Real Floor Division 8) DeepCode's Top Findings #8: The ZIP Slip 9) DeepCode's Top Findings #9: Deadlocks 10) DeepCode's Top Findings #10: Confusing Use of '!' 11) DeepCode's Top Findings #11: Synchronizing Strings 12) DeepCode's Top Findings #12: Integer Promotion on Bitwise Operations in C 13) DeepCode's Top Findings #13: Writing Immutable Objects in Python

DeepCode offers an AI-based Static Program Analysis for Java, JavaScript and TypeScript, C/C++ and Python. As you might know, DeepCode uses thousands of open source repos to train our engine. We asked the engine team to provide some stats on the findings. On the top suggestions from our engine, we want to introduce and give some background in this series of blog articles.

Language: Python
Defect: X is an immutable object and one of its elements gets assigned
Diagnose: The code tries to write to an element within an instance of an immutable data type

Writing into Range

I found this example in monkeylyf / interviewjam and as usual, you can open the repo in DeepCode and follow along. Below is the snippet of the dashboard in DeepCode:

def findCelebrityWithExtraSpace(self, n):
    """
    :type n: int
    :rtype: int
    """
    stack = range(n)
...
            stack[-2] = a
...

DeepCode gives us the feedback "Trying to store a value in element of immutable type range (from call to range) will lead to a crash." But does range() not bring a list back? Well, that used to be...

Let us replay what happens in the Python interpreter:

Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 22:39:24) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> stack = range(10)
>>> stack
range(0, 10)
>>> a = stack[-1]
>>> stack[-2] = a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'range' object does not support item assignment
>>>

What we are calling here is the range() function. In Python3 it is a renamed version of Python2's xrange() function. This means it does not actually produce a list with a sequence of values. Rather, it uses yielding to produce the specific value when it is needed instead of storing vast amounts of data. That is the reason why Python says the range object does not support item assignment. And by the way, it also does not support the pop() function which is used in the code example.

In the above code example, the application would crash when trying to write into the range object. Yet, as ranges are used often as a sequencer for loops, ranges can get quite large. Thus it makes sense to use such a range object instead of a list. Yet, if you need the list as a datastore - such as in our example here - there is an easy way to get lists with value sequences:

stack = list(range(n))

For the static code analysis, the task here was to understand the underlying type returned by the range() function and infer the abilities the type has (like supporting index operators).

Test it yourself and run analysis over your code. It is very fast and free to check out at deepcode.ai.

DeepCode Top Findings (13 Part Series)

1) DeepCode’s Top Findings #1: Java Date (This one made me dizzy) 2) DeepCode’s Top Findings #2: Java/Python Hard-Coded Password 3 ... 11 3) DeepCode’s Top Findings#3: Java missing Close or Flush 4) DeepCode’s Top Findings#4: JavaScript Attribute Access on NULL 5) DeepCode’s Top Findings#5: JavaScript Unsanitized Input is used to build RegEx 6) DeepCode’s Top Findings#6: JavaScript Result of Typeof not compared with String 7) DeepCode’s Top Findings#7: Python Use Real Floor Division 8) DeepCode's Top Findings #8: The ZIP Slip 9) DeepCode's Top Findings #9: Deadlocks 10) DeepCode's Top Findings #10: Confusing Use of '!' 11) DeepCode's Top Findings #11: Synchronizing Strings 12) DeepCode's Top Findings #12: Integer Promotion on Bitwise Operations in C 13) DeepCode's Top Findings #13: Writing Immutable Objects in Python

Posted on by:

cu_0xff profile

cu_0xff 🇪🇺

@cu_0xff

Veteran in IT, Xoogler, Ex-Microsoft, works in Static Program Analysis

DeepCode.AI

DeepCode learns from GitHub project data to give developers AI-powered code reviews

Discussion

markdown guide