Skip to content

Commit b4566fb

Browse files
authored
Add stubs for kafka-python (#15817)
1 parent 526a7dd commit b4566fb

135 files changed

Lines changed: 5556 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

pyrightconfig.stricter.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"stubs/icalendar/icalendar/timezone/provider.pyi",
5757
"stubs/jsonschema",
5858
"stubs/jwcrypto",
59+
"stubs/kafka-python",
5960
"stubs/ldap3",
6061
"stubs/m3u8/m3u8/model.pyi",
6162
"stubs/Markdown",
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Command-line entry points are not a useful typed API surface.
2+
kafka.__main__
3+
kafka.admin.__main__
4+
kafka.consumer.__main__
5+
kafka.producer.__main__
6+
7+
# Benchmark modules are not included in type stubs.
8+
kafka.benchmarks.*
9+
10+
# Concrete subclasses define these abstract properties as class attributes.
11+
kafka.protocol.api.Request.API_KEY
12+
kafka.protocol.api.Request.API_VERSION
13+
kafka.protocol.api.Request.RESPONSE_TYPE
14+
kafka.protocol.api.Request.SCHEMA
15+
kafka.protocol.api.Response.API_KEY
16+
kafka.protocol.api.Response.API_VERSION
17+
kafka.protocol.api.Response.SCHEMA
18+
19+
# Vendored compatibility modules are implementation details.
20+
kafka.vendor
21+
kafka.vendor.enum34
22+
kafka.vendor.selectors34
23+
kafka.vendor.six
24+
kafka.vendor.socketpair
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# The bytes subclass object layout differs by platform/Python build.
2+
kafka.protocol.message.PartialMessage

stubs/kafka-python/METADATA.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
version = "2.3.*"
2+
upstream-repository = "https://github.com/dpkp/kafka-python"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import logging
2+
3+
from kafka.admin import KafkaAdminClient as KafkaAdminClient
4+
from kafka.client_async import KafkaClient as KafkaClient
5+
from kafka.conn import BrokerConnection as BrokerConnection
6+
from kafka.consumer import KafkaConsumer as KafkaConsumer
7+
from kafka.consumer.subscription_state import ConsumerRebalanceListener as ConsumerRebalanceListener
8+
from kafka.producer import KafkaProducer as KafkaProducer
9+
10+
__all__ = ["BrokerConnection", "ConsumerRebalanceListener", "KafkaAdminClient", "KafkaClient", "KafkaConsumer", "KafkaProducer"]
11+
12+
class NullHandler(logging.Handler):
13+
def emit(self, record) -> None: ...
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from kafka.admin.acl_resource import (
2+
ACL as ACL,
3+
ACLFilter as ACLFilter,
4+
ACLOperation as ACLOperation,
5+
ACLPermissionType as ACLPermissionType,
6+
ACLResourcePatternType as ACLResourcePatternType,
7+
ResourcePattern as ResourcePattern,
8+
ResourcePatternFilter as ResourcePatternFilter,
9+
ResourceType as ResourceType,
10+
)
11+
from kafka.admin.client import KafkaAdminClient as KafkaAdminClient
12+
from kafka.admin.config_resource import ConfigResource as ConfigResource, ConfigResourceType as ConfigResourceType
13+
from kafka.admin.new_partitions import NewPartitions as NewPartitions
14+
from kafka.admin.new_topic import NewTopic as NewTopic
15+
16+
__all__ = [
17+
"ConfigResource",
18+
"ConfigResourceType",
19+
"KafkaAdminClient",
20+
"NewTopic",
21+
"NewPartitions",
22+
"ACL",
23+
"ACLFilter",
24+
"ResourcePattern",
25+
"ResourcePatternFilter",
26+
"ACLOperation",
27+
"ResourceType",
28+
"ACLPermissionType",
29+
"ACLResourcePatternType",
30+
]
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
from enum import IntEnum
2+
3+
class ResourceType(IntEnum):
4+
UNKNOWN = 0
5+
ANY = 1
6+
CLUSTER = 4
7+
DELEGATION_TOKEN = 6
8+
GROUP = 3
9+
TOPIC = 2
10+
TRANSACTIONAL_ID = 5
11+
12+
class ACLOperation(IntEnum):
13+
UNKNOWN = 0
14+
ANY = 1
15+
ALL = 2
16+
READ = 3
17+
WRITE = 4
18+
CREATE = 5
19+
DELETE = 6
20+
ALTER = 7
21+
DESCRIBE = 8
22+
CLUSTER_ACTION = 9
23+
DESCRIBE_CONFIGS = 10
24+
ALTER_CONFIGS = 11
25+
IDEMPOTENT_WRITE = 12
26+
CREATE_TOKENS = 13
27+
DESCRIBE_TOKENS = 13
28+
29+
class ACLPermissionType(IntEnum):
30+
UNKNOWN = 0
31+
ANY = 1
32+
DENY = 2
33+
ALLOW = 3
34+
35+
class ACLResourcePatternType(IntEnum):
36+
UNKNOWN = 0
37+
ANY = 1
38+
MATCH = 2
39+
LITERAL = 3
40+
PREFIXED = 4
41+
42+
class ACLFilter:
43+
principal: str | None
44+
host: str | None
45+
operation: ACLOperation
46+
permission_type: ACLPermissionType
47+
resource_pattern: ResourcePatternFilter
48+
def __init__(
49+
self,
50+
principal: str | None,
51+
host: str | None,
52+
operation: ACLOperation,
53+
permission_type: ACLPermissionType,
54+
resource_pattern: ResourcePatternFilter,
55+
) -> None: ...
56+
def validate(self) -> None: ...
57+
def __eq__(self, other): ...
58+
def __hash__(self): ...
59+
60+
class ACL(ACLFilter):
61+
resource_pattern: ResourcePattern
62+
def __init__(
63+
self,
64+
principal: str,
65+
host: str,
66+
operation: ACLOperation,
67+
permission_type: ACLPermissionType,
68+
resource_pattern: ResourcePattern,
69+
) -> None: ...
70+
def validate(self) -> None: ...
71+
72+
class ResourcePatternFilter:
73+
resource_type: ResourceType
74+
resource_name: str | None
75+
pattern_type: ACLResourcePatternType
76+
def __init__(self, resource_type: ResourceType, resource_name: str | None, pattern_type: ACLResourcePatternType) -> None: ...
77+
def validate(self) -> None: ...
78+
def __eq__(self, other): ...
79+
def __hash__(self): ...
80+
81+
class ResourcePattern(ResourcePatternFilter):
82+
resource_name: str
83+
def __init__(
84+
self,
85+
resource_type: ResourceType,
86+
resource_name: str,
87+
pattern_type: ACLResourcePatternType = ACLResourcePatternType.LITERAL,
88+
) -> None: ...
89+
def validate(self) -> None: ...
90+
91+
def valid_acl_operations(int_vals) -> set[ACLOperation]: ...
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import selectors
2+
import ssl
3+
from _typeshed import Incomplete
4+
from collections.abc import Callable, Iterable, Mapping, Sequence
5+
from typing import Literal, TypeAlias, TypedDict, type_check_only
6+
from typing_extensions import Unpack
7+
8+
from kafka.admin.acl_resource import ACL, ACLFilter
9+
from kafka.admin.config_resource import ConfigResource
10+
from kafka.admin.new_partitions import NewPartitions
11+
from kafka.admin.new_topic import NewTopic
12+
from kafka.errors import KafkaError
13+
from kafka.protocol.admin import ElectionType
14+
from kafka.structs import GroupInformation, OffsetAndMetadata, TopicPartition
15+
16+
_ApiVersion: TypeAlias = tuple[int, ...]
17+
_BootstrapServers: TypeAlias = str | Sequence[str]
18+
_KafkaClientFactory: TypeAlias = Callable[..., object]
19+
_SaslMechanism: TypeAlias = Literal["PLAIN", "GSSAPI", "OAUTHBEARER", "SCRAM-SHA-256", "SCRAM-SHA-512"]
20+
_SecurityProtocol: TypeAlias = Literal["PLAINTEXT", "SSL", "SASL_PLAINTEXT", "SASL_SSL"]
21+
_SocketOption: TypeAlias = tuple[int, int, int]
22+
23+
@type_check_only
24+
class _KafkaAdminClientConfig(TypedDict, total=False):
25+
bootstrap_servers: _BootstrapServers
26+
client_id: str
27+
request_timeout_ms: int
28+
connections_max_idle_ms: int
29+
reconnect_backoff_ms: int
30+
reconnect_backoff_max_ms: int
31+
max_in_flight_requests_per_connection: int
32+
receive_buffer_bytes: int | None
33+
send_buffer_bytes: int | None
34+
socket_options: Sequence[_SocketOption]
35+
sock_chunk_bytes: int
36+
sock_chunk_buffer_count: int
37+
retry_backoff_ms: int
38+
metadata_max_age_ms: int
39+
security_protocol: _SecurityProtocol
40+
ssl_context: ssl.SSLContext | None
41+
ssl_check_hostname: bool
42+
ssl_cafile: str | None
43+
ssl_certfile: str | None
44+
ssl_keyfile: str | None
45+
ssl_password: str | None
46+
ssl_crlfile: str | None
47+
api_version: _ApiVersion | None
48+
api_version_auto_timeout_ms: int
49+
selector: type[selectors.BaseSelector]
50+
sasl_mechanism: _SaslMechanism | None
51+
sasl_plain_username: str | None
52+
sasl_plain_password: str | None
53+
sasl_kerberos_name: object | None
54+
sasl_kerberos_service_name: str
55+
sasl_kerberos_domain_name: str | None
56+
sasl_oauth_token_provider: object | None
57+
socks5_proxy: str | None
58+
metric_reporters: Sequence[type[object]]
59+
metrics_num_samples: int
60+
metrics_sample_window_ms: int
61+
kafka_client: _KafkaClientFactory
62+
63+
@type_check_only
64+
class _CreateAclsResult(TypedDict):
65+
succeeded: list[ACL]
66+
failed: list[tuple[ACL, KafkaError]]
67+
68+
log: Incomplete
69+
70+
class KafkaAdminClient:
71+
DEFAULT_CONFIG: Incomplete
72+
config: Incomplete
73+
def __init__(self, **configs: Unpack[_KafkaAdminClientConfig]) -> None: ...
74+
def close(self) -> None: ...
75+
def send_request(self, request, node_id=None): ...
76+
def send_requests(self, requests_and_node_ids, response_fn=...): ...
77+
def create_topics(self, new_topics: Sequence[NewTopic], timeout_ms: int | None = None, validate_only: bool = False): ...
78+
def delete_topics(self, topics: Sequence[str], timeout_ms: int | None = None): ...
79+
def list_topics(self) -> list[str]: ...
80+
def describe_topics(self, topics: Sequence[str] | None = None) -> list[dict[str, Incomplete]]: ...
81+
def describe_cluster(self) -> dict[str, Incomplete]: ...
82+
def describe_acls(self, acl_filter: ACLFilter) -> tuple[list[ACL], KafkaError]: ...
83+
def create_acls(self, acls: Sequence[ACL]) -> _CreateAclsResult: ...
84+
def delete_acls(
85+
self, acl_filters: Sequence[ACLFilter]
86+
) -> list[tuple[ACLFilter, list[tuple[ACL, KafkaError]], KafkaError]]: ...
87+
def describe_configs(self, config_resources: Sequence[ConfigResource], include_synonyms: bool = False): ...
88+
def alter_configs(self, config_resources: Sequence[ConfigResource]): ...
89+
def create_partitions(
90+
self, topic_partitions: Mapping[str, NewPartitions], timeout_ms: int | None = None, validate_only: bool = False
91+
): ...
92+
def delete_records(
93+
self,
94+
records_to_delete: Mapping[TopicPartition, int],
95+
timeout_ms: float | None = None,
96+
partition_leader_id: int | None = None,
97+
) -> dict[TopicPartition, Incomplete]: ...
98+
def describe_consumer_groups(
99+
self, group_ids: Sequence[str], group_coordinator_id: int | None = None, include_authorized_operations: bool = False
100+
) -> list[GroupInformation]: ...
101+
def list_consumer_groups(self, broker_ids: Sequence[int] | None = None) -> list[tuple[str, str]]: ...
102+
def list_consumer_group_offsets(
103+
self, group_id: str, group_coordinator_id: int | None = None, partitions: Iterable[TopicPartition] | None = None
104+
) -> dict[TopicPartition, OffsetAndMetadata]: ...
105+
def delete_consumer_groups(
106+
self, group_ids: Sequence[str], group_coordinator_id: int | None = None
107+
) -> list[tuple[str, KafkaError]]: ...
108+
def perform_leader_election(
109+
self,
110+
election_type: int | ElectionType,
111+
topic_partitions: Mapping[str, Sequence[int]] | None = None,
112+
timeout_ms: int | None = None,
113+
): ...
114+
def describe_log_dirs(self): ...
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from collections.abc import Mapping
2+
from enum import IntEnum
3+
4+
class ConfigResourceType(IntEnum):
5+
BROKER = 4
6+
TOPIC = 2
7+
8+
class ConfigResource:
9+
resource_type: ConfigResourceType
10+
name: str
11+
configs: Mapping[str, str] | None
12+
def __init__(self, resource_type: ConfigResourceType, name: str, configs: Mapping[str, str] | None = None) -> None: ...
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from collections.abc import Sequence
2+
3+
class NewPartitions:
4+
total_count: int
5+
new_assignments: Sequence[Sequence[int]] | None
6+
def __init__(self, total_count: int, new_assignments: Sequence[Sequence[int]] | None = None) -> None: ...

0 commit comments

Comments
 (0)