-
Notifications
You must be signed in to change notification settings - Fork 915
Fix bare except #279
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix bare except #279
Conversation
975a407
to
d8b3eda
Compare
# bad schema - should not happen | ||
raise ClientError("Received bad schema from registry.") | ||
raise ClientError("Received bad schema (id %s) from registry: %s" % (schema_id, str(e))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why cast to e
to str
? Why not rely on the implicit conversion?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To favour __str__
over __repr__
, but I guess __str__
kicks in here thanks to %s anyway
@@ -269,7 +270,8 @@ def test_compatibility(self, subject, avro_schema, version='latest'): | |||
else: | |||
log.error("Unable to check the compatibility") | |||
False | |||
except: | |||
except: # noqa |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't you at least restrict this to except Exception:
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's what I thought too, until I found this:
In situations where you need to catch all “normal” errors, such as in a framework that runs callbacks, you can catch the base class for all normal exceptions, Exception. Unfortunately in Python 2.x it is possible for third-party code to raise exceptions that do not inherit from Exception, so in Python 2.x there are some cases where you may have to use a bare except: and manually re-raise the exceptions you don’t want to catch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are the exceptions being caught here from the app code or from this library (and other open-source libraries)?
If app code, that makes sense, but if from libraries, than I don't think it makes sense as generally most open source libs are well-behaved and if it is an issue, maintainers are generally very open to cleaning this up.
@@ -269,7 +270,8 @@ def test_compatibility(self, subject, avro_schema, version='latest'): | |||
else: | |||
log.error("Unable to check the compatibility") | |||
False | |||
except: | |||
except: # noqa | |||
log.error("_send_request() failed: %s" % sys.exc_info()[0]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Best practice is to use the logging
framework's built-in string interpolation:
log.level("string %s", arg1)
That way the string isn't built unless the log level is hit. For error-level, this is simply a style thing, but for debug-level logging this can occasionally have a non-trivial performance impact.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good input, thanks!
@@ -180,7 +179,7 @@ def _get_decoder_func(self, schema_id, payload): | |||
|
|||
self.id_to_decoder_func[schema_id] = lambda p: read_data(p, schema_dict) | |||
return self.id_to_decoder_func[schema_id] | |||
except: | |||
except: # noqa |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
except Exception
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review!
d8b3eda
to
a78bb20
Compare
This should address all of Jeff's review points. |
a78bb20
to
d6b0f0f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks good. caught a few things I missed before.
@@ -269,7 +269,8 @@ def test_compatibility(self, subject, avro_schema, version='latest'): | |||
else: | |||
log.error("Unable to check the compatibility") | |||
False | |||
except: | |||
except Exception as e: | |||
log.error("_send_request() failed: %s", e) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use log.exception()
... it is the same as log.error()
but it will automatically append the exception:
log.exception("_send_request() failed.")
else: | ||
return schema.Parse(schema_str) | ||
except schema.AvroException.SchemaParseException as e: | ||
raise ClientError("Schema parse failed: %s" % (str(e))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need to cast to string...
except: | ||
schema = None | ||
except ClientError as e: | ||
raise SerializerError("unable to fetch schema with id %d: %s" % (schema_id, str(e))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need to cast the exception to a string...
tests/avro/mock_registry.py
Outdated
self.server.serve_forever() | ||
|
||
def start(self): | ||
""" Start, and wait for server to be fully started, before returning. """ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Per PEP-257, docstrings don't need space before/after the sentence.
This makes the tests more robust and faster
d6b0f0f
to
efa47ae
Compare
Thanks for your review, @jeffwidman ! |
No problem. Curious why you didn't implement the other points in my review of the fixes though? Was that purposeful decision or accidental oversight? |
@jeffwidman I thought I covered them all, what did I miss? |
Scroll here and you will see a few unaddressed comments: https://github.com/confluentinc/confluent-kafka-python/pull/279/files No problem if you choose not to incorporate them, I mentioned only because I had a hunch you never even saw them. |
flake8 now warns on bare exception, this PR attempts to fix that.