Skip to content

Commit

Permalink
Misc lint things
Browse files Browse the repository at this point in the history
  • Loading branch information
rocky committed Jul 17, 2022
1 parent b4b6e30 commit c831f71
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 21 deletions.
10 changes: 10 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
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
hooks:
- id: black
language_version: python3
exclude: 'mathicsscript/version.py'
- repo: https://github.com/pycqa/flake8
rev: 3.9.2
hooks:
- id: flake8
stages: [commit]
2 changes: 1 addition & 1 deletion mathicsscript/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
2 changes: 1 addition & 1 deletion mathicsscript/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions mathicsscript/asymptote.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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():
Expand Down
11 changes: 6 additions & 5 deletions mathicsscript/completion.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2021 Rocky Bernstein <[email protected]>

# Copyright (C) 2021-2022 Rocky Bernstein <[email protected]>
# 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
Expand All @@ -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
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
8 changes: 4 additions & 4 deletions mathicsscript/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@

try:
from cairosvg import svg2png
except: # noqa
except ImportError:
svg2png = None

from mathicsscript.asymptote import Asy

have_asymptote = False
try:
asymptote_graph = Asy(show_help=False)
except:
except Exception:
asymptote_graph = None
else:
have_asymptote = True
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions mathicsscript/termshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -82,7 +84,7 @@ class TerminalShellCommon(MathicsLineFeeder):
def __init__(
self,
definitions,
style: str,
style: Optional[str],
want_completion: bool,
use_unicode: bool,
prompt: bool,
Expand Down
8 changes: 5 additions & 3 deletions mathicsscript/termshell_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -65,7 +66,7 @@ class TerminalShellPromptToolKit(TerminalShellCommon):
def __init__(
self,
definitions,
style: str,
style: Optional[str],
want_completion: bool,
use_unicode: bool,
prompt: bool,
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit c831f71

Please sign in to comment.