Sublime Forum

Red highlighting on end of Python function arguments that include tuples

#1

On any function definitions that have arguments that include a tuple, such as that depicted below, I keep getting the red highlighting indicating incorrect syntax, but as far as I am aware, this is correct?

0 Likes

#2

That is invalid. Python functions are defined with arguments or keyword arguments. You can default them with a tuple, but you don’t define the argument name as a tuple.

Okay

def fusep(graph, na, nb, p):

Okay

def fusep(graph, na_nb=(1, 2), p=3):

Not okay

def fusep(graph, (na, nb), p):

If you try to put this in Python, this is what you get:

>>> def fusep(graph, (na, nb), p):
  File "<stdin>", line 1
    def fusep(graph, (na, nb), p):
                     ^
SyntaxError: invalid syntax
>>> 
3 Likes

#3

Upon further research, this appears to have been changed in Python 3, as explained here.

However, I am currently coding in Python 2, so for me this is a valid syntax use.

Can I therefore change sublime text to only detect Python 2 syntax errors?

0 Likes

#4

Even if this is valid in old versions of Python, I would suggest you move to a more widely accepted, modern methodology. There is a reason it is not valid in later versions. It is pretty un-intuitive in my opinion, and you don’t gain anything by doing things in this manner. And generally, while there may be some exceptions, modern Python coders don’t do this anymore. Obviously this is just a suggestion, so you can make your own choice.

If you are adamant in using outdated syntax, you can override or create your own Python syntax based of the default.

1 Like

#5

You can create an issue at https://github.com/sublimehq/Packages/issues/new. Be sure to include copy-pastable example code so contributors can copy-paste your example to add it to the tests.

0 Likes