Skip to content

Allow transparent cell magics#8911

Merged
dhruvmanila merged 2 commits intomainfrom
dhruv/cell-magics
Dec 7, 2023
Merged

Allow transparent cell magics#8911
dhruvmanila merged 2 commits intomainfrom
dhruv/cell-magics

Conversation

@dhruvmanila
Copy link
Copy Markdown
Member

@dhruvmanila dhruvmanila commented Nov 29, 2023

Summary

This PR updates the logic for is_magic_cell to include certain cell magics. These cell magics would contain Python code following the line defining the command. The code could define a variable which can then be referenced in other cells. Currently, we would ignore the cell completely leading to undefined-name violation.

As discussed in #8354 (comment)

Test Plan

Add new test case to validate this scenario.

@dhruvmanila
Copy link
Copy Markdown
Member Author

Current dependencies on/for this PR:

This stack of pull requests is managed by Graphite.

// Cell 2:
// ```python
// %%time
// y = x
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

So how does the parser handle this case?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The parser would parse the first line %%time as a cell magic statement and the second line as an assignment statement.

This is technically a bug in the parser as both lines should be a single node instead but as we always ignored cell magic, it never came up.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Where does the cell magic end? Does it just include the %%time regardless of what follows? Like how would the parser deal with:

%%time x = 1
y = x

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Each statement is a single node, so %%time x = 1 would a single node and y = x would be an assignment statement. This code would raise undefined name for x. This is intentional as that requires support for inline parsing which might require more thinking.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm mostly wondering how the parser knows where the magic statement ends.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Oh, it's the newline. The lexer consumes everything till the newline and considers this as part of the command.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ah okay, makes sense. Could there be continuations though?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, but that is ignored. So,

%%time \
	x = 1
y = x

will parse in same way as your example.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Nov 29, 2023

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@charliermarsh
Copy link
Copy Markdown
Member

@dhruvmanila - What are your thoughts on moving this one forward? Are there concerns?

@dhruvmanila dhruvmanila marked this pull request as ready for review December 7, 2023 19:40
@dhruvmanila dhruvmanila added the linter Related to the linter label Dec 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

linter Related to the linter

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants