Skip to content

Commit

Permalink
Merge pull request #601 from VUnit/mypy
Browse files Browse the repository at this point in the history
Mypy
  • Loading branch information
kraigher authored Dec 1, 2019
2 parents b4c0d75 + c3f60e0 commit 7fd2def
Show file tree
Hide file tree
Showing 21 changed files with 407 additions and 300 deletions.
4 changes: 2 additions & 2 deletions .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ environment:
# For Python versions available on Appveyor, see
# https://www.appveyor.com/docs/windows-images-software/#python

- BUILD_NAME: py35-unit
PYTHON: "C:\\Python35"
- BUILD_NAME: py36-unit
PYTHON: "C:\\Python36"

- BUILD_NAME: py38-unit
PYTHON: "C:\\Python38"
Expand Down
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ matrix:
python: '3.8'


- env: BUILD_NAME=py35-unit
python: '3.5'
- env: BUILD_NAME=py36-unit
python: '3.6'
- env: BUILD_NAME=py38-unit
dist: xenial
python: '3.8'
Expand Down
2 changes: 1 addition & 1 deletion docs/about.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Operating systems
Python
******

- Python 3.5 or higher
- Python 3.6 or higher

Simulators
**********
Expand Down
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
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ def find_all_files(directory, endings=None):
"License :: OSI Approved :: Mozilla Public License 2.0 (MPL 2.0)",
"Natural Language :: English",
"Intended Audience :: Developers",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
Expand All @@ -71,7 +70,7 @@ def find_all_files(directory, endings=None):
"Topic :: Software Development :: Testing",
"Topic :: Scientific/Engineering :: Electronic Design Automation (EDA)",
],
python_requires=">=3.5",
python_requires=">=3.6",
install_requires=["colorama"],
requires=["colorama"],
license=["Mozilla Public License 2.0 (MPL 2.0)"],
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"])
4 changes: 2 additions & 2 deletions tests/unit/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ def test_error_on_duplicate_file(self, logger):
self.assertEqual(self.project.get_source_files_in_order(), [file1])
self.assertFalse(logger.warning.called)

@mock.patch("vunit.project.LOGGER")
@mock.patch("vunit.library.LOGGER")
def test_no_error_on_duplicate_identical_file(self, logger):
self.project.add_library("lib", "lib_path")
file1 = self.add_source_file("lib", "file.vhd", "entity foo is end entity;")
Expand All @@ -757,7 +757,7 @@ def _test_warning_on_duplicate(self, code, message, verilog=False):

self.add_source_file("lib", "file." + suffix, code)

with mock.patch("vunit.project.LOGGER") as mock_logger:
with mock.patch("vunit.library.LOGGER") as mock_logger:
self.add_source_file("lib", "file_copy." + suffix, code)
warning_calls = mock_logger.warning.call_args_list
log_msg = warning_calls[0][0][0] % warning_calls[0][0][1:]
Expand Down
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist = py{36,37,38}-fmt, py{35,36,37,38}-{unit}, py{35,36,37,38}-{lint,docs}, py{35,36,37,38}-{acceptance,vcomponents}-{activehdl,ghdl,modelsim,rivierapro}
envlist = py{36,37,38}-fmt, py{36,37,38}-{unit}, py{36,37,38}-{lint,docs}, py{36,37,38}-{acceptance,vcomponents}-{activehdl,ghdl,modelsim,rivierapro}
skip_missing_interpreters = True

[testenv]
Expand All @@ -11,6 +11,7 @@ deps=
pytest
lint: pycodestyle
lint: pylint
lint: mypy
docs: docutils
docs: sphinx
docs: sphinx-argparse
Expand Down
4 changes: 2 additions & 2 deletions vunit/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from os.path import join, abspath, dirname, basename
from glob import glob
from warnings import warn
from vunit.vhdl_standard import VHDL
from vunit.vhdl_standard import VHDL, VHDLStandard
from vunit.sim_if.common import simulator_check

VHDL_PATH = abspath(join(dirname(__file__), "vhdl"))
Expand All @@ -23,7 +23,7 @@ class Builtins(object):
Manage VUnit builtins and their dependencies
"""

def __init__(self, vunit_obj, vhdl_standard, simulator_class):
def __init__(self, vunit_obj, vhdl_standard: VHDLStandard, simulator_class):
self._vunit_obj = vunit_obj
self._vunit_lib = vunit_obj.add_library("vunit_lib")
self._vhdl_standard = vhdl_standard
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
Loading

0 comments on commit 7fd2def

Please sign in to comment.