Skip to content
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

Conversation

parafoxia
Copy link
Contributor

@parafoxia parafoxia commented May 24, 2024

Description

This PR adds a new MissingOptionalDependenciesError and puts it to work in the cli 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 installs strawberry 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 of patch as it could cause breaking changes for a few users if they've gone about handling this themselves.

Types of Changes

  • Core
  • Bugfix
  • New feature
  • Enhancement/optimization
  • Documentation

Issues Fixed or Closed by This PR

None.

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • I have tested the changes and verified that they work and don't break anything (as well as I can manage).

@botberry
Copy link
Member

botberry commented May 24, 2024

Thanks for adding the RELEASE.md file!

Here's a preview of the changelog:


When calling the CLI without all the necessary dependencies installed,
a MissingOptionalDependenciesError will be raised instead of a
ModuleNotFoundError. This new exception will provide a more helpful
hint regarding how to fix the problem.

Here's the tweet text:

🆕 Release (next) is out! Thanks to Ethan Henderson for the PR 👏

Get it here 👉 https://strawberry.rocks/release/(next)

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a 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

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.

@parafoxia parafoxia force-pushed the fix-unclear-error-when-missing-dependencies branch from 0604f81 to c22e8f1 Compare May 24, 2024 11:26
Comment on lines +1 to +15
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
Copy link
Member

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 🤔

Copy link
Contributor Author

@parafoxia parafoxia May 24, 2024

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

Copy link
Member

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 :)

Copy link
Member

@patrick91 patrick91 left a 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 😊

Copy link

codecov bot commented May 25, 2024

Codecov Report

Attention: Patch coverage is 93.75000% with 3 lines in your changes are missing coverage. Please review.

Project coverage is 96.52%. Comparing base (61f9e3a) to head (3121c07).

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              

Copy link

codspeed-hq bot commented May 25, 2024

CodSpeed Performance Report

Merging #3511 will not alter performance

Comparing parafoxia:fix-unclear-error-when-missing-dependencies (3121c07) with main (61f9e3a)

Summary

✅ 12 untouched benchmarks

from typing import Optional


class MissingOptionalDependenciesError(Exception):
Copy link
Member

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 😊

@patrick91 patrick91 merged commit dd4c343 into strawberry-graphql:main May 25, 2024
62 of 64 checks passed
@botberry
Copy link
Member

Thanks for contributing to Strawberry! 🎉 You've been invited to join
the Strawberry GraphQL organisation 😊

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 🔥

@parafoxia parafoxia deleted the fix-unclear-error-when-missing-dependencies branch May 25, 2024 10:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants