samuelcolvin / python-devtools

Dev tools for python
https://python-devtools.helpmanual.io/
MIT License
985 stars 47 forks source link

debug reports indentation error inside of if-else blocks within __init__ #85

Closed awray3 closed 3 years ago

awray3 commented 3 years ago

I noticed this strange behavior when using debug to debug some if-else blocks in my code. Here is a small script to recreate the bug.

# script.py
from devtools import debug

class Test:
    def __init__(self, flag: bool):
        if flag:
            debug('Flag was true')
        else:
            debug('Flag was false')

# instantiate this class with True flag
test = Test(flag=True)

Output of python script.py:

script.py:7 __init__ (error parsing code, IndentationError: unindent does not match any outer indentation level (script.py, line 3))
    'Flag was true' (str) len=13

There is no indentation error in the code, and furthermore, I notice that adding something benign after the debug statement makes this go away. For example, if instead script.py looks like this, there is no issue:

# script2.py
from devtools import debug

class Test:
    def __init__(self, flag):
        if flag:
            debug('Flag was true')
            print('This helps debug out for some reason.')
        else:
            debug('Flag was false')

test = Test(True)

Output of python script2.py (which I expect to happen normally):

script2.py:7 __init__
    'Flag was true' (str) len=13
This helps debug out for some reason.

Environment info: I tested this by creating a new conda environment with python 3.7.

$ conda --version
conda 4.10.1

$ python --version
python 3.7.10

$ pip list
Package    Version
---------- -------------------
certifi    2021.5.30
devtools   0.6.1
pip        21.1.3
setuptools 52.0.0.post20210125
wheel      0.36.2
alexmojaki commented 3 years ago

Fixed in #82

samuelcolvin commented 3 years ago

fixed in #82