jorgenschaefer / elpy

Emacs Python Development Environment
GNU General Public License v3.0
1.89k stars 259 forks source link

conditional imports consistently crash elpy #1759

Open garberw opened 4 years ago

garberw commented 4 years ago

Summary

The conditional before the import causes emacs to crash when editing the following file. There are two versions of Ui_TicketOrderDialog which were generated by designer-qt4. Not sure whether it is flycheck, elpy or py-autopep8 or jupyter that is crashing. Making this import not inside a conditional solves problem consistently. I think this "bug" should not be changed, maybe however you should include a warning for those putting an import inside a conditional ("if" statement). Basically it is foolish for me to put an import inside a condidtional and you are not supposed to do that. If this were C++ not python I could use something like a template to avoid the conditional import.

if choice == 1:
    from ui_wg_ticketorderdlg1 import Ui_TicketOrderDialog
elif choice == 2:
    from ui_wg_ticketorderdlg2 import Ui_TicketOrderDialog
else:
    raise ValueError(choice)

class TicketOrderDialog(QDialog, Ui_TicketOrderDialog):
    def __init__(self, parent=None):
        super(TicketOrderDialog, self).__init__(parent)
        self.setupUi(self)
        dt_beg = QDateTime.currentDateTime()
        dt_end = dt_beg.addYears(1)
        self.w1_dateTimeEdit.setDateTimeRange(dt_beg, dt_end)
        self.w1_dateTimeEdit.setDateTime(dt_beg)
        self.w4_lineEdit_amount.setFocusPolicy(Qt.NoFocus)
        self.w0_lineEdit_customer.setFocus()
        self.updateUi()

Steps to reproduce

try typing in the code above

My configuration

OS

fedora 31 linux

Result of (elpy-config)

Elpy Configuration

Emacs.............: 26.3 Elpy..............: 1.32.0 Virtualenv........: None Interactive Python: jupyter 4.5.0 (/usr/bin/jupyter) RPC virtualenv....: rpc-venv (/home/garberw/.emacs.d/elpy/rpc-venv) Python...........: python 3.7.6 (/home/garberw/.emacs.d/elpy/rpc-venv/bin/python) Jedi.............: 0.16.0 Rope.............: 0.16.0 Autopep8.........: 1.5 Yapf.............: 0.29.0 Black............: 19.10b0 Syntax checker....: flake8 (/home/garberw/.local/bin/flake8)

Warnings

You have not activated a virtual env. While Elpy supports this, it is often a good idea to work inside a virtual env. You can use M-x pyvenv-activate or M-x pyvenv-workon to activate a virtual env.

Elpy configuration in my init.el

(defvar wg-python-packages '(better-defaults ;; minor changes to emacs defaults;; I do not like this ????
elpy ;; comprehensive ide;; see below
flycheck ;; on-the-fly syntax checking
;; pick one of following two lines
py-autopep8 ;; run autopep8 on save
;; blacken ;; run black formatting on save
ein ;; ein-notebook
;; ein-subpackages
) )

(mapc #'(lambda (package) (unless (package-installed-p package) (package-install package)) ) wg-python-packages)

(setq inhibit-startup-message t) ;; hide startup message
;; (global-linum-mode t) ;; line numbers
(global-set-key (kbd "\C-c n") "\M-x global-linum-mode") ;; toggle line numbers
(elpy-enable) ;; include elpy in wg-python-packages

;; use IPython for REPL
(setq python-shell-interpreter "jupyter" python-shell-interpreter-args "console --simple-prompt" python-shell-prompt-detect-failure-warning nil) (add-to-list 'python-shell-completion-native-disabled-interpreters "jupyter")

;; enable flycheck ;; could not find flycheck (alone);; use flycheck-pyflakes
(when (load "flycheck" t t) (setq elpy-modules (delq 'elpy-module-flymake elpy-modules)) (add-hook 'elpy-mode-hook 'flycheck-mode) )

;; This package adds support for pyflakes to flycheck. To use it, add to your init.el:
(require 'flycheck-pyflakes) (add-hook 'python-mode-hook 'flycheck-mode)

;; If you want to use pyflakes you probably don't want pylint or flake8.
;; To disable those checkers, add the following to your init.el:
(add-to-list 'flycheck-disabled-checkers 'python-flake8) (add-to-list 'flycheck-disabled-checkers 'python-pylint)

;; black and autopep8 enabled by above; see M-x elpy-config
(require 'py-autopep8) (add-hook 'elpy-mode-hook 'py-autopep8-enable-on-save)

;; Provides the py-autopep8 command, which uses the external "autopep8"
;; tool to tidy up the current buffer according to Python's PEP8.
;; To automatically apply when saving a python file, use the following code:
(add-hook 'python-mode-hook 'py-autopep8-enable-on-save)

;; To customize the behaviour of "autopep8" you can set the py-autopep8-options e.g.
(setq py-autopep8-options '("--max-line-length=100")) ;(setq py-autopep8-options '("--max-line-length=79"))

;; inline images for jupyter
(iimage-mode) (setq org-startup-with-inline-images t)

;; gud pdb (debugger)
(setq gud-pdb-command-name "python -m pdb ") (define-key elpy-mode-map [(control down)] "\C-u1\C-v") (define-key elpy-mode-map [(control up)] "\C-u-1\C-v")

galaunay commented 4 years ago

Thanks for reporting,

I wasn't able to reproduce this, but Elpy should not, in any circumstances, make Emacs crash. Do you have any additional information on why Emacs is crashing (log, trace) ?