Skip to content

Commit

Permalink
Check types using Pyright (#251)
Browse files Browse the repository at this point in the history
  • Loading branch information
disrupted authored Jun 15, 2023
1 parent 6698917 commit c265ac2
Show file tree
Hide file tree
Showing 20 changed files with 216 additions and 312 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ jobs:
- name: Typing (mypy)
run: poetry run pre-commit run mypy --all-files

- name: Typing (pyright)
run: poetry run pre-commit run pyright --all-files

- name: Generate schema (kpops schema)
run: poetry run pre-commit run gen-schema --all-files --show-diff-on-failure

Expand Down
9 changes: 9 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ repos:
types: [python]
require_serial: true # run once for all files
exclude: ^tests/.*snapshots/
- repo: local
hooks:
- id: pyright
name: pyright
entry: pyright
language: system
types: [python]
require_serial: true # run once for all files
exclude: ^tests/.*snapshots/
- repo: https://github.com/asottile/pyupgrade
rev: v3.1.0
hooks:
Expand Down
10 changes: 8 additions & 2 deletions kpops/component_handlers/schema_handler/schema_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from functools import cached_property

from schema_registry.client import SchemaRegistryClient
from schema_registry.client.schema import AvroSchema

from kpops.cli.exception import ClassNotFoundError
from kpops.cli.pipeline_config import PipelineConfig
Expand Down Expand Up @@ -32,7 +33,7 @@ def schema_provider(self) -> SchemaProvider:
f"The Schema Registry URL is set but you haven't specified the component module path. Please provide a valid component module path where your {SchemaProvider.__name__} implementation exists."
)
schema_provider_class = find_class(self.components_module, SchemaProvider) # type: ignore[type-abstract]
return schema_provider_class()
return schema_provider_class() # pyright: ignore[reportGeneralTypeIssues]
except ClassNotFoundError:
raise ValueError(
f"No schema provider found in components module {self.components_module}. "
Expand Down Expand Up @@ -138,8 +139,13 @@ def __check_compatibility(
if not self.schema_registry_client.test_compatibility(
subject=subject, schema=schema
):
schema_str = (
schema.flat_schema
if isinstance(schema, AvroSchema)
else str(schema)
)
raise Exception(
f"Schema is not compatible for {subject} and model {schema_class}. \n {json.dumps(schema.flat_schema, indent=4)}"
f"Schema is not compatible for {subject} and model {schema_class}. \n {json.dumps(schema_str, indent=4)}"
)
else:
log.debug(
Expand Down
6 changes: 2 additions & 4 deletions kpops/component_handlers/topic/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ def __init__(self, proxy_wrapper: ProxyWrapper):
self.proxy_wrapper = proxy_wrapper

def create_topics(self, to_section: ToSection, dry_run: bool) -> None:
topics: dict[str, TopicConfig] = to_section.topics
for topic_name, topic_config in topics.items():
for topic_name, topic_config in to_section.topics.items():
topic_spec = self.__prepare_body(topic_name, topic_config)
if dry_run:
self.__dry_run_topic_creation(topic_name, topic_spec, topic_config)
Expand Down Expand Up @@ -66,8 +65,7 @@ def create_topics(self, to_section: ToSection, dry_run: bool) -> None:
self.proxy_wrapper.create_topic(topic_spec=topic_spec)

def delete_topics(self, to_section: ToSection, dry_run: bool) -> None:
topics: dict[str, TopicConfig] = to_section.topics
for topic_name in topics.keys():
for topic_name in to_section.topics.keys():
if dry_run:
self.__dry_run_topic_deletion(topic_name=topic_name)
else:
Expand Down
3 changes: 0 additions & 3 deletions kpops/components/base_components/kafka_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@ class KafkaConnector(PipelineComponent, ABC):
class Config(CamelCaseConfig):
pass

def __init__(self, **kwargs) -> None:
super().__init__(**kwargs)

@cached_property
def helm(self) -> Helm:
"""Helm object that contains component-specific config such as repo"""
Expand Down
3 changes: 3 additions & 0 deletions kpops/components/base_components/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from typing import NewType

TopicName = NewType("TopicName", str)
2 changes: 1 addition & 1 deletion kpops/components/base_components/models/from_section.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from pydantic import BaseModel, Extra, Field, root_validator

from kpops.components.base_components.models import TopicName
from kpops.utils.docstring import describe_attr
from kpops.utils.pydantic import DescConfig

Expand Down Expand Up @@ -54,7 +55,6 @@ def extra_topic_role(cls, values: dict) -> dict:
return values


TopicName = NewType("TopicName", str)
ComponentName = NewType("ComponentName", str)


Expand Down
3 changes: 2 additions & 1 deletion kpops/components/base_components/models/to_section.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from pydantic import BaseModel, Extra, Field, root_validator

from kpops.components.base_components.models import TopicName
from kpops.utils.docstring import describe_attr
from kpops.utils.pydantic import DescConfig

Expand Down Expand Up @@ -89,7 +90,7 @@ class ToSection(BaseModel):
models: dict[str, Any] = Field(
default={}, description=describe_attr("models", __doc__)
)
topics: dict[str, TopicConfig] = Field(
topics: dict[TopicName, TopicConfig] = Field(
..., description=describe_attr("topics", __doc__)
)

Expand Down
2 changes: 1 addition & 1 deletion kpops/components/base_components/pipeline_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class Config(CamelCaseConfig, DescConfig):
extra = Extra.allow
keep_untouched = (cached_property,)

def __init__(self, **kwargs):
def __init__(self, **kwargs) -> None:
super().__init__(**kwargs)
self.set_input_topics()
self.set_output_topics()
Expand Down
20 changes: 19 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ kpops = "kpops.cli.main:app"

[tool.poetry.dependencies]
python = "^3.10"
pydantic = {extras = ["dotenv"], version = "^1.10.8"}
pydantic = { extras = ["dotenv"], version = "^1.10.8" }
rich = "^12.4.4"
PyYAML = "^6.0"
requests = "^2.28.0"
Expand All @@ -50,6 +50,7 @@ flake8 = "^4.0.1"
black = "^22.3.0"
isort = "^5.12.0"
typer-cli = "^0.0.13"
pyright = "^1.1.314"

[tool.poetry.group.docs]
optional = true
Expand Down
4 changes: 3 additions & 1 deletion tests/cli/resources/module.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import Any

from schema_registry.client.schema import AvroSchema

from kpops.component_handlers.schema_handler.schema_provider import (
Schema,
SchemaProvider,
Expand All @@ -8,4 +10,4 @@

class CustomSchemaProvider(SchemaProvider):
def provide_schema(self, schema_class: str, models: dict[str, Any]) -> Schema:
pass
return AvroSchema()
4 changes: 3 additions & 1 deletion tests/component_handlers/schema_handler/resources/module.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import Any

from schema_registry.client.schema import AvroSchema

from kpops.component_handlers.schema_handler.schema_provider import (
Schema,
SchemaProvider,
Expand All @@ -8,4 +10,4 @@

class CustomSchemaProvider(SchemaProvider):
def provide_schema(self, schema_class: str, models: dict[str, Any]) -> Schema:
pass
return AvroSchema()
Loading

0 comments on commit c265ac2

Please sign in to comment.