From 42a047d1cd969ee438d85f0cca49bb01a2bee647 Mon Sep 17 00:00:00 2001 From: Alexander Goscinski Date: Wed, 3 Jul 2024 09:40:11 +0200 Subject: [PATCH] fix linter, pin mypy version exist code 2, usage has changed --- tests/conftest.py | 6 +- tests/notebooks/widget_answers.py | 1 - tests/notebooks/widget_check_registry.py | 2 - tests/notebooks/widget_code_exercise.py | 1 - tests/notebooks/widget_cue_figure-inline.py | 4 -- tests/notebooks/widget_cue_figure-ipympl.py | 17 ----- tests/notebooks/widgets_cue.py | 4 -- tests/test_widgets.py | 77 ++++++++++++++------- tox.ini | 2 +- 9 files changed, 54 insertions(+), 60 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 776471b..acd5387 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,9 +2,9 @@ import subprocess import time from urllib.parse import urljoin -from packaging.version import Version import pytest +from packaging.version import Version from selenium.common.exceptions import StaleElementReferenceException from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions @@ -115,9 +115,7 @@ def _selenium_driver(nb_path): "Restart Kernel and Run All Cells…" ) else: - restart_kernel_button_class_name = ( - "jp-ToolbarButtonComponent" - ) + restart_kernel_button_class_name = "jp-ToolbarButtonComponent" restart_kernel_button_title_attribute = ( "Restart the kernel and run all cells" ) diff --git a/tests/notebooks/widget_answers.py b/tests/notebooks/widget_answers.py index 8293aef..2180c5a 100644 --- a/tests/notebooks/widget_answers.py +++ b/tests/notebooks/widget_answers.py @@ -21,7 +21,6 @@ from scwidgets.exercise import CodeExercise, ExerciseRegistry, TextExercise sys.path.insert(0, os.path.abspath("../..")) -print() # - diff --git a/tests/notebooks/widget_check_registry.py b/tests/notebooks/widget_check_registry.py index d67743c..0ff65cd 100644 --- a/tests/notebooks/widget_check_registry.py +++ b/tests/notebooks/widget_check_registry.py @@ -23,7 +23,6 @@ sys.path.insert(0, os.path.abspath("../..")) from tests.test_check import mock_checkable_widget # noqa: E402 from tests.test_check import single_param_check # noqa: E402 -print() # - @@ -38,7 +37,6 @@ def create_check_registry(use_fingerprint, failing, buggy): ) _ = mock_checkable_widget(check_registry, check.function_to_check, [check]) return check_registry -print() # Test 1: diff --git a/tests/notebooks/widget_code_exercise.py b/tests/notebooks/widget_code_exercise.py index aa6036b..a19242f 100644 --- a/tests/notebooks/widget_code_exercise.py +++ b/tests/notebooks/widget_code_exercise.py @@ -22,7 +22,6 @@ sys.path.insert(0, os.path.abspath("../..")) from tests.test_check import single_param_check # noqa: E402 from tests.test_code import get_code_exercise # noqa: E402 -print() # - diff --git a/tests/notebooks/widget_cue_figure-inline.py b/tests/notebooks/widget_cue_figure-inline.py index 94dc86a..6c9859d 100644 --- a/tests/notebooks/widget_cue_figure-inline.py +++ b/tests/notebooks/widget_cue_figure-inline.py @@ -14,20 +14,16 @@ import matplotlib import matplotlib.pyplot as plt -print() from scwidgets.cue import CueFigure -print() def run_cue_figure(): fig = plt.figure() return CueFigure(fig) -print() matplotlib.use("module://matplotlib_inline.backend_inline") -print() # #### Test 1.1 diff --git a/tests/notebooks/widget_cue_figure-ipympl.py b/tests/notebooks/widget_cue_figure-ipympl.py index 00a572d..0b1b08a 100644 --- a/tests/notebooks/widget_cue_figure-ipympl.py +++ b/tests/notebooks/widget_cue_figure-ipympl.py @@ -14,10 +14,8 @@ import matplotlib import matplotlib.pyplot as plt -print() from scwidgets.cue import CueFigure -print() def run_cue_figure(): @@ -27,11 +25,9 @@ def run_cue_figure(): fig.canvas.toolbar_visible = False fig.canvas.resizable = False return CueFigure(fig) -print() matplotlib.use("module://ipympl.backend_nbagg") -print() # #### Test 1.1 @@ -42,12 +38,10 @@ def run_cue_figure(): # just checks if no error is raised when no axes is present cf.clear_figure() -print() # Tests if plots are properly closed inside the CueFigure # so the output in this celll should be empty plt.show() -print() # #### Test 1.2 # Tests if axis is drawn on figure @@ -60,16 +54,13 @@ def run_cue_figure(): # axes should be retained using clear_figure cf.clear_figure() cf.draw_display() -print() # Tests if plots are properly closed inside the CueFigure # so the output in this celll should be empty plt.show() -print() # + # cf.figure.savefig("../screenshots/widget_cue_figure/empty_axis.png") -print() # - # #### Test 1.3 @@ -81,17 +72,14 @@ def run_cue_figure(): ax = cf.figure.gca() ax.plot([0, 0.5], [0, 0.5]) cf.draw_display() -print() # Tests if plots are properly closed plt.show() -print() # + # screenshot for references # cf.figure.savefig("../screenshots/widget_cue_figure/update_figure_plot.png") -print() # - # #### Test 1.4 @@ -113,17 +101,14 @@ def update_figure(cf, x, y): cf.clear_display() update_figure(cf, [0, 0.5], [0, 0.5]) cf.draw_display() -print() # Tests if plots are properly closed plt.show() -print() # + # screenshot for references # cf.figure.savefig("../screenshots/widget_cue_figure/update_figure_set.png") -print() # - # #### Test 1.5 @@ -142,8 +127,6 @@ def update_figure(cf, x, y): cf.clear_display() update_figure(cf, [0, 0.5], [0, 0.5]) cf.draw_display() -print() # Tests if plots are properly closed plt.show() -print() diff --git a/tests/notebooks/widgets_cue.py b/tests/notebooks/widgets_cue.py index 073c6e2..f0e7e18 100644 --- a/tests/notebooks/widgets_cue.py +++ b/tests/notebooks/widgets_cue.py @@ -15,11 +15,9 @@ # + from ipywidgets import Text, VBox -print() import scwidgets from scwidgets.cue import CheckCueBox, CueBox, ResetCueButton, SaveCueBox, UpdateCueBox -print() scwidgets.get_css_style() # - @@ -33,7 +31,6 @@ def create_cue_box(CueBoxClass, cued): text_input = Text("Text") cued_text_input = CueBoxClass(text_input, cued=cued) return cued_text_input -print() # Test 1.1 @@ -82,7 +79,6 @@ def action(): reset_cue_button.set_cue_widgets([cue_unused_text_input]) reset_cue_button.set_cue_widgets([cue_text_input, cue_reset_cue_button]) return VBox([cue_text_input, cue_unused_text_input, cue_reset_cue_button]) -print() # Test 2.1 diff --git a/tests/test_widgets.py b/tests/test_widgets.py index 6b33918..620b633 100644 --- a/tests/test_widgets.py +++ b/tests/test_widgets.py @@ -3,15 +3,15 @@ import os import time from typing import List -from packaging.version import Version import numpy as np import pytest import requests from imageio.v3 import imread +from packaging.version import Version +from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys -from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.remote.webelement import WebElement from selenium.webdriver.support import expected_conditions from selenium.webdriver.support.wait import WebDriverWait @@ -20,6 +20,7 @@ from .conftest import JUPYTER_TYPE + def crop_const_color_borders(image: np.ndarray, const_color: int = 255): """ Removes all constant color borders of the image @@ -77,6 +78,7 @@ def scwidget_reset_cue_button_class_name(cue_type: str, cued: bool): return class_name return class_name.replace("reset-cue-button", f"{cue_type}-reset-cue-button") + class NbCells(list): def __getitem__(self, key): @@ -84,16 +86,19 @@ def __getitem__(self, key): time.sleep(0.1) ActionChains(self._driver).send_keys(Keys.HOME).perform() for i in range(key): - self._driver.execute_script('arguments[0].scrollIntoView();', super().__getitem__(i)) + self._driver.execute_script( + "arguments[0].scrollIntoView();", super().__getitem__(i) + ) nb_cell = super().__getitem__(key) - self._driver.execute_script('arguments[0].scrollIntoView();', nb_cell) + self._driver.execute_script("arguments[0].scrollIntoView();", nb_cell) time.sleep(0.1) return nb_cell def set_driver(self, driver): self._driver = driver + def get_nb_cells(driver) -> List[WebElement]: """ Filters out empty cells @@ -101,32 +106,35 @@ def get_nb_cells(driver) -> List[WebElement]: :param driver: see conftest.py selenium_driver function """ from .conftest import JUPYTER_VERSION + # Each cell of the notebook, the cell number can be retrieved from the # attribute "data-windowed-list-index" - if JUPYTER_TYPE == 'lab' and JUPYTER_VERSION < Version("4.0.0"): + if JUPYTER_TYPE == "lab" and JUPYTER_VERSION < Version("4.0.0"): nb_cells = driver.find_elements( By.CLASS_NAME, "lm-Widget.jp-Cell.jp-CodeCell.jp-Notebook-cell" ) - return [nb_cell for nb_cell in nb_cells if nb_cell.text != ''] - elif JUPYTER_TYPE == 'lab' and JUPYTER_VERSION >= Version("4.0.0"): + return [nb_cell for nb_cell in nb_cells if nb_cell.text != ""] + elif JUPYTER_TYPE == "lab" and JUPYTER_VERSION >= Version("4.0.0"): nb_cells = driver.find_elements( By.CLASS_NAME, "lm-Widget.jp-Cell.jp-CodeCell.jp-Notebook-cell" ) nb_cells_nonempty = [] for nb_cell in nb_cells: - driver.execute_script('arguments[0].scrollIntoView();', nb_cell) - if nb_cell.text != '': + driver.execute_script("arguments[0].scrollIntoView();", nb_cell) + if nb_cell.text != "": nb_cells_nonempty.append(nb_cell) # move back to start of the page ActionChains(driver).send_keys(Keys.HOME).perform() - #By.CLASS_NAME, "lm-Widget.jp-Cell.jp-CodeCell.jp-Notebook-cell.jp-mod-noOutputs" nb_cells_list = NbCells(nb_cells_nonempty) nb_cells_list.set_driver(driver) return nb_cells_list else: - raise ValueError('Jupyter type {JUPYTER_TYPE!r} for version {JUPYTER_VERSION!r} is not supported.') + raise ValueError( + "Jupyter type {JUPYTER_TYPE!r} for version {JUPYTER_VERSION!r} " + "is not supported." + ) ######### @@ -154,17 +162,29 @@ def test_notebook_running(notebook_service): # status code 200 means it was successful assert response.status_code == 200 + def test_setup_globals(): from .conftest import JUPYTER_VERSION - global BUTTON_CLASS_NAME, OUTPUT_CLASS_NAME, TEXT_INPUT_CLASS_NAME, CODE_MIRROR_CLASS_NAME, MATPLOTLIB_CANVAS_CLASS_NAME, CUE_BOX_CLASS_NAME, PRIVACY_BUTTON - global CUED_CUE_BOX_CLASS_NAME, RESET_CUE_BUTTON_CLASS_NAME, CUED_RESET_CUE_BUTTON_CLASS_NAME + # blacks format this into one line which causes an error in the linter. + # fmt: off + global BUTTON_CLASS_NAME, OUTPUT_CLASS_NAME, TEXT_INPUT_CLASS_NAME, \ + CODE_MIRROR_CLASS_NAME, MATPLOTLIB_CANVAS_CLASS_NAME, CUE_BOX_CLASS_NAME, \ + PRIVACY_BUTTON + global CUED_CUE_BOX_CLASS_NAME, RESET_CUE_BUTTON_CLASS_NAME, \ + CUED_RESET_CUE_BUTTON_CLASS_NAME + # fmt: on + if JUPYTER_TYPE == "notebook" and JUPYTER_VERSION >= Version("7.0.0"): BUTTON_CLASS_NAME = "lm-Widget.jupyter-widgets.jupyter-button.widget-button" - OUTPUT_CLASS_NAME = "lm-Widget.jp-RenderedText.jp-mod-trusted.jp-OutputArea-output" + OUTPUT_CLASS_NAME = ( + "lm-Widget.jp-RenderedText.jp-mod-trusted.jp-OutputArea-output" + ) TEXT_INPUT_CLASS_NAME = "widget-input" CODE_MIRROR_CLASS_NAME = "cm-content" - MATPLOTLIB_CANVAS_CLASS_NAME = "jupyter-widgets.jupyter-matplotlib-canvas-container" + MATPLOTLIB_CANVAS_CLASS_NAME = ( + "jupyter-widgets.jupyter-matplotlib-canvas-container" + ) CUE_BOX_CLASS_NAME = ( "lm-Widget.lm-Panel.jupyter-widgets.widget-container" ".widget-box.widget-vbox.scwidget-cue-box" @@ -180,24 +200,29 @@ def test_setup_globals(): TEXT_INPUT_CLASS_NAME = "widget-input" CODE_MIRROR_CLASS_NAME = "cm-content" - MATPLOTLIB_CANVAS_CLASS_NAME = "jupyter-widgets.jupyter-matplotlib-canvas-container" + MATPLOTLIB_CANVAS_CLASS_NAME = ( + "jupyter-widgets.jupyter-matplotlib-canvas-container" + ) CUE_BOX_CLASS_NAME = ( "lm-Widget.p-Widget.lm-Panel.p-Panel.jupyter-widgets." "widget-container.widget-box.widget-vbox.scwidget-cue-box" ) PRIVACY_BUTTON = "bp3-button.bp3-small.jp-toast-button.jp-Button" elif JUPYTER_TYPE == "lab" and JUPYTER_VERSION >= Version("4.0.0"): - BUTTON_CLASS_NAME = ( - "lm-Widget.jupyter-widgets.jupyter-button.widget-button" + BUTTON_CLASS_NAME = "lm-Widget.jupyter-widgets.jupyter-button.widget-button" + OUTPUT_CLASS_NAME = ( + "lm-Widget.jp-RenderedText.jp-mod-trusted.jp-OutputArea-output" ) - OUTPUT_CLASS_NAME = "lm-Widget.jp-RenderedText.jp-mod-trusted.jp-OutputArea-output" TEXT_INPUT_CLASS_NAME = "widget-input" CODE_MIRROR_CLASS_NAME = "cm-content" - MATPLOTLIB_CANVAS_CLASS_NAME = "jupyter-widgets.jupyter-matplotlib-canvas-container" + MATPLOTLIB_CANVAS_CLASS_NAME = ( + "jupyter-widgets.jupyter-matplotlib-canvas-container" + ) CUE_BOX_CLASS_NAME = ( - "lm-Widget.lm-Panel.jupyter-widgets.widget-container.widget-box.widget-vbox.scwidget-cue-box" + "lm-Widget.lm-Panel.jupyter-widgets.widget-container." + "widget-box.widget-vbox.scwidget-cue-box" ) PRIVACY_BUTTON = "jp-toast-button.jp-mod-small.jp-Button" else: @@ -213,6 +238,7 @@ def test_setup_globals(): f"{RESET_CUE_BUTTON_CLASS_NAME}.scwidget-reset-cue-button--cue" ) + def test_privacy_policy(selenium_driver): """ The first time jupyter lab is started on a fresh installation a privacy popup @@ -224,9 +250,7 @@ def test_privacy_policy(selenium_driver): driver = selenium_driver("tests/notebooks/widget_answers.ipynb") # we search for the button to appear so we can be sure that the privacy window # appeared - privacy_buttons = driver.find_elements( - By.CLASS_NAME, PRIVACY_BUTTON - ) + privacy_buttons = driver.find_elements(By.CLASS_NAME, PRIVACY_BUTTON) yes_button = None for button in privacy_buttons: if button.text == "Yes": @@ -577,6 +601,7 @@ def test_widget_figure(selenium_driver, nb_filename, mpl_backend): :param selenium_driver: see conftest.py """ from .conftest import JUPYTER_VERSION + if JUPYTER_VERSION >= Version("4.0.0"): return # TODO for inline i need to get the image directly from the panel @@ -959,9 +984,9 @@ def test_button_clicks( check_all_widgets_button = buttons[1] assert check_all_widgets_button.get_property("title") == "Check all widgets" - #WebDriverWait(driver, 5).until( + # WebDriverWait(driver, 5).until( # expected_conditions.element_to_be_clickable(check_all_widgets_button) - #) + # ) time.sleep(0.1) if JUPYTER_TYPE == "lab" and JUPYTER_VERSION >= Version("4.0.0"): # button is obscured so we need to click with action on the cell diff --git a/tox.ini b/tox.ini index b6d97c9..c187dba 100644 --- a/tox.ini +++ b/tox.ini @@ -130,7 +130,7 @@ deps = black blackdoc isort - mypy + mypy==1.8.0 commands = flake8 {[tox]lint_folders}