Skip to content

Raising AssertionErrors instead of assert statements #794

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

Closed
wants to merge 9 commits into from
Closed

Raising AssertionErrors instead of assert statements #794

wants to merge 9 commits into from

Conversation

Kacppian
Copy link

@Kacppian Kacppian commented Jul 8, 2018

Code to use

if something is not None:
    raise AssertionError("Something was unexpectedly None")

instead of

assert something is not None, "Something was unexpectedly None"

@Kacppian Kacppian changed the title Issue #784 Raising AssertionErrors instead of assert statements Jul 8, 2018
@@ -44,8 +44,10 @@ def get_complete_version(version=None):
if version is None:
from graphene import VERSION as version
else:
assert len(version) == 5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest only using is for comparing versus None, or identity comparisons. == is for comparing logical equivalence, which is going to be pretty much all numeric comparisons.

Some of the answers on https://stackoverflow.com/questions/2239737/is-it-better-to-use-is-or-for-number-comparison-in-python discuss it a bit more (1st SO page I googled).

"Resolved value from the connection field have to be iterable or instance of {}. "
'Received "{}"'
).format(connection_type, resolved)
if not isinstance(resolved, Iterable):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than repeating the construction:

if BOOLEAN_EXPRESSION:
    raise AssertionError("...")

Could you DRY things up by writing a helper function that does this and use it across the code as a straight replacement for the assert statements?

Copy link
Author

@Kacppian Kacppian Jul 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am trying to do something like this -

def raise_assertion_if(condition=None, message=None):
    if condition:
        raise AssertionError(message)

raise_assertion_if(
    condition=version[3] not in ("alpha", "beta", "rc", "final"),
    message="Release version is unkown"
)

But, Black (linter) keeps failing. Any clue as to why?

@Kacppian Kacppian closed this Jul 29, 2018
@Kacppian
Copy link
Author

Closed. This was resolved in #808

@Kacppian Kacppian mentioned this pull request Aug 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants