From c831f718271f9197cd8c679486874f947571cbf8 Mon Sep 17 00:00:00 2001 From: rocky Date: Sun, 17 Jul 2022 14:11:37 -0400 Subject: [PATCH] Misc lint things --- .pre-commit-config.yaml | 10 ++++++++++ mathicsscript/__init__.py | 2 +- mathicsscript/__main__.py | 2 +- mathicsscript/asymptote.py | 10 +++++----- mathicsscript/completion.py | 11 ++++++----- mathicsscript/format.py | 8 ++++---- mathicsscript/termshell.py | 6 ++++-- mathicsscript/termshell_prompt.py | 8 +++++--- 8 files changed, 36 insertions(+), 21 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 3f695b1..be3ee5b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,9 +1,14 @@ +default_language_version: + python: python repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: v4.0.1 hooks: + - id: check-merge-conflict - id: debug-statements + stages: [commit] - id: end-of-file-fixer + stages: [commit] - id: trailing-whitespace - repo: https://github.com/psf/black rev: 22.3.0 @@ -11,3 +16,8 @@ repos: - id: black language_version: python3 exclude: 'mathicsscript/version.py' +- repo: https://github.com/pycqa/flake8 + rev: 3.9.2 + hooks: + - id: flake8 + stages: [commit] diff --git a/mathicsscript/__init__.py b/mathicsscript/__init__.py index f6813b1..d12f2c9 100644 --- a/mathicsscript/__init__.py +++ b/mathicsscript/__init__.py @@ -16,4 +16,4 @@ def load_default_settings_files(definitions): autoload_files(definitions, root_dir, "autoload") -__all__ = [__version__, load_default_settings_files] +__all__ = ["__version__", "load_default_settings_files"] diff --git a/mathicsscript/__main__.py b/mathicsscript/__main__.py index b63b244..4bf37dc 100755 --- a/mathicsscript/__main__.py +++ b/mathicsscript/__main__.py @@ -436,7 +436,7 @@ def main( if len(source_code) and source_code[1] == "!": try: print(open(source_code[2:], "r").read()) - except: + except Exception: print(str(sys.exc_info()[1])) else: subprocess.run(source_code[1:], shell=True) diff --git a/mathicsscript/asymptote.py b/mathicsscript/asymptote.py index 240ce68..a19c97c 100644 --- a/mathicsscript/asymptote.py +++ b/mathicsscript/asymptote.py @@ -8,6 +8,7 @@ import os.path as osp from subprocess import Popen, PIPE, run +from typing import Optional asy_program = os.environ.get("ASY_PROG", "asy") @@ -18,13 +19,12 @@ os.environ["ASYMPTOTE_DIR"] = with_asymptote_dir result = run([asy_program, "--version"], timeout=0.5, capture_output=True) +asymptote_version: Optional[str] = None if result.returncode == 0: # Use the first line of output only, not all of the enabled options - asymptote_version = result.stderr.decode("utf-8").split("\n")[0] - # Just the name and version, not the copyright and authors - asymptote_version = asymptote_version.split("[")[0].strip() -else: - asymptote_version = None + asymptote_version = result.stderr.decode("utf-8").split("\n")[0] + # Just the name and version, not the copyright and authors + asymptote_version = asymptote_version.split("[")[0].strip() def get_srcdir(): diff --git a/mathicsscript/completion.py b/mathicsscript/completion.py index fae18bd..d6513c1 100644 --- a/mathicsscript/completion.py +++ b/mathicsscript/completion.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright (C) 2021 Rocky Bernstein + +# Copyright (C) 2021-2022 Rocky Bernstein # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or @@ -17,7 +18,7 @@ import os.path as osp import re -from typing import Iterable, NamedTuple +from typing import Iterable, List, NamedTuple, Tuple from mathics.core.symbols import strip_context from mathics_scanner import named_characters @@ -82,7 +83,7 @@ def __init__(self, definitions): self.escape_sequences = aliased_characters.keys() - def _is_space_before_cursor(self, document, text_before_cursor: bool) -> bool: + def _is_space_before_cursor(self, document, text_before_cursor: str) -> bool: """Space before or no text before cursor.""" return text_before_cursor == "" or text_before_cursor[-1:].isspace() @@ -121,7 +122,7 @@ def word_matches(word: str) -> bool: display_meta=display_meta, ) - def get_word_before_cursor_with_kind(self, document: Document) -> WordToken: + def get_word_before_cursor_with_kind(self, document: Document) -> Tuple[str, TokenKind]: """ Get the word before the cursor and clasify it into one of the kinds of tokens: NamedCharacter, AsciiOperator, Symbol, etc. @@ -164,7 +165,7 @@ def get_word_before_cursor_with_kind(self, document: Document) -> WordToken: return word_before_cursor, TokenKind.Symbol - def get_word_names(self: str): + def get_word_names(self) -> List[str]: names = self.definitions.get_names() short_names = [strip_context(m) for m in names] return list(names) + short_names diff --git a/mathicsscript/format.py b/mathicsscript/format.py index 0da32fe..dd85fdc 100644 --- a/mathicsscript/format.py +++ b/mathicsscript/format.py @@ -33,7 +33,7 @@ try: from cairosvg import svg2png -except: # noqa +except ImportError: svg2png = None from mathicsscript.asymptote import Asy @@ -41,7 +41,7 @@ have_asymptote = False try: asymptote_graph = Asy(show_help=False) -except: +except Exception: asymptote_graph = None else: have_asymptote = True @@ -81,7 +81,7 @@ def eval_boxes(result, fn, obj, **options): elif expr_type == "System`TeXForm": format = "tex" elements = expr.elements - if len(elemtns) == 1: + if len(elements) == 1: expr = elements[0] elif expr_type == "System`Image": if get_settings_value(obj.definitions, "Settings`$UseMatplotlib") and plt: @@ -95,7 +95,7 @@ def eval_boxes(result, fn, obj, **options): result = png_expr.evaluate(obj) plt.axes().set_axis_off() img = mpimg.imread(temp_png) - cmap="gray" if expr.color_space == "Grayscale" else None + cmap = "gray" if expr.color_space == "Grayscale" else None plt.imshow(img, cmap=cmap) plt.show() except: # noqa diff --git a/mathicsscript/termshell.py b/mathicsscript/termshell.py index b41cf50..93cca69 100644 --- a/mathicsscript/termshell.py +++ b/mathicsscript/termshell.py @@ -9,6 +9,8 @@ import pathlib import sys +from typing import Optional + from mathics_pygments.lexer import MathematicaLexer, MToken from mathics.core.atoms import String, Symbol @@ -49,7 +51,7 @@ os.makedirs(CONFIGDIR, exist_ok=True) try: - HISTSIZE = int(os.environ.get("MATHICSSCRIPT_HISTSIZE")) + HISTSIZE = int(os.environ.get("MATHICSSCRIPT_HISTSIZE", 50)) except: HISTSIZE = 50 @@ -82,7 +84,7 @@ class TerminalShellCommon(MathicsLineFeeder): def __init__( self, definitions, - style: str, + style: Optional[str], want_completion: bool, use_unicode: bool, prompt: bool, diff --git a/mathicsscript/termshell_prompt.py b/mathicsscript/termshell_prompt.py index 1ae7ee2..b015a97 100644 --- a/mathicsscript/termshell_prompt.py +++ b/mathicsscript/termshell_prompt.py @@ -6,6 +6,7 @@ import os.path as osp import re import sys +from typing import Optional from mathics_pygments.lexer import MathematicaLexer, MToken from mathicsscript.completion import MathicsCompleter @@ -22,7 +23,7 @@ from mathics.core.atoms import String from mathics.core.attributes import attribute_string_to_number -from mathics.core.expression import Expression, Symbol, from_python +from mathics.core.expression import Expression, from_python from mathics.core.rules import Rule from mathics.core.systemsymbols import SymbolMessageName @@ -65,7 +66,7 @@ class TerminalShellPromptToolKit(TerminalShellCommon): def __init__( self, definitions, - style: str, + style: Optional[str], want_completion: bool, use_unicode: bool, prompt: bool, @@ -196,7 +197,7 @@ def print_result( if last_eval is not None: try: eval_type = last_eval.get_head_name() - except: + except Exception: print(sys.exc_info()[1]) return @@ -243,6 +244,7 @@ def print_result( def read_line(self, prompt): # FIXME set and update inside self. + style = style_from_pygments_cls(get_style_by_name(self.pygments_style)) line = self.session.prompt(