Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Statement expected, found py: Dedent

We are willing/ forced to develop a small Web App for the university. Now we started and everything seems to be fine, until the above strange error raises.

Statement expected, found py: Dedent

The error is raised by the following lines of code:

def get_reset_token(self, mysql, userid):
    try:
        conn = mysql.connect()
        cursor = conn.cursor()
        cursor.execute("""SELECT token FROM tralala_reset_password 
                       WHERE uid=(%s)""", userid)
        data = cursor.fetchall()
        cursor.close()
        conn.close()
        return data[0]
    except Exception as e:
        app.logger(str(e))
        return ""

PyCharm started to mark the return "" statement.

like image 994
Keine-Ahnung Avatar asked Dec 12 '17 15:12

Keine-Ahnung


4 Answers

If you face this issue in PyCharm 2021.2 add the following line

-ea

to Help | Edit Custom VM Options ... or to <PyCharm_installation_folder>/bin/pycharm.vmoptions (or pycharm64.vmoptions). Restart PyCharm and the parser should work correctly.

See the relevant ticket in PyCharm's bug tracker https://youtrack.jetbrains.com/issue/PY-49970


Update: the fix is available in 2021.2.1 RC.

like image 119
Pavel Karateev Avatar answered Nov 09 '22 21:11

Pavel Karateev


Problem solved by ignoring the error. Copied to an other editor and nothing here. So seems to be a PyCharm mistake.

like image 27
Keine-Ahnung Avatar answered Nov 09 '22 21:11

Keine-Ahnung


My problem was caused by indentation mismatch. Most of the document was indented with spaces but there were some tabs copied in that caused the Py:DEDENT error. Replacing the tabs with spaces fixed the error.

like image 7
David Warnke Avatar answered Nov 09 '22 21:11

David Warnke


I was also scratching my head for significant period of time and finally figured it out.

The thing is outside of "pycharm did not recognize certain char" scope

When you write this:

class Foo:
   def complicated_method(self):
       for f to self.whatever:
           # plenty of code goes here
           pass
   def another one():
       # here too
       pass

And then you decide to rewrite it:

class Foo:
   def complicated_method(self):
           # plenty of code goes here  <- mistakenly leaved unindented, many unseen errors here
           pass
   def another one(self):
       # here too
       pass
    ....
   def do(self):
       for f in self.whatever:
           self.complicated_method() <- here will be Py:DEDENT

Refactor long methods, if you can, and Py:DEDENT will never bother you again

like image 3
im_infamous Avatar answered Nov 09 '22 19:11

im_infamous