The results of a study on Python code susceptibility to errors associated with incorrect comma usage in code have been published. The issues arise because Python automatically concatenates strings in a list if they are not separated by a comma, and also treats a value as a tuple if it is followed by a comma. By conducting an automated analysis of 666 GitHub repositories with Python code, researchers identified potential comma issues in 5% of the studied projects.
Further manual inspection revealed that actual errors were present in only 24 repositories (3.6%), while the remaining 1.4% were false positives (for instance, a comma might have been intentionally omitted between lines to concatenate broken file paths, long hashes, HTML blocks, or SQL expressions). Notably, among the 24 repositories with actual errors were large projects such as Tensorflow, Google V8, Sentry, Pydata xarray, rapidpro, django-colorfield, and django-helpdesk. Furthermore, comma issues are not specific to Python and frequently occur in C/C++ projects as well (recent fixes include LLVM, Mono, Tensorflow).
The main types of errors studied include:
- An accidentally omitted comma in lists, tuples, and sets, leading to concatenation of strings instead of their interpretation as separate values. For example, in Sentry, one of the tests had a comma missing between the strings "releases" and "discover" in the list, resulting in a check for a non-existent handler "/releasesdiscover" instead of separate checks for "/releases" and "/discover."

Another example is a missing comma in rapidpro that led to two different rules being concatenated in line 572:

- A missing comma at the end of a single-element tuple definition results in the assignment of a regular type instead of a tuple. For example, the expression 'values = (1,)' will assign a single-element tuple to the variable, while 'values = (1)' will assign an integer type. The parentheses in the mentioned assignments do not affect the type definition and are optional; the presence of a tuple is determined by the parser solely based on the presence of commas. REST_FRAMEWORK = { 'DEFAULT_PERMISSION_CLASSES': ( 'rest_framework.permissions.IsAuthenticated' # instead of a tuple, a string will be assigned. ) }
- The reverse situation is extra commas during assignment. If a comma is accidentally left at the end of an assignment, a tuple will be assigned instead of a regular type (for example, if 'value = 1' is specified as 'value = 1,').
Source: opennet.ru


