Skip to content

Commit

Permalink
Make mypy run cleanly
Browse files Browse the repository at this point in the history
  • Loading branch information
kraigher committed Dec 1, 2019
1 parent 35ea1b1 commit 2f8fd27
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 49 deletions.
3 changes: 3 additions & 0 deletions docs/contributing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ following are also required for developers to run the test suite manually:
`pylint <https://pypi.python.org/pypi/pylint>`__
Code analysis.

`mypy <http://www.mypy-lang.org/>`__
Optional static typing for Python.

Code coverage
~~~~~~~~~~~~~

Expand Down
24 changes: 24 additions & 0 deletions tests/lint/test_mypy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright (c) 2014-2019, Lars Asplund lars.anders.asplund@gmail.com

"""
MyPy check
"""

import unittest
import sys
from subprocess import check_call
from vunit import ROOT


class TestMyPy(unittest.TestCase):
"""
Run MyPy static type analysis
"""

@staticmethod
def test_pycodestyle():
check_call([sys.executable, "-m", "mypy", "vunit"])
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ deps=
pytest
lint: pycodestyle
lint: pylint
lint: mypy
docs: docutils
docs: sphinx
docs: sphinx-argparse
Expand Down
72 changes: 46 additions & 26 deletions vunit/color_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
from vunit.ostools import IS_WINDOWS_SYSTEM


class LinuxColorPrinter(object):
class ColorPrinter(object):
"""
Print in color on linux
Base class
"""

BLUE = "b"
Expand All @@ -29,6 +29,45 @@ class LinuxColorPrinter(object):
def __init__(self):
pass

@staticmethod
def write(
text, output_file=None, fg=None, bg=None
): # pylint: disable=unused-argument
"""
Print the text in color to the output_file
uses stdout if output_file is None
"""


class NoColorPrinter(ColorPrinter):
"""
Dummy printer that does not print in color
"""

def __init__(self):
ColorPrinter.__init__(self)

@staticmethod
def write(
text, output_file=None, fg=None, bg=None
): # pylint: disable=unused-argument
"""
Print the text in color to the output_file
uses stdout if output_file is None
"""
if output_file is None:
output_file = sys.stdout
output_file.write(text)


class LinuxColorPrinter(ColorPrinter):
"""
Print in color on linux
"""

def __init__(self):
ColorPrinter.__init__(self)

def write(self, text, output_file=None, fg=None, bg=None):
"""
Print the text in color to the output_file
Expand Down Expand Up @@ -110,13 +149,13 @@ class ConsoleScreenBufferInfo(Structure):
]


class Win32ColorPrinter(LinuxColorPrinter):
class Win32ColorPrinter(ColorPrinter):
"""
Prints in color on windows
"""

def __init__(self):
LinuxColorPrinter.__init__(self)
ColorPrinter.__init__(self)
self._stdout_handle = ctypes.windll.kernel32.GetStdHandle(-11)
self._stderr_handle = ctypes.windll.kernel32.GetStdHandle(-12)
self._default_attr = self._get_text_attr(self._stdout_handle)
Expand Down Expand Up @@ -186,32 +225,13 @@ def _decode_color(self, color_str):
return code


class NoColorPrinter(object):
"""
Dummy printer that does not print in color
"""

def __init__(self):
pass

@staticmethod
def write(
text, output_file=None, fg=None, bg=None
): # pylint: disable=unused-argument
"""
Print the text in color to the output_file
uses stdout if output_file is None
"""
if output_file is None:
output_file = sys.stdout
output_file.write(text)


NO_COLOR_PRINTER = NoColorPrinter()
NO_COLOR_PRINTER: ColorPrinter = NoColorPrinter()

# On MSYS/MINGW shells (https://www.msys2.org/) with Python installed through pacman, IS_WINDOWS_SYSTEM is true.
# However, regular Linux color strings are supported/required, instead of 'native' windows color format.
# Environment variable MSYSTEM is checked, which should contain 'msys'|'mingw32'|'mingw64' or be unset/empty.
COLOR_PRINTER: ColorPrinter

if IS_WINDOWS_SYSTEM and ("MSYSTEM" not in os.environ):
COLOR_PRINTER = Win32ColorPrinter()
else:
Expand Down
6 changes: 5 additions & 1 deletion vunit/parsing/tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ def Token(kind, value="", location=None): # pylint: disable=invalid-name
return TokenType(kind, value, location)


def new_token_kind(name):
class TokenKind:
pass


def new_token_kind(name: str) -> TokenKind:
"""
Create a new token kind with nice __repr__
"""
Expand Down
5 changes: 3 additions & 2 deletions vunit/parsing/verilog/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
Verilog tokens
"""

from vunit.parsing.tokenizer import new_token_kind
from typing import Dict
from vunit.parsing.tokenizer import new_token_kind, TokenKind


def __token(kind):
Expand All @@ -27,7 +28,7 @@ def __keyword(kind):
return token_kind


KEYWORDS = {}
KEYWORDS: Dict[str, TokenKind] = {}

PREPROCESSOR = __token("preprocessor")
STRING = __token("string")
Expand Down
39 changes: 20 additions & 19 deletions vunit/sim_if/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,38 @@
import sys
import os
import subprocess
from typing import List
from ..ostools import Process, simplify_path
from ..exceptions import CompileError
from ..color_printer import NO_COLOR_PRINTER


class Option(object):
"""
A compile or sim option
"""

def __init__(self, name):
self._name = name

@property
def name(self):
return self._name

def validate(self, value):
pass


class SimulatorInterface(object): # pylint: disable=too-many-public-methods
"""
Generic simulator interface
"""

name = None
name: str = "none"
supports_gui_flag = False
package_users_depend_on_bodies = False
compile_options = []
sim_options = []
compile_options: List[Option] = []
sim_options: List[Option] = []

# True if simulator supports ANSI colors in GUI mode
supports_colors_in_gui = False
Expand Down Expand Up @@ -347,22 +364,6 @@ def check_output(command, env=None):
return output.decode("utf-8")


class Option(object):
"""
A compile or sim option
"""

def __init__(self, name):
self._name = name

@property
def name(self):
return self._name

def validate(self, value):
pass


class BooleanOption(Option):
"""
Must be a boolean
Expand Down
4 changes: 3 additions & 1 deletion vunit/test/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,9 @@ def wrap(file_obj, use_color=True):
NOTE:
imports colorama here to avoid dependency from setup.py importing VUnit before colorama is installed
"""
from colorama import AnsiToWin32 # pylint: disable=import-outside-toplevel
from colorama import ( # type: ignore # pylint: disable=import-outside-toplevel
AnsiToWin32,
)

if use_color:
return AnsiToWin32(file_obj).stream
Expand Down

0 comments on commit 2f8fd27

Please sign in to comment.