From: Michael R. Crusoe Subject: make mypy truly optional for running the tests Forwarded: https://github.com/Delgan/loguru/pull/1465 --- loguru.orig/tests/conftest.py +++ loguru/tests/conftest.py @@ -48,35 +48,38 @@ if sys.version_info >= (3, 6): - from pytest_mypy_plugins.item import YamlTestItem + try: + from pytest_mypy_plugins.item import YamlTestItem - def _fix_positional_only_args(item: YamlTestItem): - """Remove forward-slash marker from the expected output for Python 3.6.""" - for output in item.expected_output: - output.message = output.message.replace(", /", "") - # Also patch the "severity" attribute because there is a parsing bug in the plugin. - output.severity = output.severity.replace(", /", "") - - def _add_mypy_config(item: YamlTestItem): - """Add some extra options to the mypy configuration for Python 3.7+.""" - item.additional_mypy_config += "\n".join( - [ - "show_error_codes = false", - "force_uppercase_builtins = true", - "force_union_syntax = true", - ] - ) - - def pytest_collection_modifyitems(config, items): - """Modify the tests to ensure they produce the same output regardless of Python version.""" - for item in items: - if not isinstance(item, YamlTestItem): - continue - - if sys.version_info >= (3, 7): - _add_mypy_config(item) - else: - _fix_positional_only_args(item) + def _fix_positional_only_args(item: "YamlTestItem"): + """Remove forward-slash marker from the expected output for Python 3.6.""" + for output in item.expected_output: + output.message = output.message.replace(", /", "") + # Also patch the "severity" attribute because there is a parsing bug in the plugin. + output.severity = output.severity.replace(", /", "") + + def _add_mypy_config(item: "YamlTestItem"): + """Add some extra options to the mypy configuration for Python 3.7+.""" + item.additional_mypy_config += "\n".join( + [ + "show_error_codes = false", + "force_uppercase_builtins = true", + "force_union_syntax = true", + ] + ) + + def pytest_collection_modifyitems(config, items): + """Modify the tests to ensure they produce the same output regardless of Python version.""" + for item in items: + if not isinstance(item, "YamlTestItem"): + continue + + if sys.version_info >= (3, 7): + _add_mypy_config(item) + else: + _fix_positional_only_args(item) + except ImportError: + pass @contextlib.contextmanager --- loguru.orig/tests/test_type_hinting.py +++ loguru/tests/test_type_hinting.py @@ -1,11 +1,17 @@ import sys -import mypy.api +import pytest +try: + import mypy.api as mypy_api +except ImportError: + mypy_api = None + +@pytest.mark.skipif(mypy_api is None, reason="Requires mypy to be installed.") def test_mypy_import(): # Check stub file is valid and can be imported by Mypy. # There exist others tests in "typesafety" subfolder but they aren't compatible with Python 3.5. - out, _, result = mypy.api.run(["--strict", "-c", "from loguru import logger"]) + out, _, result = mypy_api.run(["--strict", "-c", "from loguru import logger"]) print("".join(out), file=sys.stderr) assert result == 0