gruns / icecream

🍦 Never use print() to debug again.
MIT License
9.22k stars 187 forks source link

IndentationError #117

Closed lwilhoit closed 2 years ago

lwilhoit commented 2 years ago

I just started getting an IndentationError for any calls to ic. I actually made no changes to my script but the next time I ran it I now get that error. My python script is quite large and unfortunately I cannot replicate the error with a smaller script. Here is the full traceback:

Traceback (most recent call last): File "C:\docs\python\gui\pur.py", line 985, in self.ui.which_rate_ai_lw.itemClicked.connect(lambda: self.setSelectList('ai_name', 'rate')) File "C:\docs\python\gui\pur.py", line 1454, in setSelectList ic(self.ai_list) File "C:\docs\python\gui\pur_env\lib\site-packages\icecream\icecream.py", line 181, in call out = self._format(callFrame, *args) File "C:\docs\python\gui\pur_env\lib\site-packages\icecream\icecream.py", line 215, in _format out = self._formatArgs( File "C:\docs\python\gui\pur_env\lib\site-packages\icecream\icecream.py", line 222, in _formatArgs sanitizedArgStrs = [ File "C:\docs\python\gui\pur_env\lib\site-packages\icecream\icecream.py", line 223, in source.get_text_with_indentation(arg) File "C:\docs\python\gui\pur_env\lib\site-packages\icecream\icecream.py", line 119, in get_text_with_indentation result = self.asttokens().get_text(node) File "C:\docs\python\gui\pur_env\lib\site-packages\executing\executing.py", line 416, in asttokens return ASTTokens( File "C:\docs\python\gui\pur_env\lib\site-packages\asttokens\asttokens.py", line 59, in init self._tokens = list(self._generate_tokens(source_text)) File "C:\docs\python\gui\pur_env\lib\site-packages\asttokens\asttokens.py", line 85, in _generate_tokens for index, tok in enumerate(tokenize.generate_tokens(io.StringIO(text).readline)): File "C:\Users\lwilhoit\AppData\Local\Programs\Python\Python310\lib\tokenize.py", line 514, in _tokenize raise IndentationError( File "", line 1680 def setAdjuvants(self): IndentationError: unindent does not match any outer indentation level

alexmojaki commented 2 years ago

Interesting. Please see if you get the same error with this script:

import executing

executing.Source.for_filename("pur.py").asttokens()

If so, you should be able to delete parts of pur.py to narrow down the problem.

lwilhoit commented 2 years ago

I finally got a shorter script that resulted in this error, which occurs when there is a "\" at the beginning of a line:

from icecream import ic

def ic_test_func():
    test_value = 3

    ic(test_value)
\
    if test_value == 3:
        ic('value is 3')
    else:
        ic('value is not 3')

ic_test_func()
lwilhoit commented 2 years ago

My previous code did not format correctly. See the attached file ic_test_func.txt .

alexmojaki commented 2 years ago

I'm very surprised that such a script is valid Python syntax. The traceback you shared shows Python's own tokenizer failing on it. I get the same error message running python -m tokenize script.py in a terminal. PyCharm underlines the area in red thinking it's invalid syntax. And yet it does run!

Is there any reason you have a slash there like that?

lwilhoit commented 2 years ago

No, that was just an overlooked and accidental hitting the β€œ\” key.

salabim commented 2 years ago

I also found it weird. In order to see check the behaviour, I made a small test script

def m():
    print(1)
\
    if True:
        print(2)
    else:
        print(3)

m()

Under Python 3.6 and 3.8 this indeed gives an IndentationError at compile time. But, under Python 3.10 it runs and results in:

1
2

No idea what's behind that.

alexmojaki commented 2 years ago

It first works in 3.9. Looks like a quirk of the new 3.10 PEG parser.

I don't think icecream should deal with this. Maybe open a bug on bugs.python.org if there isn't one already.

gruns commented 2 years ago

@alexmojaki agreed this is beyond the scope of icecream πŸ‘

@lwilhoit really interesting find. ive never seen py code formatted like this, either. thanks for opening this issue!