-
Notifications
You must be signed in to change notification settings - Fork 183
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
Usage of Asserts Breaks Code #244
Comments
@dsanders11, this project is still active and has maintainers (again). In graphql-core-next, which will become the next version of graphq-core, I'm using TypeErrors and ValueErrors for the same reasons. Your proposed PR for graphql-core sounds reasonable to me. |
I actually disagree with disallowing asserts. If any assert does not match the statement above we should fix it... I would use the following guidelines on when not to use assert (blatantly stolen from https://github.com/emre/notes/blob/master/python/when-to-use-assert.md):
|
I disagree with that. My use-case involves generating schemas dynamically at runtime, based on inputs. With assertions disabled, a bad schema can be created instead of an error occurring. An existing project example of this is When assertions are disabled From the section you quoted:
From the link there's a more appropriate part to quote (emphasis mine):
|
@dsanders11 if you create a schema dynamically you still shouldn't reach most of the cases I see in your PR. A good example for something that's not an assert is in your PR where you fixed the |
@ekampf, I think we're going to continue to have fundamental differences of opinion here. Asserts shouldn't be used to validate input to a function or method. That's what What asserts should be used for is to validate assumptions and post-conditions. Assertions triggering should mean there's a bug in your code. Someone passing you bad input is not a bug in your code, it's in theirs. Some good examples from Django: else:
# resolve_expression has already validated the output_field so this
# assert should never be hit.
assert False, "Tried to Extract from an invalid type." while remaining != 0:
assert remaining > 0, 'remaining bytes to read should never go negative' While there's certainly cases of bad (IMO) uses of assert in Django, doing some grepping shows they use asserts less than 100 times and Using assertions for input validation is dangerous since they can be disabled by using optimize mode. Having code that acts differently in development and production is a recipe for bad times. If the code won't work with that input it needs to throw an actual exception, not silently fail. I run my code during development with I didn't open this issue out of overzealous concern for coding style, I did so because it affects my real world usage of |
This project uses asserts in a lot of places (I found about 100 cases) where they should not be used. They're being used to validate arguments to functions, where the normal Python way would be to use a
TypeError
or aValueError
. Assertions are essentially debug code, not code for validating arguments.Any usage of optimize mode or Python (
-O
or-OO
, orPYTHONOPTIMIZE
) strips out assertions, making this code useless. That's very problematic if you're generating schema dynamically, as you won't get errors and this library will generate an invalid GraphQL schema.At this point it's probably too late to change the uses of assert to a proper
TypeError
orValueError
, without a major version bump as dependent code in other projects may be catchingAssertionError
.I'm proposing all instances of
assert foo, "Error"
be replaced with:This will at least make the code work in optimize modes where assertions are stripped.
I'll gladly make a PR doing so, but it's hard to tell if this project is still actively developed.
The text was updated successfully, but these errors were encountered: