apache / pulsar-client-python

Apache Pulsar Python client library
https://pulsar.apache.org/
Apache License 2.0
51 stars 40 forks source link

[Bug] schema_avro.py module's exception when fast avro not installed #89

Closed keenborder786 closed 1 year ago

keenborder786 commented 1 year ago

The exception raised for AvroSchema class when there is no Avro Library Support has a strange issue:


from pulsar.schema import AvroSchema
from schema_test import final_account_payload_schema,account_schema,source_schema
import json
import requests
import pulsar

client = pulsar.Client('pulsar://localhost:6650')
producer = client.create_producer('data-test' , schema = AvroSchema(final_account_payload_schema))
# The below code becomes inaccessible.
after_vals = account_schema(id_value = '21' , id_set = True , txn_code_value ='1231' , txn_code_set = True , from_account_value = '12321' , from_account_set = True , amount_value = 123 , amount_set = True)
source_vals = source_schema(version = '213' , connector = '12321' , name = '12312' , ts_ms = 123213214321)
test_val = final_account_payload_schema(before = None , after = after_vals , source = source_vals ,
op = 'c' , ts_ms = 123213124 , transaction = 'XXXX78181')
producer.send(test_val)
client.close()

However, If I change the raised exception in schema_avro.py to the following then the code becomes accessible:

if HAS_AVRO:
   ## Same Code
else:
    raise Exception("Avro library support was not found. Make sure to install Pulsar client " +
                            "with Avro support: pip3 install 'pulsar-client[avro]'")

For reference, this is how the current exception is:

if HAS_AVRO:
   ## Same Code
else:
     class AvroSchema(Schema):
          def __init__(self, _record_cls, _schema_definition=None):
              raise Exception("Avro library support was not found. Make sure to install Pulsar client " +
                              "with Avro support: pip3 install 'pulsar-client[avro]'")

          def encode(self, obj):
              pass

          def decode(self, data):
              pass

As it is clear that instead of raising an exception in the initialization of a class, I am just raising the exception as it is and now my entire code is accessible.

keenborder786 commented 1 year ago

Created a PR for the issue #90

tisonkun commented 1 year ago

Closed. From the PR discussion it seems by design.