Skip to content

Commit fe823c5

Browse files
committed
Maintenance: Update to mypy v1.13.0
This patch intends to contribute to unlock upgrading to Python 3.11.
1 parent 806017d commit fe823c5

18 files changed

+80
-44
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ repos:
1818
hooks:
1919
- id: isort
2020
- repo: https://github.com/pre-commit/mirrors-mypy
21-
rev: "v0.981"
21+
rev: "v1.13.0"
2222
hooks:
2323
- id: mypy
2424
additional_dependencies:

crate/operator/config.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,6 @@ def load(self):
192192
f"Invalid {self._prefix}BOOTSTRAP_TIMEOUT="
193193
f"'{bootstrap_timeout}'. Needs to be a positive integer or 0."
194194
)
195-
if self.BOOTSTRAP_TIMEOUT == 0:
196-
self.BOOTSTRAP_TIMEOUT = None
197195

198196
bootstrap_delay = self.env(
199197
"BOOTSTRAP_RETRY_DELAY", default=str(self.BOOTSTRAP_RETRY_DELAY)

crate/operator/cratedb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ async def create_user(
7777
name: str,
7878
username: str,
7979
password: str,
80-
privileges: List[str] = None,
80+
privileges: Optional[List[str]] = None,
8181
) -> None:
8282
"""
8383
Create user ``username`` and grant it given privileges.

crate/operator/handlers/handle_create_cratedb.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ async def create_cratedb(
5656
logger: logging.Logger,
5757
):
5858
context = status.get(CLUSTER_CREATE_ID)
59+
if context is None:
60+
raise RuntimeError(
61+
f"Operation context is empty or unknown: {CLUSTER_CREATE_ID}"
62+
)
5963
hash = hashlib.md5(str(spec).encode("utf-8")).hexdigest()
6064
name = meta["name"]
6165
base_labels = {

crate/operator/operations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1059,7 +1059,7 @@ async def _ensure_no_backup_cronjobs_running(
10591059
):
10601060
await kopf.execute(
10611061
fns={
1062-
"notify_backup_running": subhandler_partial(
1062+
"notify_backup_running": subhandler_partial( # type: ignore[dict-item] # noqa: E501
10631063
self._notify_backup_running, logger
10641064
)
10651065
}

crate/operator/prometheus.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
# with Crate these terms will supersede the license and you may use the
2020
# software solely pursuant to the terms of the relevant commercial agreement.
2121

22+
# mypy: disable-error-code="arg-type, attr-defined, list-item, operator"
23+
2224
import enum
2325
import time
2426
from datetime import datetime

crate/operator/scale.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -515,8 +515,8 @@ async def scale_cluster(
515515
total_number_of_nodes = (
516516
total_number_of_nodes + new_replicas - old_replicas
517517
)
518-
index, *_ = field_path
519-
index = int(index)
518+
index_raw, *_ = field_path
519+
index = int(index_raw)
520520
node_spec = spec["nodes"]["data"][index]
521521
node_name = node_spec["name"]
522522
sts_name = f"crate-data-{node_name}-{name}"
@@ -559,8 +559,8 @@ async def scale_cluster(
559559
total_number_of_nodes = (
560560
total_number_of_nodes + new_replicas - old_replicas
561561
)
562-
index, *_ = field_path
563-
index = int(index)
562+
index_raw, *_ = field_path
563+
index = int(index_raw)
564564
node_spec = spec["nodes"]["data"][index]
565565
node_name = node_spec["name"]
566566
sts_name = f"crate-data-{node_name}-{name}"

crate/operator/utils/kubeapi.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ async def call_kubeapi(
4444
*,
4545
continue_on_absence=False,
4646
continue_on_conflict=False,
47-
namespace: str = None,
48-
body: K8sModel = None,
47+
namespace: Optional[str] = None,
48+
body: Optional[K8sModel] = None,
4949
**kwargs,
5050
) -> Optional[Awaitable[K8sModel]]:
5151
"""

crate/operator/utils/version.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
import re
2323
from distutils.version import Version
24-
from typing import Optional, Tuple
24+
from typing import Optional, Tuple, Union, cast
2525

2626

2727
class CrateVersion(Version):
@@ -63,9 +63,9 @@ class CrateVersion(Version):
6363

6464
version: Optional[Tuple[int, int, int]]
6565

66-
major: int
67-
minor: int
68-
hotfix: int
66+
major: Union[int, None]
67+
minor: Union[int, None]
68+
hotfix: Union[int, None]
6969

7070
stable: bool
7171
snapshot: bool
@@ -130,7 +130,7 @@ def __repr__(self):
130130

131131
def __str__(self):
132132
if self.stable:
133-
return ".".join(map(str, self.version))
133+
return ".".join(map(str, cast(list, self.version)))
134134

135135
if self.nightly:
136136
vstring = "nightly"
@@ -250,14 +250,15 @@ def _parse_regular(self, vstring):
250250
)
251251

252252
if hotfix:
253-
self.version = tuple(map(int, [major, minor, hotfix]))
253+
self.version = tuple(map(int, [major, minor, hotfix])) # type: ignore[assignment] # noqa: E501
254254
else:
255-
self.version = tuple(map(int, [major, minor])) + (0,)
255+
self.version = tuple(map(int, [major, minor])) + (0,) # type: ignore[assignment] # noqa: E501
256256

257257
self.stable = False if snapshot else True
258258
self.nightly = False
259259
self.snapshot = True if snapshot else False
260-
self.major, self.minor, self.hotfix = self.version
260+
if self.version is not None:
261+
self.major, self.minor, self.hotfix = self.version
261262

262263
def _parse_nightly(self, vstring):
263264
match = self.nightly_version_re.match(vstring)
@@ -270,7 +271,7 @@ def _parse_nightly(self, vstring):
270271

271272
if has_version:
272273
self.major, self.minor, self.hotfix = int(major), int(minor), int(hotfix)
273-
self.version = tuple(map(int, [self.major, self.minor, self.hotfix]))
274+
self.version = tuple(map(int, [self.major, self.minor, self.hotfix])) # type: ignore[assignment] # noqa: E501
274275
else:
275276
self.major, self.minor, self.hotfix = None, None, None
276277
self.version = None

pyproject.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,21 @@ target-version = ['py38']
55
requires = ["setuptools>=58", "wheel", "setuptools_scm>=6.2"]
66

77
[tool.setuptools_scm]
8+
9+
[tool.mypy]
10+
packages = ["crate", "tests"]
11+
check_untyped_defs = true
12+
explicit_package_bases = true
13+
ignore_missing_imports = true
14+
implicit_optional = false
15+
install_types = true
16+
namespace_packages = true
17+
non_interactive = true
18+
show_error_codes = true
19+
strict_equality = true
20+
warn_unused_ignores = false
21+
warn_redundant_casts = false
22+
23+
[[tool.mypy.overrides]]
24+
module = "crate.operator.main"
25+
ignore_errors = true

setup.cfg

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
[bdist_wheel]
2-
universal = 1
3-
4-
[mypy]
5-
ignore_missing_imports = true
6-
namespace_packages = true
7-
mypy_path = .
8-
explicit_package_bases = True
9-
101
[flake8]
112
max-line-length = 88
123
ignore = E203 W503

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def read(path: str) -> str:
7777
"black==22.3.0",
7878
"flake8==3.8.4",
7979
"isort==5.12.0",
80-
"mypy==0.981",
80+
"mypy==1.13.0",
8181
],
8282
},
8383
python_requires=">=3.10",

tests/conftest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
# with Crate these terms will supersede the license and you may use the
2020
# software solely pursuant to the terms of the relevant commercial agreement.
2121

22+
# Sphinx configuration file does not need type checking.
23+
# type: ignore
24+
2225
import asyncio
2326
import os
2427
import pathlib

tests/test_create.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ def test_testing_false(self, faker):
128128
with mock.patch("crate.operator.create.config.TESTING", False):
129129
affinity = get_statefulset_affinity(name, logging.getLogger(__name__), {})
130130

131+
assert affinity, "`affinity` is None or empty"
132+
131133
apa = affinity.pod_anti_affinity
132134
terms = apa.required_during_scheduling_ignored_during_execution[0]
133135
expressions = terms.label_selector.match_expressions
@@ -173,6 +175,8 @@ def test_dedicated_resources_affinity(self, node_spec, faker):
173175
name, logging.getLogger(__name__), node_spec
174176
)
175177

178+
assert affinity, "`affinity` is None or empty"
179+
176180
apa = affinity.pod_anti_affinity
177181
terms = apa.required_during_scheduling_ignored_during_execution[0]
178182
expressions = terms.label_selector.match_expressions
@@ -207,6 +211,8 @@ def test_shared_resources_affinity(self, node_spec, faker):
207211
name, logging.getLogger(__name__), node_spec
208212
)
209213

214+
assert affinity, "`affinity` is None or empty"
215+
210216
na = affinity.node_affinity
211217
selector = na.required_during_scheduling_ignored_during_execution
212218
terms = selector.node_selector_terms[0]
@@ -228,6 +234,8 @@ def test_cloud_provider(self, provider, faker):
228234
):
229235
topospread = get_topology_spread(name, logging.getLogger(__name__))
230236

237+
assert topospread, "`topospread` is None or empty"
238+
231239
terms = topospread[0]
232240
expressions = terms.label_selector.match_expressions
233241
assert [e.to_dict() for e in expressions] == [
@@ -279,6 +287,8 @@ def test_dedicated_resources_tolerations(self, node_spec, faker):
279287
with mock.patch("crate.operator.create.config.TESTING", False):
280288
tolerations = get_tolerations(name, logging.getLogger(__name__), node_spec)
281289

290+
assert tolerations, "`tolerations` is None or empty"
291+
282292
assert len(tolerations) == 1
283293
assert tolerations[0].to_dict() == {
284294
"effect": "NoSchedule",
@@ -307,6 +317,8 @@ def test_shared_resources_tolerations(self, node_spec, faker):
307317
with mock.patch("crate.operator.create.config.TESTING", False):
308318
tolerations = get_tolerations(name, logging.getLogger(__name__), node_spec)
309319

320+
assert tolerations, "`tolerations` is None or empty"
321+
310322
toleration = tolerations[0]
311323
expected = {
312324
"key": "cratedb",
@@ -1014,7 +1026,7 @@ def test_get_data_service(self, provider, dns, faker):
10141026
http = faker.port_number()
10151027
psql = faker.port_number()
10161028
with mock.patch("crate.operator.create.config.CLOUD_PROVIDER", provider):
1017-
service = get_data_service(None, name, None, http, psql, dns)
1029+
service = get_data_service(None, name, {}, http, psql, dns)
10181030
annotation_keys = service.metadata.annotations.keys()
10191031
if provider == "aws":
10201032
assert (
@@ -1495,7 +1507,7 @@ async def test_preserve_unknown_object_keys(
14951507

14961508

14971509
def test_sql_exporter_config():
1498-
result = get_sql_exporter_config(None, "test-name", None)
1510+
result = get_sql_exporter_config(None, "test-name", {})
14991511
assert result.metadata.name == "crate-sql-exporter-test-name"
15001512

15011513
_, _, filenames = next(walk("crate/operator/data"))

tests/test_metrics.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
# with Crate these terms will supersede the license and you may use the
2020
# software solely pursuant to the terms of the relevant commercial agreement.
2121

22+
# mypy: disable-error-code="attr-defined, arg-type, union-attr"
23+
2224
import logging
2325
import time
2426

tests/test_update_allowed_cidrs.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ async def _are_source_ranges_updated(core, name, namespace, cidr_list):
193193
ingress = await read_grand_central_ingress(namespace=namespace, name=name)
194194
actual = cidr_list if len(cidr_list) > 0 else None
195195

196+
assert ingress, "`ingress` is None"
197+
196198
return (
197199
service.spec.load_balancer_source_ranges == actual
198200
and ingress.metadata.annotations.get(

tests/test_webhooks.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ def test_payload_serialization_scale():
4848
namespace="some-namespace",
4949
cluster="some-cluster",
5050
scale_data=WebhookScalePayload(
51-
old_data_replicas={"a": 1},
52-
new_data_replicas={"a": 2},
51+
old_data_replicas=[{"name": "a", "replicas": "1"}],
52+
new_data_replicas=[{"name": "a", "replicas": "2"}],
5353
old_master_replicas=3,
5454
new_master_replicas=4,
5555
),
@@ -68,8 +68,8 @@ def test_payload_serialization_scale():
6868
"namespace": "some-namespace",
6969
"cluster": "some-cluster",
7070
"scale_data": {
71-
"old_data_replicas": {"a": 1},
72-
"new_data_replicas": {"a": 2},
71+
"old_data_replicas": [{"name": "a", "replicas": "1"}],
72+
"new_data_replicas": [{"name": "a", "replicas": "2"}],
7373
"old_master_replicas": 3,
7474
"new_master_replicas": 4,
7575
},
@@ -136,8 +136,11 @@ async def test_configure():
136136
assert c._url == "http://localhost:1234/some/path"
137137
assert c._session._default_headers["Content-Type"] == "application/json"
138138
assert c._session._default_headers["User-Agent"].startswith("cratedb-operator/")
139-
assert c._session._default_auth.login == "itsme"
140-
assert c._session._default_auth.password == "secr3t password"
139+
assert c._session._default_auth and c._session._default_auth.login == "itsme"
140+
assert (
141+
c._session._default_auth
142+
and c._session._default_auth.password == "secr3t password"
143+
)
141144

142145

143146
@pytest.mark.asyncio
@@ -173,8 +176,8 @@ async def test_send_scale_notification(self):
173176
"my-cluster",
174177
WebhookEvent.SCALE,
175178
WebhookScalePayload(
176-
old_data_replicas={"a": 1},
177-
new_data_replicas={"a": 2},
179+
old_data_replicas=[{"name": "a", "replicas": "1"}],
180+
new_data_replicas=[{"name": "a", "replicas": "2"}],
178181
old_master_replicas=3,
179182
new_master_replicas=4,
180183
),
@@ -192,8 +195,8 @@ async def test_send_scale_notification(self):
192195
"namespace": "my-namespace",
193196
"cluster": "my-cluster",
194197
"scale_data": {
195-
"old_data_replicas": {"a": 1},
196-
"new_data_replicas": {"a": 2},
198+
"old_data_replicas": [{"name": "a", "replicas": "1"}],
199+
"new_data_replicas": [{"name": "a", "replicas": "2"}],
197200
"old_master_replicas": 3,
198201
"new_master_replicas": 4,
199202
},

tests/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import logging
2424
import os
2525
from functools import reduce
26-
from typing import Any, Callable, List, Mapping, Optional, Set, Tuple, Union
26+
from typing import Any, Callable, Dict, List, Mapping, Optional, Set, Tuple
2727
from unittest import mock
2828

2929
import psycopg2
@@ -484,7 +484,7 @@ async def mocked_coro_func_called_with(
484484
async def cluster_setting_equals(
485485
conn_factory: Callable[[], Connection],
486486
setting: str,
487-
expected_value: Union[str, int],
487+
expected_value: Dict[Any, Any],
488488
) -> bool:
489489
try:
490490
async with conn_factory() as conn:

0 commit comments

Comments
 (0)