Elpy: as you type syntax checking

Created on 1 Oct 2015  路  11Comments  路  Source: jorgenschaefer/elpy

As far as I can tell Elpy only checks syntax when a file is saved (the doc is consistent with that, saying: _Whenever you save a file, Elpy will run a syntax check and highlight possible errors or warnings inline_).

It'd be lovely to have a way to configure Elpy so that syntax checking is done on the fly, while you type.
That's because if you don't save often, it might happen that you've introduced pep8 issues in a place that is not visible when you save, resulting in overlooked issue unless you also do C-c C-v.

(As a background, before switching to Elpy I used to have a much simpler Emacs/Python configuration, which was not up to par with what Elpy does, but which was better in the above respect. Here's what I was using:

(add-hook 'python-mode-hook 'flymake-mode)
(add-hook 'python-mode-hook 'flymake-python-pyflakes-load)
(setq flymake-python-pyflakes-executable "flake8")

having back support for something like the above with Elpy would be great.)

Thanks for Elpy!

Question

Most helpful comment

Did you check those values in a Python buffer? Elpy sets those values buffer-local in Python buffers, not globally.

Ah, yes!, that was it. I've fixed my problem with this:

(add-hook 'elpy-mode-hook
          '(lambda ()
             (progn
               (setq-local flymake-start-syntax-check-on-newline t)
               (setq-local flymake-no-changes-timeout 0.5))))

Thanks a lot for your support.

All 11 comments

Hello, and thanks for the report :-)

On-the-fly checking of syntax problems has caused lots of problems for me, because Python files tend not to have correct syntax most of the time while editing for me, so I tended to have the current line highlighted a lot. Hence why Elpy disables this behavior.

You can simply remove elpy-module-flymake from elpy-modules. Elpy will then not configure flymake at all, and you can configure as you like.

Alternatively, you can add a function to elpy-mode-hook to change flymake-start-syntax-check-on-newline and flymake-no-changes-timeout to your liking, which are the two settings Elpy changes from the default.

Let me know if this solves your problem! :-)

On Thu, Oct 01, 2015 at 01:18:16AM -0700, Jorgen Sch盲fer wrote:

Hello, and thanks for the report :-)

Thank you for your quick feedback :-)

You can simply remove elpy-module-flymake from elpy-modules. Elpy
will then not configure flymake at all, and you can configure as you
like.

I'll try this, and let know here how it goes. OTOH,

Alternatively, you can add a function to elpy-mode-hook to change
flymake-start-syntax-check-on-newline and
flymake-no-changes-timeout to your liking, which are the two
settings Elpy changes from the default.

this doesn't work, and this is part of why I'm puzzled. What I see is
that flymake-start-syntax-check-on-newline is already set to true, and
flymake-no-changes-timeout is set to 0.5 --- I haven't changed those
values, so I suspect they're Elpy's defaults. But the point is, they
don't seem to work: flymake is certainly not being invoked when newlines
are added/removed, nor after 0.5 seconds since the last change.

Is that the expected behavior or not?

(the interaction between Elpy and Flymake are not entirely clear to me
after reading the doc)

Cheers

(the interaction between Elpy and Flymake are not entirely clear to me
after reading the doc)

Did you check those values in a Python buffer? Elpy sets those values buffer-local in Python buffers, not globally.

Did you check those values in a Python buffer? Elpy sets those values buffer-local in Python buffers, not globally.

Ah, yes!, that was it. I've fixed my problem with this:

(add-hook 'elpy-mode-hook
          '(lambda ()
             (progn
               (setq-local flymake-start-syntax-check-on-newline t)
               (setq-local flymake-no-changes-timeout 0.5))))

Thanks a lot for your support.

You're welcome, and enjoy Elpy! :-)

I actually encountered same problem now, but 3 years gone since issue was raised.
I just installed elpy, made venv, elpy-config, and issue was there - flymake did not checked syntax every 0.5 sec. by default it took a minute to refresh\redraw syntax highlights. even when elpy config stated that lymake-no-changes-timeout equal 0.5

Adding those strings from last zacchiro post solved my case. But it took me a long to find this solution and this was not easy\obvious.
@jorgenschaefer don't you think this is an bug, and it should be solved by elpy package, since elpy explicitly declares it as configurable option? but changing the value of this option does not make any affect, but it should as promoted.

Since user open *.py files in elpy mode , and all other features of elpy-mode and flymake seem to work out of the box (like autocompleteion, syntax check during initial file opening, and so on), without the special requirement to manually edit ~/.emacs , so mortal guess flymake-no-changes-timeout from elpy-config menu, should too, if it has been set there.

=======
sincerely thanks for beautiful elpy package

I'm afraid I do not know what is happening there. Does it also happen if you do not have any personal configuration?

without ~/.emacs file , my emacs doesn't load elpy and can't activate virtual env.

https://gist.github.com/lexleogryfon/d0da72f9505772849573063d14802d4f

GNU Emacs 25.3.1
ls ~/.emacs.d/elpa/
archives find-file-in-project-5.4.7 pyvenv-1.11
company-0.9.4 highlight-indentation-0.7.0 s-1.12.0
elpy-1.18.0 ivy-0.10.0 yasnippet-0.12.2

Virtualenv........: (/.../nixos_cpython35_venv/)
RPC Python........: 3.5.4 (/.../nixos_cpython35_venv/bin/python)
Interactive Python: python (/.../nixos_cpython35_venv/bin/python)
Emacs.............: 25.3.1
Elpy..............: 1.18.0
Jedi..............: 0.11.1
Rope..............: 0.10.7
Autopep8..........: 1.3.4
Yapf..............: 0.20.1
Syntax checker....: flake8 (/../nixos_cpython35_venv/bin/flake8)

*path's were edited

You can start Emacs with emacs -Q, then run M-x package-initialize and M-x elpy-enable to test.

Tried your recommendation, issue still exist. flymake refreshes syntax highlight only after ~1 minute. I start emacs -Q -nw because my main goal\use-case is to use it as CLI editor. But issue still exist and could be reproduced with GUI `emacs -Q', in my environment.

I am afraid I have no idea what might be causing this.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kirk86 picture kirk86  路  5Comments

SergiyKolesnikov picture SergiyKolesnikov  路  3Comments

mdk2029 picture mdk2029  路  5Comments

yitang picture yitang  路  4Comments

rthompsonj picture rthompsonj  路  6Comments