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

Provide platform-specific hint for poetry shell #10177 #10181

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

daviewales
Copy link

@daviewales daviewales commented Feb 14, 2025

Pull Request Check List

Resolves: #10177

  • Added tests for changed code.
  • Updated documentation for changed code.

I'm not sure if this needs a documentation change.
Let me know if it needs a test and I'll add one.

Copy link

sourcery-ai bot commented Feb 14, 2025

Reviewer's Guide by Sourcery

This pull request introduces a new feature that provides shell-specific hints to the user when the poetry shell command fails. This helps users activate the Poetry environment correctly based on their shell.

Sequence diagram for poetry shell command with shell detection and hint

sequenceDiagram
  participant User
  participant PoetryApplication
  participant Shellingham
  participant IO

  User->>PoetryApplication: Runs `poetry shell`
  PoetryApplication->>Shellingham: detect_shell()
  alt Shell detected
    Shellingham-->>PoetryApplication: Shell name (e.g., bash)
    PoetryApplication->>PoetryApplication: Determines activation command based on shell
    PoetryApplication->>IO: Writes error message and activation hint
    IO-->>User: Displays error and hint
  else Shell detection fails
    Shellingham-->>PoetryApplication: ShellDetectionFailure
    PoetryApplication->>IO: Writes error message without hint
    IO-->>User: Displays error
  end
Loading

File-Level Changes

Change Details Files
Added a function to provide shell-specific hints for activating the Poetry environment.
  • Introduced env_activate_hint() function to detect the user's shell.
  • Based on the detected shell, the function returns the appropriate command to activate the Poetry environment.
  • The function returns None if the shell is not supported.
  • The env_activate_hint function uses the shellingham library to detect the shell.
  • Added logic to display the hint when the shell command fails.
  • The hint is displayed only when the shell command is invoked and an environment activation hint is available.
src/poetry/console/application.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@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 @daviewales - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding a fallback mechanism if shellingham fails to detect the shell, perhaps prompting the user to manually specify their shell type.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: 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 and I'll use the feedback to improve your reviews.

Comment on lines +66 to +69
elif shell in ("fish", "elvish"):
env_activate_command = "eval (poetry env activate)"
Copy link

Choose a reason for hiding this comment

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

question (bug_risk): Double-check the activation command syntax for fish and elvish.

Make sure that the command syntax used here is correct for fish/elvish shells, as they might have specific requirements or variations from other shells.

Copy link
Author

Choose a reason for hiding this comment

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

I checked this. I would appreciate a human double-checking the command syntax however.

except shellingham.ShellDetectionFailure:
shell = None

if shell in ("sh", "bash", "dash", "ash", "zsh", "ksh", "xonsh"):
Copy link

Choose a reason for hiding this comment

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

issue (complexity): Consider using a dictionary to map shells to activation commands for improved readability and maintainability.

def env_activate_hint() -> str | None:
    try:
        import shellingham
        shell = shellingham.detect_shell()[0]
    except shellingham.ShellDetectionFailure:
        shell = None

    shell_commands = {
        "sh": "eval $(poetry env activate)",
        "bash": "eval $(poetry env activate)",
        "dash": "eval $(poetry env activate)",
        "ash": "eval $(poetry env activate)",
        "zsh": "eval $(poetry env activate)",
        "ksh": "eval $(poetry env activate)",
        "xonsh": "eval $(poetry env activate)",
        "csh": "eval `poetry env activate`",
        "tcsh": "eval `poetry env activate`",
        "fish": "eval (poetry env activate)",
        "elvish": "eval (poetry env activate)",
        "powershell": "Invoke-Expression (poetry env activate)",
        "pwsh": "Invoke-Expression (poetry env activate)",
    }

    env_activate_command = shell_commands.get(shell)

    if env_activate_command:
        return (
            f"<b>HINT</b>: Your shell appears to be {shell}. You probably need this command:\n\n"
            f"<c1>{env_activate_command}</>"
        )
    return None

Actionable Steps:

  1. Replace the multiple if/elif branches with a mapping (shell_commands) to associate shells with commands.
  2. Use the get method on the dictionary to retrieve the command based on shell, which reduces duplicate logic and improves maintainability.

These changes keep functionality intact while simplifying the code logic.

Copy link
Author

Choose a reason for hiding this comment

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

I'm not convinced that this is better. There is a lot of duplication in the dictionary version. You could deduplicate the dictionary with variables, but I think the if-else approach is OK.

Copy link

Choose a reason for hiding this comment

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

Thank you for your feedback, we will generate fewer comments like this in the future.

@daviewales
Copy link
Author

A fall-back is not necessary, because this is an extension to the existing help message. It automatically falls back to the existing message if the shell is not detected.

@Secrus
Copy link
Member

Secrus commented Feb 15, 2025

I don't like it. I was against having the previous help message about shell command too. If another maintainer accepts this, I won't block it, but I won't approve it myself.

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.

Consider prompting the user to run eval $(poetry env activate) when they mistakenly run poetry shell
2 participants