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

Allow transparent cell magics #8911

Merged
merged 2 commits into from
Dec 7, 2023
Merged

Allow transparent cell magics #8911

merged 2 commits into from
Dec 7, 2023

Conversation

dhruvmanila
Copy link
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
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
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
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
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
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
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
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
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
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.

Copy link
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
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
@dhruvmanila dhruvmanila merged commit 6bbabce into main Dec 7, 2023
17 checks passed
@dhruvmanila dhruvmanila deleted the dhruv/cell-magics branch December 7, 2023 20:15
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