From eacbe7e1a64ad1483cbd33ed6af7b8f700f82e1f Mon Sep 17 00:00:00 2001 From: staticdev Date: Thu, 25 Jun 2020 10:00:53 -0300 Subject: [PATCH 1/4] Strict mypy --- mypy.ini | 33 ++++++++++++++++++++++++++++++++- src/toml_validator/py.typed | 0 tests/test_main.py | 15 ++++++++------- tests/test_validation.py | 13 +++++++------ 4 files changed, 47 insertions(+), 14 deletions(-) create mode 100644 src/toml_validator/py.typed diff --git a/mypy.ini b/mypy.ini index 313e479..8685796 100644 --- a/mypy.ini +++ b/mypy.ini @@ -1,4 +1,35 @@ [mypy] +check_untyped_defs = True +disallow_any_generics = True +disallow_incomplete_defs = True +disallow_subclassing_any = True +disallow_untyped_calls = True +disallow_untyped_decorators = True +disallow_untyped_defs = True +no_implicit_optional = True +no_implicit_reexport = True +pretty = True +show_column_numbers = True +show_error_codes = True +show_error_context = True +strict_equality = True +warn_redundant_casts = True +warn_return_any = True +warn_unreachable = True +warn_unused_configs = True +warn_unused_ignores = True -[mypy-tomlkit.*,pytest,pytest_mock,_pytest.*] +[mypy-tests.*] +disallow_untyped_decorators = False + +[mypy-pytest] +ignore_missing_imports = True + +[mypy-_pytest.*] +ignore_missing_imports = True + +[mypy-pytest_mock] +ignore_missing_imports = True + +[mypy-tomlkit.*] ignore_missing_imports = True diff --git a/src/toml_validator/py.typed b/src/toml_validator/py.typed new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_main.py b/tests/test_main.py index 420fc16..13eaff0 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -1,4 +1,5 @@ """Test cases for the __main__ module.""" +from typing import Any from unittest.mock import Mock import pytest @@ -15,13 +16,13 @@ def runner() -> CliRunner: @pytest.fixture -def mock_validation_validate_extension(mocker: MockFixture) -> Mock: +def mock_validation_validate_extension(mocker: MockFixture) -> Any: """Fixture for mocking validation.validate_extension.""" return mocker.patch("toml_validator.validation.validate_extension") @pytest.fixture -def mock_validation_validate_toml_no_error(mocker: MockFixture) -> Mock: +def mock_validation_validate_toml_no_error(mocker: MockFixture) -> Any: """Fixture for mocking validation.validate_toml with no errors.""" mock = mocker.patch("toml_validator.validation.validate_toml") mock.return_value = "" @@ -29,14 +30,14 @@ def mock_validation_validate_toml_no_error(mocker: MockFixture) -> Mock: @pytest.fixture -def mock_validation_validate_toml_with_error(mocker: MockFixture) -> Mock: +def mock_validation_validate_toml_with_error(mocker: MockFixture) -> Any: """Fixture for mocking validation.validate_toml with error.""" mock = mocker.patch("toml_validator.validation.validate_toml") mock.return_value = "|some error description|" return mock -def test_main_without_argument(runner: CliRunner): +def test_main_without_argument(runner: CliRunner) -> None: """It exits with a status code of 2.""" result = runner.invoke(__main__.main) assert result.exit_code == 2 @@ -46,7 +47,7 @@ def test_main_with_argument_success( runner: CliRunner, mock_validation_validate_extension: Mock, mock_validation_validate_toml_no_error: Mock, -): +) -> None: """It exits with a status code of zero.""" with runner.isolated_filesystem(): with open("file.toml", "w") as f: @@ -63,7 +64,7 @@ def test_main_with_argument_fail( runner: CliRunner, mock_validation_validate_extension: Mock, mock_validation_validate_toml_with_error: Mock, -): +) -> None: """It outputs error.""" with runner.isolated_filesystem(): with open("file.toml", "w") as f: @@ -77,7 +78,7 @@ def test_main_with_argument_fail( @pytest.mark.e2e -def test_main_without_arguments_in_production_env(runner: CliRunner): +def test_main_without_arguments_in_production_env(runner: CliRunner) -> None: """It exits with a status code of 2 (e2e).""" result = runner.invoke(__main__.main) assert result.exit_code == 2 diff --git a/tests/test_validation.py b/tests/test_validation.py index 9526f8b..8420319 100644 --- a/tests/test_validation.py +++ b/tests/test_validation.py @@ -1,4 +1,5 @@ """Test cases for the validation module.""" +from typing import Any from unittest.mock import Mock import pytest @@ -9,13 +10,13 @@ @pytest.fixture -def mock_tomlkit_parse(mocker: MockFixture) -> Mock: +def mock_tomlkit_parse(mocker: MockFixture) -> Any: """Fixture for mocking tomlkit.parse.""" return mocker.patch("tomlkit.parse") @pytest.fixture -def mock_tomlkit_parse_exception(mocker: MockFixture) -> Mock: +def mock_tomlkit_parse_exception(mocker: MockFixture) -> Any: """Fixture for mocking tomlkit.parse.""" mock = mocker.patch("tomlkit.parse") mock.side_effect = TOMLKitError("|some tomlkit error|") @@ -23,13 +24,13 @@ def mock_tomlkit_parse_exception(mocker: MockFixture) -> Mock: @pytest.fixture -def mock_open_valid_file(mocker: MockFixture) -> Mock: +def mock_open_valid_file(mocker: MockFixture) -> Any: """Fixture for mocking build-in open for valid TOML file.""" return mocker.patch("builtins.open", mocker.mock_open(read_data="[x]\na = 3")) @pytest.fixture -def mock_open_invalid_file(mocker: MockFixture) -> Mock: +def mock_open_invalid_file(mocker: MockFixture) -> Any: """Fixture for mocking build-in open for valid TOML file.""" return mocker.patch( "builtins.open", mocker.mock_open(read_data="[x]\na = 3\n[x]\na = 3") @@ -62,12 +63,12 @@ def test_validate_toml_with_error( @pytest.mark.e2e -def test_validate_toml_no_error_production(mock_open_valid_file) -> None: +def test_validate_toml_no_error_production(mock_open_valid_file: Mock) -> None: """It returns no errors when valid TOML (e2e).""" assert validation.validate_toml("file.toml") == "" @pytest.mark.e2e -def test_validate_toml_with_error_production(mock_open_invalid_file) -> None: +def test_validate_toml_with_error_production(mock_open_invalid_file: Mock) -> None: """It returns errors when invalid TOML (e2e).""" assert validation.validate_toml("file.toml") == 'Key "x" already exists.' From 45fa99a6c5afbcafd37caddceeb04ffdd847128a Mon Sep 17 00:00:00 2001 From: staticdev Date: Fri, 26 Jun 2020 08:23:09 -0300 Subject: [PATCH 2/4] Return to Mock from Any --- tests/test_main.py | 23 ++++++++++++----------- tests/test_validation.py | 9 ++++----- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/tests/test_main.py b/tests/test_main.py index 13eaff0..67a3e65 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -1,28 +1,27 @@ """Test cases for the __main__ module.""" -from typing import Any from unittest.mock import Mock +import click.testing import pytest -from click.testing import CliRunner from pytest_mock import MockFixture from toml_validator import __main__ @pytest.fixture -def runner() -> CliRunner: +def runner() -> click.testing.CliRunner: """Fixture for invoking command-line interfaces.""" - return CliRunner() + return click.testing.CliRunner() @pytest.fixture -def mock_validation_validate_extension(mocker: MockFixture) -> Any: +def mock_validation_validate_extension(mocker: MockFixture) -> Mock: """Fixture for mocking validation.validate_extension.""" return mocker.patch("toml_validator.validation.validate_extension") @pytest.fixture -def mock_validation_validate_toml_no_error(mocker: MockFixture) -> Any: +def mock_validation_validate_toml_no_error(mocker: MockFixture) -> Mock: """Fixture for mocking validation.validate_toml with no errors.""" mock = mocker.patch("toml_validator.validation.validate_toml") mock.return_value = "" @@ -30,21 +29,21 @@ def mock_validation_validate_toml_no_error(mocker: MockFixture) -> Any: @pytest.fixture -def mock_validation_validate_toml_with_error(mocker: MockFixture) -> Any: +def mock_validation_validate_toml_with_error(mocker: MockFixture) -> Mock: """Fixture for mocking validation.validate_toml with error.""" mock = mocker.patch("toml_validator.validation.validate_toml") mock.return_value = "|some error description|" return mock -def test_main_without_argument(runner: CliRunner) -> None: +def test_main_without_argument(runner: click.testing.CliRunner) -> None: """It exits with a status code of 2.""" result = runner.invoke(__main__.main) assert result.exit_code == 2 def test_main_with_argument_success( - runner: CliRunner, + runner: click.testing.CliRunner, mock_validation_validate_extension: Mock, mock_validation_validate_toml_no_error: Mock, ) -> None: @@ -61,7 +60,7 @@ def test_main_with_argument_success( def test_main_with_argument_fail( - runner: CliRunner, + runner: click.testing.CliRunner, mock_validation_validate_extension: Mock, mock_validation_validate_toml_with_error: Mock, ) -> None: @@ -78,7 +77,9 @@ def test_main_with_argument_fail( @pytest.mark.e2e -def test_main_without_arguments_in_production_env(runner: CliRunner) -> None: +def test_main_without_arguments_in_production_env( + runner: click.testing.CliRunner, +) -> None: """It exits with a status code of 2 (e2e).""" result = runner.invoke(__main__.main) assert result.exit_code == 2 diff --git a/tests/test_validation.py b/tests/test_validation.py index 8420319..56a3843 100644 --- a/tests/test_validation.py +++ b/tests/test_validation.py @@ -1,5 +1,4 @@ """Test cases for the validation module.""" -from typing import Any from unittest.mock import Mock import pytest @@ -10,13 +9,13 @@ @pytest.fixture -def mock_tomlkit_parse(mocker: MockFixture) -> Any: +def mock_tomlkit_parse(mocker: MockFixture) -> Mock: """Fixture for mocking tomlkit.parse.""" return mocker.patch("tomlkit.parse") @pytest.fixture -def mock_tomlkit_parse_exception(mocker: MockFixture) -> Any: +def mock_tomlkit_parse_exception(mocker: MockFixture) -> Mock: """Fixture for mocking tomlkit.parse.""" mock = mocker.patch("tomlkit.parse") mock.side_effect = TOMLKitError("|some tomlkit error|") @@ -24,13 +23,13 @@ def mock_tomlkit_parse_exception(mocker: MockFixture) -> Any: @pytest.fixture -def mock_open_valid_file(mocker: MockFixture) -> Any: +def mock_open_valid_file(mocker: MockFixture) -> Mock: """Fixture for mocking build-in open for valid TOML file.""" return mocker.patch("builtins.open", mocker.mock_open(read_data="[x]\na = 3")) @pytest.fixture -def mock_open_invalid_file(mocker: MockFixture) -> Any: +def mock_open_invalid_file(mocker: MockFixture) -> Mock: """Fixture for mocking build-in open for valid TOML file.""" return mocker.patch( "builtins.open", mocker.mock_open(read_data="[x]\na = 3\n[x]\na = 3") From fe98bd494714217053108f025d16bd5f1bdfa831 Mon Sep 17 00:00:00 2001 From: staticdev Date: Fri, 26 Jun 2020 08:35:22 -0300 Subject: [PATCH 3/4] Temporary add of private stubs --- mypy.ini | 3 --- pytest_mock/__init__.pyi | 3 +++ pytest_mock/plugin.pyi | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 pytest_mock/__init__.pyi create mode 100644 pytest_mock/plugin.pyi diff --git a/mypy.ini b/mypy.ini index 8685796..e4b9f96 100644 --- a/mypy.ini +++ b/mypy.ini @@ -28,8 +28,5 @@ ignore_missing_imports = True [mypy-_pytest.*] ignore_missing_imports = True -[mypy-pytest_mock] -ignore_missing_imports = True - [mypy-tomlkit.*] ignore_missing_imports = True diff --git a/pytest_mock/__init__.pyi b/pytest_mock/__init__.pyi new file mode 100644 index 0000000..e993084 --- /dev/null +++ b/pytest_mock/__init__.pyi @@ -0,0 +1,3 @@ +# incomplete + +from .plugin import MockFixture as MockFixture diff --git a/pytest_mock/plugin.pyi b/pytest_mock/plugin.pyi new file mode 100644 index 0000000..160afa5 --- /dev/null +++ b/pytest_mock/plugin.pyi @@ -0,0 +1,18 @@ +# incomplete + +from typing import Any, TypeVar, overload +from unittest.mock import MagicMock + +_T = TypeVar("_T") + +class MockFixture: + mock_module: Any # unittest.mock module + def __init__(self, config: Any) -> None: ... + patch: _Patcher + + class _Patcher: + mock_module: Any # unittest.mock module + @overload + def __call__(self, target: Any, new: _T, *args: Any, **kwargs: Any) -> _T: ... + @overload + def __call__(self, target: Any, *args: Any, **kwargs: Any) -> MagicMock: ... From 74e6bfc14dfe5b48fd0c9ac6a471465d8ec5b74b Mon Sep 17 00:00:00 2001 From: staticdev Date: Fri, 17 Jul 2020 20:58:02 -0300 Subject: [PATCH 4/4] Mypy fixes --- mypy.ini | 9 ++++++--- pytest_mock/__init__.pyi | 3 --- pytest_mock/plugin.pyi | 18 ------------------ tests/test_main.py | 7 ++++--- tests/test_validation.py | 9 +++++---- 5 files changed, 15 insertions(+), 31 deletions(-) delete mode 100644 pytest_mock/__init__.pyi delete mode 100644 pytest_mock/plugin.pyi diff --git a/mypy.ini b/mypy.ini index e4b9f96..3a36ba2 100644 --- a/mypy.ini +++ b/mypy.ini @@ -19,14 +19,17 @@ warn_unreachable = True warn_unused_configs = True warn_unused_ignores = True -[mypy-tests.*] -disallow_untyped_decorators = False +[mypy-_pytest.*] +ignore_missing_imports = True [mypy-pytest] ignore_missing_imports = True -[mypy-_pytest.*] +[mypy-pytest_mock] ignore_missing_imports = True +[mypy-tests.*] +disallow_untyped_decorators = False + [mypy-tomlkit.*] ignore_missing_imports = True diff --git a/pytest_mock/__init__.pyi b/pytest_mock/__init__.pyi deleted file mode 100644 index e993084..0000000 --- a/pytest_mock/__init__.pyi +++ /dev/null @@ -1,3 +0,0 @@ -# incomplete - -from .plugin import MockFixture as MockFixture diff --git a/pytest_mock/plugin.pyi b/pytest_mock/plugin.pyi deleted file mode 100644 index 160afa5..0000000 --- a/pytest_mock/plugin.pyi +++ /dev/null @@ -1,18 +0,0 @@ -# incomplete - -from typing import Any, TypeVar, overload -from unittest.mock import MagicMock - -_T = TypeVar("_T") - -class MockFixture: - mock_module: Any # unittest.mock module - def __init__(self, config: Any) -> None: ... - patch: _Patcher - - class _Patcher: - mock_module: Any # unittest.mock module - @overload - def __call__(self, target: Any, new: _T, *args: Any, **kwargs: Any) -> _T: ... - @overload - def __call__(self, target: Any, *args: Any, **kwargs: Any) -> MagicMock: ... diff --git a/tests/test_main.py b/tests/test_main.py index 67a3e65..d152eb9 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -1,4 +1,5 @@ """Test cases for the __main__ module.""" +from typing import Any from unittest.mock import Mock import click.testing @@ -15,13 +16,13 @@ def runner() -> click.testing.CliRunner: @pytest.fixture -def mock_validation_validate_extension(mocker: MockFixture) -> Mock: +def mock_validation_validate_extension(mocker: MockFixture) -> Any: """Fixture for mocking validation.validate_extension.""" return mocker.patch("toml_validator.validation.validate_extension") @pytest.fixture -def mock_validation_validate_toml_no_error(mocker: MockFixture) -> Mock: +def mock_validation_validate_toml_no_error(mocker: MockFixture) -> Any: """Fixture for mocking validation.validate_toml with no errors.""" mock = mocker.patch("toml_validator.validation.validate_toml") mock.return_value = "" @@ -29,7 +30,7 @@ def mock_validation_validate_toml_no_error(mocker: MockFixture) -> Mock: @pytest.fixture -def mock_validation_validate_toml_with_error(mocker: MockFixture) -> Mock: +def mock_validation_validate_toml_with_error(mocker: MockFixture) -> Any: """Fixture for mocking validation.validate_toml with error.""" mock = mocker.patch("toml_validator.validation.validate_toml") mock.return_value = "|some error description|" diff --git a/tests/test_validation.py b/tests/test_validation.py index 56a3843..8420319 100644 --- a/tests/test_validation.py +++ b/tests/test_validation.py @@ -1,4 +1,5 @@ """Test cases for the validation module.""" +from typing import Any from unittest.mock import Mock import pytest @@ -9,13 +10,13 @@ @pytest.fixture -def mock_tomlkit_parse(mocker: MockFixture) -> Mock: +def mock_tomlkit_parse(mocker: MockFixture) -> Any: """Fixture for mocking tomlkit.parse.""" return mocker.patch("tomlkit.parse") @pytest.fixture -def mock_tomlkit_parse_exception(mocker: MockFixture) -> Mock: +def mock_tomlkit_parse_exception(mocker: MockFixture) -> Any: """Fixture for mocking tomlkit.parse.""" mock = mocker.patch("tomlkit.parse") mock.side_effect = TOMLKitError("|some tomlkit error|") @@ -23,13 +24,13 @@ def mock_tomlkit_parse_exception(mocker: MockFixture) -> Mock: @pytest.fixture -def mock_open_valid_file(mocker: MockFixture) -> Mock: +def mock_open_valid_file(mocker: MockFixture) -> Any: """Fixture for mocking build-in open for valid TOML file.""" return mocker.patch("builtins.open", mocker.mock_open(read_data="[x]\na = 3")) @pytest.fixture -def mock_open_invalid_file(mocker: MockFixture) -> Mock: +def mock_open_invalid_file(mocker: MockFixture) -> Any: """Fixture for mocking build-in open for valid TOML file.""" return mocker.patch( "builtins.open", mocker.mock_open(read_data="[x]\na = 3\n[x]\na = 3")