-
-
Notifications
You must be signed in to change notification settings - Fork 543
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 unclear error when missing dependencies #3511
Fix unclear error when missing dependencies #3511
Conversation
Thanks for adding the Here's a preview of the changelog: When calling the CLI without all the necessary dependencies installed, Here's the tweet text:
|
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.
Hey @parafoxia - I've reviewed your changes and they look great!
Here's what I looked at during the review
- 🟡 General issues: 4 issues found
- 🟢 Security: all looks good
- 🟡 Testing: 5 issues found
- 🟢 Complexity: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.
tests/exceptions/classes/test_exception_class_missing_optional_dependencies_error.py
Show resolved
Hide resolved
tests/exceptions/classes/test_exception_class_missing_optional_dependencies_error.py
Show resolved
Hide resolved
tests/exceptions/classes/test_exception_class_missing_optional_dependencies_error.py
Outdated
Show resolved
Hide resolved
tests/exceptions/classes/test_exception_class_missing_optional_dependencies_error.py
Show resolved
Hide resolved
tests/exceptions/classes/test_exception_class_missing_optional_dependencies_error.py
Show resolved
Hide resolved
0604f81
to
c22e8f1
Compare
try: | ||
from .app import app | ||
from .commands.codegen import codegen as codegen # noqa | ||
from .commands.export_schema import export_schema as export_schema # noqa | ||
from .commands.schema_codegen import schema_codegen as schema_codegen # noqa | ||
from .commands.server import server as server # noqa | ||
from .commands.upgrade import upgrade as upgrade # noqa | ||
|
||
from .app import app | ||
def run() -> None: | ||
app() | ||
|
||
except ModuleNotFoundError as exc: | ||
from strawberry.exceptions import MissingOptionalDependenciesError | ||
|
||
def run() -> None: | ||
app() | ||
raise MissingOptionalDependenciesError(extras=["cli"]) from exc |
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 great, it's something I was thinking about a few weeks ago!
Do you think it would be possible to make this granular too? Maybe it's not worth it, but some commands need different dependencies (for example schema codegen needs libcst, most of the other commands don't), so maybe we can suggest to install libcst
or [cli]
in that case?
But I don't know if we can do this cleanly 🤔
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.
Oh wow, how convenient!
If you did want to do that, the cleanest way would probably be to import anything necessary in the commands themselves and raise errors within that. That would add quite a few lines (unless you went to the trouble of using a decorator to transform the error or something), but would get extra granularity when needed.
Idk how worth it it would be though honestly, unless libcst
installs like 20 dependencies of its own.
I did do it like that for a personal project of mine though, where you could use it without the dependencies, but would need them for extra functionality: https://github.com/parafoxia/analytix/blob/main/analytix/reports/interfaces.py#L315-L318
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.
yeah, maybe it's not worth doing it for now
I want to add some more commands in future, maybe I can do that then 😊
I'll properly review this and merge later! thanks :)
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 🔥 Thank you so much 😊
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3511 +/- ##
==========================================
- Coverage 96.53% 96.52% -0.02%
==========================================
Files 516 518 +2
Lines 33174 33214 +40
Branches 5521 5522 +1
==========================================
+ Hits 32025 32060 +35
- Misses 916 921 +5
Partials 233 233 |
CodSpeed Performance ReportMerging #3511 will not alter performanceComparing Summary
|
from typing import Optional | ||
|
||
|
||
class MissingOptionalDependenciesError(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.
I've changed this to just be based on Exception 😊
Thanks for contributing to Strawberry! 🎉 You've been invited to join You can also request a free sticker by filling this form: https://forms.gle/dmnfQUPoY5gZbVT67 And don't forget to join our discord server: https://strawberry.rocks/discord 🔥 |
Description
This PR adds a new
MissingOptionalDependenciesError
and puts it to work in thecli
module.This was motivated by the previous
ModuleNotFoundError
causing confusion as to the exact cause of the error (especially as this would become a chain of six errors for the CLI). The new error emits a more helpful message, telling the user what they need to install to resolve the issue. It also makes it clearer that there are optional dependencies at work, especially in the case a user installsstrawberry
expecting the CLI to work out of the box.The exception class is only in use in the CLI here as I'm not familiar enough with the rest of the code to implement it there (though if maintainers have any shortcuts for working out where it needs to be, happy to do that). The exception class is also futureproofed to hell (arguably overly so lmao) to allow seamless introduction wherever else seems fitting.
It's also tested, despite there not being a precedent to test exception classes. I took a punt at a best standard for this, but if maintainers want this removed or changed let me know.
I've also set this to
minor
instead ofpatch
as it could cause breaking changes for a few users if they've gone about handling this themselves.Types of Changes
Issues Fixed or Closed by This PR
None.
Checklist