From 12f086531546265f028910e1000120e9958d3e8e Mon Sep 17 00:00:00 2001 From: Anton Agestam Date: Mon, 3 Feb 2020 16:20:49 +0100 Subject: [PATCH 1/2] Add stricter mypy type check flags - Remove unused type: ignore. - Remove asterisk import and add `__all__` with all exported names, to pass with the `no_explicit_reexport` mypy flag. - Add various mypy config for developer ergonomics (prettier errors, printing error codes, warning about unused config, ignores and casts etc). Kept `warn_return_any = False` in `dacite.types` for now since there would have to be a lot of ignores/casts otherwise. --- .travis.yml | 2 +- dacite/__init__.py | 17 ++++++++++++++++- dacite/dataclasses.py | 2 +- dacite/types.py | 2 +- setup.cfg | 22 ++++++++++++++++++++++ 5 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 setup.cfg diff --git a/.travis.yml b/.travis.yml index 2962b56..d4ad079 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,7 +9,7 @@ install: script: - pytest --cov=dacite - black --check . -- mypy dacite +- mypy - pylint dacite after_success: coveralls diff --git a/dacite/__init__.py b/dacite/__init__.py index 1166404..abf28a7 100644 --- a/dacite/__init__.py +++ b/dacite/__init__.py @@ -1,3 +1,18 @@ from dacite.config import Config from dacite.core import from_dict -from dacite.exceptions import * +from dacite.exceptions import ( + DaciteError, DaciteFieldError, WrongTypeError, MissingValueError, + UnionMatchError, ForwardReferenceError, UnexpectedDataError, +) + +__all__ = ( + 'Config', + 'from_dict', + 'DaciteError', + 'DaciteFieldError', + 'WrongTypeError', + 'MissingValueError', + 'UnionMatchError', + 'ForwardReferenceError', + 'UnexpectedDataError', +) diff --git a/dacite/dataclasses.py b/dacite/dataclasses.py index 2941019..15e5d8e 100644 --- a/dacite/dataclasses.py +++ b/dacite/dataclasses.py @@ -22,7 +22,7 @@ def get_default_value_for_field(field: Field) -> Any: def create_instance(data_class: Type[T], init_values: Data, post_init_values: Data) -> T: - instance = data_class(**init_values) + instance: T = data_class(**init_values) for key, value in post_init_values.items(): setattr(instance, key, value) return instance diff --git a/dacite/types.py b/dacite/types.py index c4f757a..cd32495 100644 --- a/dacite/types.py +++ b/dacite/types.py @@ -125,7 +125,7 @@ def is_generic_collection(type_: Type) -> bool: def extract_generic(type_: Type) -> tuple: - return type_.__args__ # type: ignore + return type_.__args__ def is_subclass(sub_type: Type, base_type: Type) -> bool: diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..9aee99d --- /dev/null +++ b/setup.cfg @@ -0,0 +1,22 @@ +[mypy] +python_version = 3.6 +files = dacite +show_error_codes = True +pretty = True + +no_implicit_reexport = True +no_implicit_optional = True +strict_equality = True +strict_optional = True +check_untyped_defs = True +disallow_incomplete_defs = True +ignore_missing_imports = False + +warn_unused_configs = True +warn_redundant_casts = True +warn_unused_ignores = True +warn_return_any = True +warn_unreachable = True + +[mypy-dacite.types] +warn_return_any = False From f9f9dbe980f040e3b37f71a48325f26c492eb198 Mon Sep 17 00:00:00 2001 From: Anton Agestam Date: Mon, 3 Feb 2020 16:45:19 +0100 Subject: [PATCH 2/2] fix: black --- dacite/__init__.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/dacite/__init__.py b/dacite/__init__.py index abf28a7..5909a72 100644 --- a/dacite/__init__.py +++ b/dacite/__init__.py @@ -1,18 +1,23 @@ from dacite.config import Config from dacite.core import from_dict from dacite.exceptions import ( - DaciteError, DaciteFieldError, WrongTypeError, MissingValueError, - UnionMatchError, ForwardReferenceError, UnexpectedDataError, + DaciteError, + DaciteFieldError, + WrongTypeError, + MissingValueError, + UnionMatchError, + ForwardReferenceError, + UnexpectedDataError, ) __all__ = ( - 'Config', - 'from_dict', - 'DaciteError', - 'DaciteFieldError', - 'WrongTypeError', - 'MissingValueError', - 'UnionMatchError', - 'ForwardReferenceError', - 'UnexpectedDataError', + "Config", + "from_dict", + "DaciteError", + "DaciteFieldError", + "WrongTypeError", + "MissingValueError", + "UnionMatchError", + "ForwardReferenceError", + "UnexpectedDataError", )