diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 42332093f..e6c0d5270 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -23,6 +23,14 @@ repos: name: isort (python) files: \.py$ + - repo: https://github.com/pre-commit/mirrors-mypy + rev: v0.931 + hooks: + - id: mypy + name: Mypy Karapace + pass_filenames: false + args: ["--ignore-missing-imports", "--package", "karapace"] + - repo: https://github.com/pre-commit/pre-commit-hooks rev: v2.5.0 hooks: diff --git a/karapace/compatibility/__init__.py b/karapace/compatibility/__init__.py index 4bdc2cf41..732c20b3f 100644 --- a/karapace/compatibility/__init__.py +++ b/karapace/compatibility/__init__.py @@ -15,7 +15,9 @@ ) from karapace.compatibility.jsonschema.checks import compatibility as jsonschema_compatibility from karapace.compatibility.protobuf.checks import check_protobuf_schema_compatibility +from karapace.protobuf.schema import ProtobufSchema from karapace.schema_reader import SchemaType, TypedSchema +from karapace.utils import assert_never import logging @@ -67,7 +69,7 @@ def check_jsonschema_compatibility(reader: Draft7Validator, writer: Draft7Valida return jsonschema_compatibility(reader, writer) -def check_protobuf_compatibility(reader, writer) -> SchemaCompatibilityResult: +def check_protobuf_compatibility(reader: ProtobufSchema, writer: ProtobufSchema) -> SchemaCompatibilityResult: return check_protobuf_schema_compatibility(reader, writer) @@ -161,10 +163,6 @@ def check_compatibility( ) else: - result = SchemaCompatibilityResult.incompatible( - incompat_type=SchemaIncompatibilityType.type_mismatch, - message=f"Unknow schema_type {old_schema.schema_type}", - location=[], - ) + assert_never(f"Unknown schema_type {old_schema.schema_type}") return result diff --git a/karapace/compatibility/jsonschema/checks.py b/karapace/compatibility/jsonschema/checks.py index 659bc7662..e9833e22d 100644 --- a/karapace/compatibility/jsonschema/checks.py +++ b/karapace/compatibility/jsonschema/checks.py @@ -129,7 +129,11 @@ ) -def type_mismatch(reader_type, writer_type, location: List[str]) -> SchemaCompatibilityResult: +def type_mismatch( + reader_type: JSONSCHEMA_TYPES, + writer_type: JSONSCHEMA_TYPES, + location: List[str], +) -> SchemaCompatibilityResult: return SchemaCompatibilityResult.incompatible( incompat_type=Incompatibility.type_changed, message=f"type {reader_type} is not compatible with type {writer_type}", @@ -189,7 +193,11 @@ def compatibility(reader: Draft7Validator, writer: Draft7Validator) -> SchemaCom def check_simple_subschema( - simplified_reader_schema, simplified_writer_schema, original_reader_type, original_writer_type, location + simplified_reader_schema: Any, + simplified_writer_schema: Any, + original_reader_type: JSONSCHEMA_TYPES, + original_writer_type: JSONSCHEMA_TYPES, + location: List[str], ) -> SchemaCompatibilityResult: rec_result = compatibility_rec(simplified_reader_schema, simplified_writer_schema, location) if is_compatible(rec_result): @@ -307,7 +315,7 @@ def check_assertion_compatibility( ) # The type error below is due to a mypy bug for version 0.820 (issue #10131) - if assertion_check.comparison(reader_value, writer_value): # type: ignore + if assertion_check.comparison(reader_value, writer_value): # type: ignore[call-arg] result.add_incompatibility( incompat_type=assertion_check.error_when_restricting, message=assertion_check.error_msg_when_restricting.format( @@ -816,7 +824,7 @@ def compatibility_subschemas(reader_schema, writer_schema, location: List[str]) qty_of_required_compatible_subschemas = len_writer_subschemas compatible_schemas_count = count_uniquely_compatible_schemas( - reader_type, + reader_type, # type: ignore reader_subschemas, writer_subschemas, subschema_location, diff --git a/karapace/compatibility/jsonschema/utils.py b/karapace/compatibility/jsonschema/utils.py index fdb5c90f4..79f68f2ba 100644 --- a/karapace/compatibility/jsonschema/utils.py +++ b/karapace/compatibility/jsonschema/utils.py @@ -14,7 +14,7 @@ def normalize_schema(validator: Draft7Validator) -> Any: return normalize_schema_rec(validator, original_schema) -def normalize_schema_rec(validator, original_schema) -> Any: +def normalize_schema_rec(validator: Draft7Validator, original_schema: Any) -> Any: if isinstance(original_schema, (bool, str, float, int)) or original_schema is None: return original_schema diff --git a/karapace/utils.py b/karapace/utils.py index 3fae52f1b..0fee33293 100644 --- a/karapace/utils.py +++ b/karapace/utils.py @@ -7,7 +7,7 @@ from functools import partial from http import HTTPStatus from kafka.client_async import BrokerConnection, KafkaClient, MetadataRequest -from typing import Optional +from typing import NoReturn, Optional from urllib.parse import urljoin import aiohttp @@ -65,6 +65,10 @@ def json_encode(obj, *, compact=True, sort_keys=True, binary=False): return res.encode("utf-8") if binary else res +def assert_never(value: NoReturn) -> NoReturn: + raise RuntimeError(f"This code should never be reached, got: {value}") + + class Result: def __init__(self, status, json_result, headers=None): # We create both status and status_code so people can be agnostic on whichever to use diff --git a/mypy.ini b/mypy.ini index db2ebbf89..a0952b212 100644 --- a/mypy.ini +++ b/mypy.ini @@ -1,11 +1,6 @@ [mypy] python_version = 3.7 warn_redundant_casts = True - -[mypy-karapace.*] -ignore_errors = True - -[mypy-karapace.avro_compatibility] ignore_errors = False disallow_untyped_defs = True disallow_incomplete_defs = True @@ -16,13 +11,157 @@ warn_no_return = True warn_unreachable = True strict_equality = True -[mypy-karapace.compatibility] -ignore_errors = False -disallow_untyped_defs = True -disallow_incomplete_defs = True -check_untyped_defs = True -no_implicit_optional = True -warn_unused_ignores = True -warn_no_return = True -warn_unreachable = True -strict_equality = True +[mypy-karapace.schema_registry_apis] +ignore_errors = True + +[mypy-karapace.karapace] +ignore_errors = True + +[mypy-karapace] +ignore_errors = True + +[mypy-karapace.version] +ignore_errors = True + +[mypy-karapace.compatibility.jsonschema.checks] +disallow_untyped_defs = False +disallow_incomplete_defs = False +warn_unused_ignores = False + +[mypy-karapace.constants] +ignore_errors = True + +[mypy-karapace.karapace_all] +ignore_errors = True + +[mypy-karapace.protobuf.one_of_element] +ignore_errors = True + +[mypy-karapace.protobuf.syntax] +ignore_errors = True + +[mypy-karapace.protobuf.field] +ignore_errors = True + +[mypy-karapace.protobuf.kotlin_wrapper] +ignore_errors = True + +[mypy-karapace.protobuf.io] +ignore_errors = True + +[mypy-karapace.protobuf.field_element] +ignore_errors = True + +[mypy-karapace.protobuf.proto_file_element] +ignore_errors = True + +[mypy-karapace.protobuf.compare_type_storage] +ignore_errors = True + +[mypy-karapace.protobuf.type_element] +ignore_errors = True + +[mypy-karapace.protobuf.enum_element] +ignore_errors = True + +[mypy-karapace.protobuf.encoding_variants] +ignore_errors = True + +[mypy-karapace.protobuf] +ignore_errors = True + +[mypy-karapace.protobuf.schema] +ignore_errors = True + +[mypy-karapace.protobuf.extensions_element] +ignore_errors = True + +[mypy-karapace.protobuf.protobuf_to_dict] +ignore_errors = True + +[mypy-karapace.protobuf.exception] +ignore_errors = True + +[mypy-karapace.protobuf.rpc_element] +ignore_errors = True + +[mypy-karapace.protobuf.option_reader] +ignore_errors = True + +[mypy-karapace.protobuf.option_element] +ignore_errors = True + +[mypy-karapace.protobuf.enum_constant_element] +ignore_errors = True + +[mypy-karapace.protobuf.compare_result] +ignore_errors = True + +[mypy-karapace.protobuf.location] +ignore_errors = True + +[mypy-karapace.protobuf.message_element] +ignore_errors = True + +[mypy-karapace.protobuf.syntax_reader] +ignore_errors = True + +[mypy-karapace.protobuf.utils] +ignore_errors = True + +[mypy-karapace.protobuf.extend_element] +ignore_errors = True + +[mypy-karapace.protobuf.reserved_element] +ignore_errors = True + +[mypy-karapace.protobuf.proto_type] +ignore_errors = True + +[mypy-karapace.protobuf.group_element] +ignore_errors = True + +[mypy-karapace.protobuf.service_element] +ignore_errors = True + +[mypy-karapace.protobuf.proto_parser] +ignore_errors = True + +[mypy-karapace.config] +ignore_errors = True + +[mypy-karapace.schema_reader] +ignore_errors = True + +[mypy-karapace.statsd] +ignore_errors = True + +[mypy-karapace.utils] +ignore_errors = True + +[mypy-karapace.rapu] +ignore_errors = True + +[mypy-karapace.serialization] +ignore_errors = True + +[mypy-karapace.master_coordinator] +ignore_errors = True + +[mypy-karapace.kafka_rest_apis.__main__] +ignore_errors = True + +[mypy-karapace.kafka_rest_apis.consumer_manager] +ignore_errors = True + +[mypy-karapace.kafka_rest_apis] +ignore_errors = True + +[mypy-karapace.kafka_rest_apis.admin] +ignore_errors = True + +[mypy-karapace.kafka_rest_apis.error_codes] +ignore_errors = True + +[mypy-karapace.schema_backup] +ignore_errors = True diff --git a/tests/__init__.py b/tests/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/tests/schemas/__init__.py b/tests/schemas/__init__.py deleted file mode 100644 index e69de29bb..000000000