Skip to content

Commit 6af4e6f

Browse files
fangnxMSeal
andauthored
Add mypy type checking to CI + upgrade to Python 3.11 in CI (#2123)
* ci * add types-requests dep * pin boto3 * pin requests * pin boto3 for now * pin httpcore * pin more to try * try pip upgrade * add constraints back * upgrade to python 3.11 * cleanup ; * upgrade semaphore builds to use 3.11 * one new type error * whitespace * Resolving setup issues * Resolved all type hinting issues locally * Applied unasync * Resolved mypy failures * Flake8 fix plus print for clang-format version * Reverting admin.c to clang-format 18 variant * Resolved new avro type checks * Added mypy ignore for celpy calls * Upped memory limit as mypy increased the overhead slightly * Bumped memory for mypy overhead * Adding file missed in prior commit --------- Co-authored-by: Matthew Seal <mseal@confluent.io>
1 parent 728e6da commit 6af4e6f

File tree

17 files changed

+68
-29
lines changed

17 files changed

+68
-29
lines changed

.semaphore/semaphore.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,14 @@ blocks:
197197
jobs:
198198
- name: Build and Tests with 'classic' group protocol
199199
commands:
200-
- sem-version python 3.9
200+
- sem-version python 3.11
201201
# use a virtualenv
202202
- python3 -m venv _venv && source _venv/bin/activate
203203
- chmod u+r+x tools/source-package-verification.sh
204204
- tools/source-package-verification.sh
205205
- name: Build and Tests with 'consumer' group protocol
206206
commands:
207-
- sem-version python 3.9
207+
- sem-version python 3.11
208208
- sem-version java 17
209209
# use a virtualenv
210210
- python3 -m venv _venv && source _venv/bin/activate
@@ -213,7 +213,7 @@ blocks:
213213
- tools/source-package-verification.sh
214214
- name: Build, Test, and Report coverage
215215
commands:
216-
- sem-version python 3.9
216+
- sem-version python 3.11
217217
# use a virtualenv
218218
- python3 -m venv _venv && source _venv/bin/activate
219219
- chmod u+r+x tools/source-package-verification.sh
@@ -237,7 +237,7 @@ blocks:
237237
jobs:
238238
- name: Build
239239
commands:
240-
- sem-version python 3.9
240+
- sem-version python 3.11
241241
# use a virtualenv
242242
- python3 -m venv _venv && source _venv/bin/activate
243243
- chmod u+r+x tools/source-package-verification.sh
@@ -256,7 +256,7 @@ blocks:
256256
jobs:
257257
- name: Build
258258
commands:
259-
- sem-version python 3.9
259+
- sem-version python 3.11
260260
# use a virtualenv
261261
- python3 -m venv _venv && source _venv/bin/activate
262262
- chmod u+r+x tools/source-package-verification.sh
@@ -275,7 +275,7 @@ blocks:
275275
jobs:
276276
- name: Build
277277
commands:
278-
- sem-version python 3.9
278+
- sem-version python 3.11
279279
# use a virtualenv
280280
- python3 -m venv _venv && source _venv/bin/activate
281281
- chmod u+r+x tools/source-package-verification.sh
@@ -302,7 +302,7 @@ blocks:
302302
- name: Build and Tests
303303
commands:
304304
# Setup Python environment
305-
- sem-version python 3.9
305+
- sem-version python 3.11
306306
- python3 -m venv _venv && source _venv/bin/activate
307307

308308
# Install ducktape framework and additional dependencies

DEVELOPER.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,11 @@ tox -e black,isort
212212
# Check linting
213213
tox -e flake8
214214
215+
# Check typing
216+
tox -e mypy
217+
215218
# Run all formatting and linting checks
216-
tox -e black,isort,flake8
219+
tox -e black,isort,flake8,mypy
217220
```
218221
219222
## Documentation build

MANIFEST.in

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
include README.md
22
include LICENSE
33
include src/confluent_kafka/src/*.[ch]
4+
include src/confluent_kafka/py.typed
5+
include src/confluent_kafka/cimpl.pyi
46
prune tests
5-
prune docs
7+
prune docs

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ all:
22
@echo "Targets:"
33
@echo " clean"
44
@echo " docs"
5+
@echo " mypy"
6+
@echo " style-check"
7+
@echo " style-fix"
58

69

710
clean:
@@ -14,6 +17,9 @@ clean:
1417
docs:
1518
$(MAKE) -C docs html
1619

20+
mypy:
21+
python3 -m mypy src/confluent_kafka
22+
1723
style-check:
1824
@(tools/style-format.sh \
1925
$$(git ls-tree -r --name-only HEAD | egrep '\.(c|h|py)$$') )

requirements/requirements-avro.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
fastavro < 1.8.0; python_version == "3.7"
22
fastavro < 2; python_version > "3.7"
33
requests
4-
avro>=1.11.1,<2
4+
avro>=1.11.1,<2

requirements/requirements-tests.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
urllib3<3
33
flake8
44
mypy
5+
attrs
56
types-cachetools
7+
types-requests
68
orjson
79
pytest
810
pytest-timeout

src/confluent_kafka/deserializing_consumer.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,11 @@ def poll(self, timeout: float = -1) -> Optional[Message]:
105105
if error is not None:
106106
raise ConsumeError(error, kafka_message=msg)
107107

108-
ctx = SerializationContext(msg.topic(), MessageField.VALUE, msg.headers())
108+
topic = msg.topic()
109+
if topic is None:
110+
raise TypeError("Message topic is None")
111+
ctx = SerializationContext(topic, MessageField.VALUE, msg.headers())
112+
109113
value = msg.value()
110114
if self._value_deserializer is not None:
111115
try:

src/confluent_kafka/schema_registry/common/avro.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from collections import defaultdict
66
from copy import deepcopy
77
from io import BytesIO
8-
from typing import Dict, Optional, Set, Tuple, Union
8+
from typing import Dict, Optional, Set, Tuple, Union, cast
99

1010
from fastavro import repository, validate
1111
from fastavro.schema import load_schema
@@ -217,11 +217,17 @@ def _resolve_union(schema: AvroSchema, message: AvroMessage) -> Tuple[Optional[A
217217
for subschema in schema:
218218
try:
219219
if is_wrapped_union:
220-
if isinstance(subschema, dict) and subschema["name"] == message[0]:
221-
return (subschema, message[1])
220+
if isinstance(subschema, dict):
221+
dict_schema = cast(dict, subschema)
222+
tuple_message = cast(tuple, message)
223+
if dict_schema["name"] == tuple_message[0]:
224+
return (dict_schema, tuple_message[1])
222225
elif is_typed_union:
223-
if isinstance(subschema, dict) and subschema["name"] == message['-type']:
224-
return (subschema, message)
226+
if isinstance(subschema, dict):
227+
dict_schema = cast(dict, subschema)
228+
dict_message = cast(dict, message)
229+
if dict_schema["name"] == dict_message['-type']:
230+
return (dict_schema, dict_message)
225231
else:
226232
validate(message, subschema)
227233
return (subschema, message)

src/confluent_kafka/schema_registry/rules/cel/string_format.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ def __init__(self, locale: str):
4444
def format(self, fmt: celtypes.Value, args: celtypes.Value) -> celpy.Result:
4545
if not isinstance(fmt, celtypes.StringType):
4646
return celpy.native_to_cel( # type: ignore[attr-defined]
47-
celpy.new_error("format() requires a string as the first argument")
47+
celpy.new_error("format() requires a string as the first argument") # type: ignore[attr-defined]
4848
) # type: ignore[attr-defined]
4949
if not isinstance(args, celtypes.ListType):
5050
return celpy.native_to_cel( # type: ignore[attr-defined]
51-
celpy.new_error("format() requires a list as the second argument")
51+
celpy.new_error("format() requires a list as the second argument") # type: ignore[attr-defined]
5252
) # type: ignore[attr-defined]
5353
# printf style formatting
5454
i = 0

src/confluent_kafka/schema_registry/rules/encryption/encrypt_executor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ def _is_expired(self, ctx: RuleContext, dek: Optional[Dek]) -> bool:
365365
ctx.rule_mode != RuleMode.READ
366366
and self._dek_expiry_days > 0
367367
and dek is not None
368-
and (now - dek.ts) / MILLIS_IN_DAY > self._dek_expiry_days
368+
and (now - (dek.ts or 0)) / MILLIS_IN_DAY > self._dek_expiry_days
369369
) # type: ignore[operator]
370370

371371
def transform(self, ctx: RuleContext, field_type: FieldType, field_value: Any) -> Any:

0 commit comments

Comments
 (0)