[python mode] Fix handling of scope end by dedent

Issue #2503