Eglot Python: How to add "'--additional-builtins" to syntax checker (flake8?)

When it comes to Python some linters (e.g. PyLint) offer a option “–additional-builtins” to define extra names that should be ignored (or considered as Python build ins). This is help full when you use localziation via GNU gettext.

This works well when using linters on shell. But not when using Emacs with eglot.

This functions are always marked as errors (red underscores):

_('translate me')
ngettext('one', 'more', 2)

I am far away from being a Lisp expert. I setup my Emacs based on Davids videos. I am using eglot and python-language-server (pylsp) in the back.

I am even not sure which linter is used by eglot or the language server. I am even not sure who execute the linter: Eglot or the language server? Based on my “Messages” buffer I am assuming it is “flake8”. There seems to be a command line option –buildins= offering that behavior I need.

But I don’t know how to start in Emacs/eglot/pylsp/somewherelese to add this option.

EDIT:
I set this alias in my bash: alias flake8="flake8 --builtins=_,ngettext" . This works on shell but has no effect on the flake8 instance used by my Emacs environment.

EDIT2:
I am so doomed that I also “asked” ChatGPT. Shame on me. Because of that I added the :config section to this python config. But without effect but also no error.

;; ...snipped...

(use-package python
  :ensure nil  ; in-build!
  :hook
  (python-mode . (lambda ()
   		   (if (not (string-equal (system-name) "quark"))
   		       (eglot-ensure)))
  )
  ;; (python-mode . eglot-ensure)
  (python-mode . ws-butler-mode)
  (python-mode . hs-minor-mode)
  :custom
  (python-shell-interpreter "python3")
  :config
  (setq-default eglot-workspace-configuration
    '((:pylsp . (:plugins (:pyflakes (:enabled t)
                           :flake8 (:enabled t
                                    :builtins ["_", "ngettext"]
                                    )
                           )
                )
     ))
  )
)

;; ...snipped...

(use-package eglot
  ;; :hook
  ;; (prog-mode . eglot-ensure)
  :bind (:map eglot-mode-map
	      ("C-c a r" . #'eglot-rename))
  :custom
  (eglot-autoshutdown t)
  (flymake-no-changes-timeout 5)
  )

I’m pretty bad at Emacs config, but I’ve managed to set up a more or less productive C++ dev environment for myself. I’ve had a similar need to pass extra cmd line args to LSP server like follows:

(use-package eglot
  :config
  (add-to-list 'eglot-server-programs
               '((c++-mode c-mode) "clangd" "--enable-config" "--background-index")))

I think you first need to find out which LSP server Eglot is using for Python. It has autodetection, but you can use a prefix argument to select a specific one, I think.

Then you can look in that LSP server’s documentation about linting configuration. It may or may not mean making the change in Eglot’s config.

Another random test post from within emacs. I apologise in advance, I am just testing out features. Thanks.

1 Like