Skip to content

Allow SQLAlchemy class mapping errors to propagate #281

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 3 commits into from

Conversation

MPickfield
Copy link

There's a few PR's open for this already but it seems like they're dead. This PR is the same as #169 with the suggested readability changes.

@coveralls
Copy link

coveralls commented Jul 27, 2020

Coverage Status

Coverage increased (+0.02%) to 97.631% when pulling dad2abe on MPickfield:master into 20ecaea on graphql-python:master.

"You need to pass a valid SQLAlchemy Model in " '{}.Meta, received "{}".'
).format(cls.__name__, model)

if not is_mapped_class(model):
Copy link
Contributor

Choose a reason for hiding this comment

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

assert is only evaluated when the interpreter is running in "debug" mode and is ignored when optimizations are enabled with the -O command line switch. I personally think it's proper to have this check every time and what you have written is an improvement, but to match the previous behavior it might be best to wrap this in an if __debug__: check.

https://docs.python.org/3/reference/simple_stmts.html#grammar-token-assert-stmt

Perhaps

if __debug__ and not is_mapped_class(model):
    raise ValueError(...)

except (ArgumentError, UnmappedClassError):
except ArgumentError as error:
# Only handle ArgumentErrors for non-class objects
if "Class object expected" in str(error):
Copy link
Contributor

Choose a reason for hiding this comment

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

To me, this is a bit of a code-smell. Is it possible to instead create a new exception class and catch that instead? I'm not sure what the import tree looks like, so I'm unsure where it should go, but perhaps:

class InvalidSQLAlchemyModel(ValueError):
    pass

...

    raise InvalidSQLAlchemyModel("You need to pass a valid SQLAlchemy Model in ... ")

Then there's no need for checking the string of the error message here.

Copy link
Author

Choose a reason for hiding this comment

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

I agree but I think that would need to be handled in SQLAlchemy itself

@ianepperson
Copy link
Contributor

ianepperson commented Oct 5, 2020

I really want to see this go in - I'm currently troubleshooting an error hidden by that overly broad except clause.

Oh, and just so we're clear, I'm not the maintainer. My suggestions have no weight.

@MPickfield
Copy link
Author

I really want to see this go in - I'm currently troubleshooting an error hidden by that overly broad except clause.

Oh, and just so we're clear, I'm not the maintainer. My suggestions have no weight.

Good feedback! I'm going to leave things as they are in hopes that @Nabellaleen who requested these changes on #169 approves this should they take a look.

@iamareebjamal
Copy link

Ping @jnak @Nabellaleen

@erikwrede
Copy link
Member

erikwrede commented Apr 28, 2022

Closing this in favor of the original PR #169. Thanks for the effort!

I agree with @MPickfield that this should be handled upstream in SQLAlchemy. However, @ianepperson if you have more specifics on that error you were tracking, feel free to post them here or open another PR. For now, this is an important change, further improvements are always welcome.

@erikwrede erikwrede closed this Apr 28, 2022
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.

5 participants