| 1 |
From: Michael R. Crusoe <[email protected]> |
| 2 |
Subject: make mypy truly optional for running the tests |
| 3 |
Forwarded: https://github.com/Delgan/loguru/pull/1465 |
| 4 |
--- loguru.orig/tests/conftest.py |
| 5 |
+++ loguru/tests/conftest.py |
| 6 |
@@ -48,35 +48,38 @@ |
| 7 |
|
| 8 |
|
| 9 |
if sys.version_info >= (3, 6): |
| 10 |
- from pytest_mypy_plugins.item import YamlTestItem |
| 11 |
+ try: |
| 12 |
+ from pytest_mypy_plugins.item import YamlTestItem |
| 13 |
|
| 14 |
- def _fix_positional_only_args(item: YamlTestItem): |
| 15 |
- """Remove forward-slash marker from the expected output for Python 3.6.""" |
| 16 |
- for output in item.expected_output: |
| 17 |
- output.message = output.message.replace(", /", "") |
| 18 |
- # Also patch the "severity" attribute because there is a parsing bug in the plugin. |
| 19 |
- output.severity = output.severity.replace(", /", "") |
| 20 |
- |
| 21 |
- def _add_mypy_config(item: YamlTestItem): |
| 22 |
- """Add some extra options to the mypy configuration for Python 3.7+.""" |
| 23 |
- item.additional_mypy_config += "\n".join( |
| 24 |
- [ |
| 25 |
- "show_error_codes = false", |
| 26 |
- "force_uppercase_builtins = true", |
| 27 |
- "force_union_syntax = true", |
| 28 |
- ] |
| 29 |
- ) |
| 30 |
- |
| 31 |
- def pytest_collection_modifyitems(config, items): |
| 32 |
- """Modify the tests to ensure they produce the same output regardless of Python version.""" |
| 33 |
- for item in items: |
| 34 |
- if not isinstance(item, YamlTestItem): |
| 35 |
- continue |
| 36 |
- |
| 37 |
- if sys.version_info >= (3, 7): |
| 38 |
- _add_mypy_config(item) |
| 39 |
- else: |
| 40 |
- _fix_positional_only_args(item) |
| 41 |
+ def _fix_positional_only_args(item: "YamlTestItem"): |
| 42 |
+ """Remove forward-slash marker from the expected output for Python 3.6.""" |
| 43 |
+ for output in item.expected_output: |
| 44 |
+ output.message = output.message.replace(", /", "") |
| 45 |
+ # Also patch the "severity" attribute because there is a parsing bug in the plugin. |
| 46 |
+ output.severity = output.severity.replace(", /", "") |
| 47 |
+ |
| 48 |
+ def _add_mypy_config(item: "YamlTestItem"): |
| 49 |
+ """Add some extra options to the mypy configuration for Python 3.7+.""" |
| 50 |
+ item.additional_mypy_config += "\n".join( |
| 51 |
+ [ |
| 52 |
+ "show_error_codes = false", |
| 53 |
+ "force_uppercase_builtins = true", |
| 54 |
+ "force_union_syntax = true", |
| 55 |
+ ] |
| 56 |
+ ) |
| 57 |
+ |
| 58 |
+ def pytest_collection_modifyitems(config, items): |
| 59 |
+ """Modify the tests to ensure they produce the same output regardless of Python version.""" |
| 60 |
+ for item in items: |
| 61 |
+ if not isinstance(item, "YamlTestItem"): |
| 62 |
+ continue |
| 63 |
+ |
| 64 |
+ if sys.version_info >= (3, 7): |
| 65 |
+ _add_mypy_config(item) |
| 66 |
+ else: |
| 67 |
+ _fix_positional_only_args(item) |
| 68 |
+ except ImportError: |
| 69 |
+ pass |
| 70 |
|
| 71 |
|
| 72 |
@contextlib.contextmanager |
| 73 |
--- loguru.orig/tests/test_type_hinting.py |
| 74 |
+++ loguru/tests/test_type_hinting.py |
| 75 |
@@ -1,11 +1,17 @@ |
| 76 |
import sys |
| 77 |
|
| 78 |
-import mypy.api |
| 79 |
+import pytest |
| 80 |
|
| 81 |
+try: |
| 82 |
+ import mypy.api as mypy_api |
| 83 |
+except ImportError: |
| 84 |
+ mypy_api = None |
| 85 |
|
| 86 |
+ |
| 87 |
[email protected](mypy_api is None, reason="Requires mypy to be installed.") |
| 88 |
def test_mypy_import(): |
| 89 |
# Check stub file is valid and can be imported by Mypy. |
| 90 |
# There exist others tests in "typesafety" subfolder but they aren't compatible with Python 3.5. |
| 91 |
- out, _, result = mypy.api.run(["--strict", "-c", "from loguru import logger"]) |
| 92 |
+ out, _, result = mypy_api.run(["--strict", "-c", "from loguru import logger"]) |
| 93 |
print("".join(out), file=sys.stderr) |
| 94 |
assert result == 0 |