Skip to content

Fix vscode integration #52

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 65 commits into from
Closed

Fix vscode integration #52

wants to merge 65 commits into from

Conversation

MusicalNinjaDad
Copy link
Owner

@MusicalNinjaDad MusicalNinjaDad commented Mar 8, 2025

Summary by Sourcery

Fixes VS Code integration by changing the CellPath format to use subscript notation (path/to/notebook.ipynb[Celln]) and patching pytest to handle this new format.

Bug Fixes:

  • Fixes integration with vscode Test Explorer by changing the CellPath format.

Enhancements:

  • Updates CellPath to use subscript format path/to/notebook.ipynb[Celln].

Tests:

  • Updates tests to reflect the change in CellPath format.

Copy link
Contributor

sourcery-ai bot commented Mar 8, 2025

Reviewer's Guide by Sourcery

This pull request fixes integration with vscode Test Explorer by changing the CellPath format to use subscript notation (path/to/notebook.ipynb[Celln]) and patching pytest functions to handle the new format. It also updates tests to reflect the change in CellPath format.

Sequence diagram for test execution within a cell

sequenceDiagram
    participant Cell
    participant _ParsedNotebook
    participant dummy_module
    participant linecache

    Cell->>_ParsedNotebook: _getobj()
    activate Cell
    Cell->>_ParsedNotebook: Get cell source
    _ParsedNotebook-->>Cell: cell source
    Cell->>dummy_module: exec(cellsabove, dummy_module.__dict__)
    Cell->>dummy_module: exec(testcell, dummy_module.__dict__)
    Cell->>linecache: cache[cell_filename] = (..., testcell_source, ...)
    Cell-->>pytest: dummy_module
    deactivate Cell
Loading

Updated class diagram for CellPath

classDiagram
    class CellPath {
        -notebook: Path
        -cell: str
        +__eq__(other: object) bool
        +__hash__() int
        +exists(*args: Any, **kwargs: Any) bool
        +relative_to(other: PathLike, *args: Any, **kwargs: Any) Self
        +notebook() Path
        +cell() str
        +is_cellpath(path: str) bool
        +get_notebookpath(path: str) Path
        +get_cellid(path: str) int
        +to_nodeid(path: str) str
        +patch_pytest_absolutepath() dict[tuple[ModuleType, str], FunctionType]
    }
    Path <|-- CellPath
    class Path {
    }
    CellPath .. Path : notebook
Loading

File-Level Changes

Change Details Files
Updates CellPath to use subscript format path/to/notebook.ipynb[Celln].
  • Updates CellPath.is_cellpath to check for the new subscript format.
  • Updates CellPath.get_notebookpath to extract the notebook path from the new format.
  • Updates CellPath.get_cellid to extract the cell ID from the new format.
  • Updates CellPath.to_nodeid to convert the new format to a pytest node ID.
  • Updates Cell.from_parent to use the new CellPath format.
  • Updates Cell.reportinfo to use the new CellPath format.
  • Updates tests to reflect the change in CellPath format.
pytest_ipynb2/plugin.py
tests/test_execution.py
tests/test_commandline.py
tests/test_collection.py
pytest_ipynb2/_pytester_helpers.py
pytest_ipynb2/_cellpath.py
Patches pytest functions to handle the new CellPath format.
  • Monkeypatches _pytest.pathlib.absolutepath to handle CellPaths.
  • Monkeypatches _pytest.nodes.absolutepath to handle CellPaths.
  • Stores original functions in stash to revert later.
  • Reverts monkeypatches based on stashed versions during session finish.
  • Converts any CellPaths passed as commandline args to "::"-separated nodeids.
pytest_ipynb2/plugin.py
pytest_ipynb2/_cellpath.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

@MusicalNinjaDad MusicalNinjaDad marked this pull request as ready for review March 8, 2025 16:29
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 @MusicalNinjaDad - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding a helper function to encapsulate the logic for patching pytest functions, making the code more modular and readable.
  • The pytest_load_initial_conftests hook modifies command-line arguments; ensure this doesn't interfere with other plugins or pytest's argument parsing logic.
Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🟢 Security: all looks good
  • 🟡 Review instructions: 4 issues found
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 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.

return f"{CELL_PREFIX}{self.get_cellid(str(self))}"

@classmethod
def is_cellpath(cls, path: str) -> bool:
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion (bug_risk): Assess robustness of is_cellpath string splitting logic.

The current implementation relies on splitting the string based on the '[Cell' substring. Consider edge cases such as unexpected additional '[' characters or variations of file names that might inadvertently fulfill the condition. If these formats are strictly controlled, the code is acceptable; if not, enhanced validation might be warranted.

Suggested implementation:

import re
# (existing imports)
    @classmethod
    def is_cellpath(cls, path: str) -> bool:
        """Determine whether a str is a valid representation of our pseudo-path."""
        pattern = r'^(?P<notebook>.+\.ipynb)\[' + re.escape(CELL_PREFIX) + r'(?P<cellid>\d+)\]$'
        return re.fullmatch(pattern, path) is not None

If CELL_PREFIX is subject to change or defined elsewhere with different values, ensure that its usage with re.escape is valid across the codebase.

@MusicalNinjaDad MusicalNinjaDad deleted the vscode_subscripting branch March 9, 2025 19:02
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.

1 participant