Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable mypy in precommit #318

Merged
merged 3 commits into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 4 additions & 6 deletions karapace/compatibility/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)


Expand Down Expand Up @@ -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
16 changes: 12 additions & 4 deletions karapace/compatibility/jsonschema/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion karapace/compatibility/jsonschema/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 5 additions & 1 deletion karapace/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
169 changes: 154 additions & 15 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Empty file removed tests/__init__.py
Empty file.
Empty file removed tests/schemas/__init__.py
Empty file.