graphql-python / graphql-python/graphql-core-legacy

Usage of Asserts Breaks Code

Open
#244 5 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
372
Forks
175
PR merge metrics
No merged PRs in 30d

Description

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 a `ValueError`. Assertions are essentially debug code, not code for validating arguments.

Any usage of optimize mode or Python (`-O` or `-OO`, or `PYTHONOPTIMIZE`) 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` or `ValueError`, without a major version bump as dependent code in other projects may be catching `AssertionError`.

I'm proposing all instances of `assert foo, "Error"` be replaced with:

```python
if not foo:
raise AssertionError("Error") # Should be a TypeError
```

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.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.