Skip to content
This repository

Dedent prefix bugfix + tests: #142 #346

Merged
merged 2 commits into from over 3 years ago

3 participants

David Warde-Farley Thomas Kluyver Fernando Perez
David Warde-Farley
dwf commented

Re: issue #142, this expands the inputsplitter regex for 'dedentation' to correctly handle ignore lines such as "passes += 1", which should not dedent even though it begins with "pass". It's fine with arbitrary trailing whitespace and a few other wacky corner cases (return() is valid Python, as is return(0), same with raise(TypeError()), etc. -- these only dedent if you at least have a close paren after the open paren.)

Also adds tests for these new behaviours.

Thomas Kluyver
Collaborator

Works for me. Thanks, David.

David Warde-Farley
dwf commented

Okay, I think I've got the hang of it. It appears you can have a period afterward and it will still work (tested in a dummy project). Sorry if anyone got flooded with email. :P

Fernando Perez fperez merged commit 2b92905 into from
Fernando Perez fperez closed this
Fernando Perez
Owner

Thanks a lot David! Merged and pushed. Good luck with those exams :)

David Warde-Farley
dwf commented
Damián Avila damianavila referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

Apr 09, 2011
David Warde-Farley dwf New tests related to issue #142. 47ca451
Apr 10, 2011
David Warde-Farley dwf Fix behaviour of dedent triggering. Closes gh-142. 2b92905
This page is out of date. Refresh to see the latest.
8 IPython/core/inputsplitter.py
@@ -103,7 +103,13 @@
103 103 # while developing.
104 104
105 105 # compiled regexps for autoindent management
106   -dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
  106 +dedent_re = re.compile('|'.join([
  107 + r'^\s+raise(\s.*)?$', # raise statement (+ space + other stuff, maybe)
  108 + r'^\s+raise\([^\)]*\).*$', # wacky raise with immediate open paren
  109 + r'^\s+return(\s.*)?$', # normal return (+ space + other stuff, maybe)
  110 + r'^\s+return\([^\)]*\).*$', # wacky return with immediate open paren
  111 + r'^\s+pass\s*$' # pass (optionally followed by trailing spaces)
  112 +]))
107 113 ini_spaces_re = re.compile(r'^([ \t\r\f\v]+)')
108 114
109 115 # regexp to match pure comment lines so we don't accidentally insert 'if 1:'
37 IPython/core/tests/test_inputsplitter.py
@@ -192,13 +192,42 @@ def test_indent3(self):
192 192 isp.push(" x = (1+\n 2)")
193 193 self.assertEqual(isp.indent_spaces, 4)
194 194
195   - def test_dedent(self):
  195 + def test_dedent_pass(self):
196 196 isp = self.isp # shorthand
197   - isp.push('if 1:')
  197 + # should NOT cause dedent
  198 + isp.push('if 1:\n passes = 5')
198 199 self.assertEqual(isp.indent_spaces, 4)
199   - isp.push(' pass')
  200 + isp.push('if 1:\n pass')
200 201 self.assertEqual(isp.indent_spaces, 0)
201   -
  202 + isp.push('if 1:\n pass ')
  203 + self.assertEqual(isp.indent_spaces, 0)
  204 +
  205 + def test_dedent_raise(self):
  206 + isp = self.isp # shorthand
  207 + # should NOT cause dedent
  208 + isp.push('if 1:\n raised = 4')
  209 + self.assertEqual(isp.indent_spaces, 4)
  210 + isp.push('if 1:\n raise TypeError()')
  211 + self.assertEqual(isp.indent_spaces, 0)
  212 + isp.push('if 1:\n raise')
  213 + self.assertEqual(isp.indent_spaces, 0)
  214 + isp.push('if 1:\n raise ')
  215 + self.assertEqual(isp.indent_spaces, 0)
  216 +
  217 + def test_dedent_return(self):
  218 + isp = self.isp # shorthand
  219 + # should NOT cause dedent
  220 + isp.push('if 1:\n returning = 4')
  221 + self.assertEqual(isp.indent_spaces, 4)
  222 + isp.push('if 1:\n return 5 + 493')
  223 + self.assertEqual(isp.indent_spaces, 0)
  224 + isp.push('if 1:\n return')
  225 + self.assertEqual(isp.indent_spaces, 0)
  226 + isp.push('if 1:\n return ')
  227 + self.assertEqual(isp.indent_spaces, 0)
  228 + isp.push('if 1:\n return(0)')
  229 + self.assertEqual(isp.indent_spaces, 0)
  230 +
202 231 def test_push(self):
203 232 isp = self.isp
204 233 self.assertTrue(isp.push('x=1'))

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.