From 910e781594a459dd10e998b1a60c4a5be1e9055e Mon Sep 17 00:00:00 2001 From: Predrag Gruevski Date: Sat, 9 Jan 2021 20:05:28 -0500 Subject: [PATCH] Resolve a few straightforward mypy type errors. (#1492) * Add missing type hint to resolve mypy type inference failure. * Suppress conditional ujson/json import-related mypy errors. --- arviz/data/base.py | 4 +++- arviz/data/inference_data.py | 4 +++- arviz/data/io_json.py | 4 +++- arviz/data/io_pystan.py | 4 +++- arviz/tests/helpers.py | 2 +- 5 files changed, 13 insertions(+), 5 deletions(-) diff --git a/arviz/data/base.py b/arviz/data/base.py index 77e6042b0b..0f2ba04154 100644 --- a/arviz/data/base.py +++ b/arviz/data/base.py @@ -12,7 +12,9 @@ try: import ujson as json except ImportError: - import json + # mypy struggles with conditional imports expressed as catching ImportError: + # https://github.com/python/mypy/issues/1153 + import json # type: ignore from .. import __version__, utils diff --git a/arviz/data/inference_data.py b/arviz/data/inference_data.py index 07b444cda0..a28da39ea0 100644 --- a/arviz/data/inference_data.py +++ b/arviz/data/inference_data.py @@ -20,7 +20,9 @@ try: import ujson as json except ImportError: - import json + # mypy struggles with conditional imports expressed as catching ImportError: + # https://github.com/python/mypy/issues/1153 + import json # type: ignore SUPPORTED_GROUPS = [ "posterior", diff --git a/arviz/data/io_json.py b/arviz/data/io_json.py index 2503d58900..82ada77009 100644 --- a/arviz/data/io_json.py +++ b/arviz/data/io_json.py @@ -6,7 +6,9 @@ import ujson as json except ImportError: # Can't find ujson using json - import json + # mypy struggles with conditional imports expressed as catching ImportError: + # https://github.com/python/mypy/issues/1153 + import json # type: ignore def from_json(filename): diff --git a/arviz/data/io_pystan.py b/arviz/data/io_pystan.py index 22af4376b5..790b9d1057 100644 --- a/arviz/data/io_pystan.py +++ b/arviz/data/io_pystan.py @@ -17,7 +17,9 @@ import ujson as json except ImportError: # Can't find ujson using json - import json + # mypy struggles with conditional imports expressed as catching ImportError: + # https://github.com/python/mypy/issues/1153 + import json # type: ignore class PyStanConverter: diff --git a/arviz/tests/helpers.py b/arviz/tests/helpers.py index 4d80acb169..9df2d8ba3c 100644 --- a/arviz/tests/helpers.py +++ b/arviz/tests/helpers.py @@ -241,7 +241,7 @@ def check_multiple_attrs( in ``sample_stats``, also against what was expected. """ - failed_attrs = [] + failed_attrs: List[Union[str, Tuple[str, str]]] = [] for dataset_name, attributes in test_dict.items(): if dataset_name.startswith("~"): if hasattr(parent, dataset_name[1:]):