'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.



Solution 1:[1]

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

Solution 2:[2]

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.

Solution 3:[3]

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.

Solution 4:[4]

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

Solution 5:[5]

Had the same problem after upgrading my pycharm-professional snap on Ubuntu 21.*. Fixed it reinstalling pycharm using jetbrains-toolbox.

Solution 6:[6]

On PyCharm 2021.2.2 Professional Edition i see the error in following case

defthe_fun_one(cls):
 cls.some_module.some_function()

def the_fun_two(cls):
 cls.some_other_module.some_other_function()

I see the Statement expected, found Py:DEDENT on line cls.some_other_module.some_other_function() when a previous function had def not separated by a space typo like defthe_fun_one(cls):

Solution 7:[7]

  • List item

I had an issue where I had an extra -> returnobject in my file. So it was a syntax error.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 bad_coder
Solution 2
Solution 3 David Warnke
Solution 4
Solution 5 NAX
Solution 6 Nafeez Quraishi
Solution 7 Thomas Pickett