@@ -608,11 +608,17 @@ def commit_async(self, offsets=None, callback=None):
608608 struct. This callback can be used to trigger custom actions when
609609 a commit request completes.
610610
611+ Raises:
612+ IncompatibleBrokerVersion: if broker version < 0.8.1
613+ IllegalStateError: if group_id is None
614+
611615 Returns:
612616 kafka.future.Future
613617 """
614- assert self .config ['api_version' ] >= (0 , 8 , 1 ), 'Requires >= Kafka 0.8.1'
615- assert self .config ['group_id' ] is not None , 'Requires group_id'
618+ if self .config ['api_version' ] < (0 , 8 , 1 ):
619+ raise Errors .IncompatibleBrokerVersion ('Requires >= Kafka 0.8.1' )
620+ if self .config ['group_id' ] is None :
621+ raise Errors .IllegalStateError ('Requires group_id' )
616622 if offsets is None :
617623 offsets = self ._subscription .all_consumed_offsets ()
618624 log .debug ("Committing offsets: %s" , offsets )
@@ -639,9 +645,15 @@ def commit(self, offsets=None, timeout_ms=None):
639645 offsets (dict, optional): {TopicPartition: OffsetAndMetadata} dict
640646 to commit with the configured group_id. Defaults to currently
641647 consumed offsets for all subscribed partitions.
648+
649+ Raises:
650+ IncompatibleBrokerVersion: if broker version < 0.8.1
651+ IllegalStateError: if group_id is None
642652 """
643- assert self .config ['api_version' ] >= (0 , 8 , 1 ), 'Requires >= Kafka 0.8.1'
644- assert self .config ['group_id' ] is not None , 'Requires group_id'
653+ if self .config ['api_version' ] < (0 , 8 , 1 ):
654+ raise Errors .IncompatibleBrokerVersion ('Requires >= Kafka 0.8.1' )
655+ if self .config ['group_id' ] is None :
656+ raise Errors .IllegalStateError ('Requires group_id' )
645657 if offsets is None :
646658 offsets = self ._subscription .all_consumed_offsets ()
647659 self ._coordinator .commit_offsets_sync (offsets , timeout_ms = timeout_ms )
@@ -680,11 +692,16 @@ def committed(self, partition, metadata=False, timeout_ms=None):
680692 The last committed offset (int or OffsetAndMetadata), or None if there was no prior commit.
681693
682694 Raises:
683- KafkaTimeoutError if timeout_ms provided
684- BrokerResponseErrors if OffsetFetchRequest raises an error.
695+ IncompatibleBrokerVersion: if broker version < 0.8.1
696+ IllegalStateError: if group_id is None
697+ TypeError: if partition is not TopicPartition
698+ KafkaTimeoutError: if timeout_ms provided
699+ BrokerResponseError: if OffsetFetchRequest raises an error.
685700 """
686- assert self .config ['api_version' ] >= (0 , 8 , 1 ), 'Requires >= Kafka 0.8.1'
687- assert self .config ['group_id' ] is not None , 'Requires group_id'
701+ if self .config ['api_version' ] < (0 , 8 , 1 ):
702+ raise Errors .IncompatibleBrokerVersion ('Requires >= Kafka 0.8.1' )
703+ if self .config ['group_id' ] is None :
704+ raise Errors .IllegalStateError ('Requires group_id' )
688705 if not isinstance (partition , TopicPartition ):
689706 raise TypeError ('partition must be a TopicPartition namedtuple' )
690707 committed = self ._coordinator .fetch_committed_offsets ([partition ], timeout_ms = timeout_ms )
@@ -755,6 +772,11 @@ def poll(self, timeout_ms=0, max_records=None, update_offsets=True):
755772 in a single call to :meth:`~kafka.KafkaConsumer.poll`.
756773 Default: Inherit value from max_poll_records.
757774
775+ Raises:
776+ ValueError: if timeout is < 0 or max_records <= 0.
777+ TypeError: if max_records is not int.
778+ IllegalStateError: if consumer already closed.
779+
758780 Returns:
759781 dict[TopicPartition, list[ConsumerRecord]]: records since the last
760782 fetch for the subscribed list of topics and partitions.
@@ -765,12 +787,16 @@ def poll(self, timeout_ms=0, max_records=None, update_offsets=True):
765787 # updated until the iterator returns each record to the user. As such,
766788 # the argument is not documented and should not be relied on by library
767789 # users to not break in the future.
768- assert timeout_ms >= 0 , 'Timeout must not be negative'
790+ if timeout_ms < 0 :
791+ raise ValueError ('Timeout must not be negative' )
769792 if max_records is None :
770793 max_records = self .config ['max_poll_records' ]
771- assert isinstance (max_records , int ), 'max_records must be an integer'
772- assert max_records > 0 , 'max_records must be positive'
773- assert not self ._closed , 'KafkaConsumer is closed'
794+ if not isinstance (max_records , int ):
795+ raise TypeError ('max_records must be an integer' )
796+ if max_records <= 0 :
797+ raise ValueError ('max_records must be positive' )
798+ if self ._closed :
799+ raise Errors .IllegalStateError ('KafkaConsumer is closed' )
774800
775801 # Poll for new data until the timeout expires
776802 timer = Timer (timeout_ms )
@@ -830,12 +856,17 @@ def position(self, partition, timeout_ms=None):
830856 Arguments:
831857 partition (TopicPartition): Partition to check
832858
859+ Raises:
860+ TypeError: if partition is not a TopicPartition.
861+ IllegalStateError: if partition is not assigned.
862+
833863 Returns:
834864 int: Offset or None
835865 """
836866 if not isinstance (partition , TopicPartition ):
837867 raise TypeError ('partition must be a TopicPartition namedtuple' )
838- assert self ._subscription .is_assigned (partition ), 'Partition is not assigned'
868+ if not self ._subscription .is_assigned (partition ):
869+ raise Errors .IllegalStateError ('Partition is not assigned' )
839870
840871 timer = Timer (timeout_ms )
841872 # Phase 1: blocking refresh of committed offsets (network round-trip
@@ -883,12 +914,17 @@ def highwater(self, partition):
883914 Arguments:
884915 partition (TopicPartition): Partition to check
885916
917+ Raises:
918+ TypeError: if partition is not a TopicPartition.
919+ IllegalStateError: if partition is not assigned.
920+
886921 Returns:
887922 int or None: Offset if available
888923 """
889924 if not isinstance (partition , TopicPartition ):
890925 raise TypeError ('partition must be a TopicPartition namedtuple' )
891- assert self ._subscription .is_assigned (partition ), 'Partition is not assigned'
926+ if not self ._subscription .is_assigned (partition ):
927+ raise Errors .IllegalStateError ('Partition is not assigned' )
892928 return self ._subscription .assignment [partition ].highwater
893929
894930 def pause (self , * partitions ):
@@ -950,13 +986,15 @@ def seek(self, partition, offset):
950986 offset (int): Message offset in partition
951987
952988 Raises:
953- AssertionError : If offset is not an int >= 0; or if partition is not
989+ ValueError : If offset is not an int >= 0; or if partition is not
954990 currently assigned.
955991 """
956992 if not isinstance (partition , TopicPartition ):
957993 raise TypeError ('partition must be a TopicPartition namedtuple' )
958- assert isinstance (offset , int ) and offset >= 0 , 'Offset must be >= 0'
959- assert partition in self ._subscription .assigned_partitions (), 'Unassigned partition'
994+ if not isinstance (offset , int ) or offset < 0 :
995+ raise ValueError ('Offset must be int >= 0' )
996+ if partition not in self ._subscription .assigned_partitions ():
997+ raise ValueError ('Unassigned partition' )
960998 log .debug ("Seeking to offset %s for partition %s" , offset , partition )
961999 self ._subscription .assignment [partition ].seek (offset )
9621000 self ._iterator = None
@@ -969,17 +1007,19 @@ def seek_to_beginning(self, *partitions):
9691007 default to all assigned partitions.
9701008
9711009 Raises:
972- AssertionError : If any partition is not currently assigned, or if
1010+ ValueError : If any partition is not currently assigned, or if
9731011 no partitions are assigned.
9741012 """
9751013 if not all ([isinstance (p , TopicPartition ) for p in partitions ]):
9761014 raise TypeError ('partitions must be TopicPartition namedtuples' )
9771015 if not partitions :
9781016 partitions = self ._subscription .assigned_partitions ()
979- assert partitions , 'No partitions are currently assigned'
1017+ if not partitions :
1018+ raise ValueError ('No partitions are currently assigned' )
9801019 else :
9811020 for p in partitions :
982- assert p in self ._subscription .assigned_partitions (), 'Unassigned partition'
1021+ if p not in self ._subscription .assigned_partitions ():
1022+ raise ValueError ('Unassigned partition: %s' % (p ,))
9831023
9841024 for tp in partitions :
9851025 log .debug ("Seeking to beginning of partition %s" , tp )
@@ -994,17 +1034,19 @@ def seek_to_end(self, *partitions):
9941034 default to all assigned partitions.
9951035
9961036 Raises:
997- AssertionError : If any partition is not currently assigned, or if
1037+ ValueError : If any partition is not currently assigned, or if
9981038 no partitions are assigned.
9991039 """
10001040 if not all ([isinstance (p , TopicPartition ) for p in partitions ]):
10011041 raise TypeError ('partitions must be TopicPartition namedtuples' )
10021042 if not partitions :
10031043 partitions = self ._subscription .assigned_partitions ()
1004- assert partitions , 'No partitions are currently assigned'
1044+ if not partitions :
1045+ raise ValueError ('No partitions are currently assigned' )
10051046 else :
10061047 for p in partitions :
1007- assert p in self ._subscription .assigned_partitions (), 'Unassigned partition'
1048+ if p not in self ._subscription .assigned_partitions ():
1049+ raise ValueError ('Unassigned partition: %s' % (p ,))
10081050
10091051 for tp in partitions :
10101052 log .debug ("Seeking to end of partition %s" , tp )
@@ -1049,7 +1091,7 @@ def subscribe(self, topics=(), pattern=None, listener=None):
10491091 Raises:
10501092 IllegalStateError: If called after previously calling
10511093 :meth:`~kafka.KafkaConsumer.assign`.
1052- AssertionError : If neither topics or pattern is provided.
1094+ ValueError : If neither topics or pattern is provided.
10531095 TypeError: If listener is not a ConsumerRebalanceListener.
10541096 """
10551097 # SubscriptionState handles error checking
0 commit comments