-
Notifications
You must be signed in to change notification settings - Fork 25
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
Restore unmodified code in parameters cell #19
Conversation
Codecov ReportBase: 92.08% // Head: 93.54% // Increases project coverage by
Additional details and impacted files@@ Coverage Diff @@
## master #19 +/- ##
==========================================
+ Coverage 92.08% 93.54% +1.46%
==========================================
Files 3 3
Lines 139 186 +47
==========================================
+ Hits 128 174 +46
- Misses 11 12 +1
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report at Codecov. |
Thinking some more about it, I realised that it would be easy enough to keep the recreated code in its original order, replacing each assignment in place. However, that highlights a couple of corner cases that so far haven't had much thought:
🤔 |
From Python 3.8, we could possibly even do something cleverer, using the character positions of the nodes to replace just those pieces, leaving the rest of the code untouched. That would be cool, but it's not my priority today. |
Wow this looks great |
I've now gone for supporting Python 3.8+ only, and preserving the surrounding code when replacing assignments with new parameter values. With regard to my questions from this comment:
And one that I didn't ask:
|
OK, I'm fairly happy with this now. I'll probably merge it on Monday unless anyone spots a major problem before then. Then I'll get back to other pull requests - sorry it has taken so long. |
This uses
ast.unparse()
on Python 3.9, and the astunparse backport on older versions, to recreate any code in the parameters cell not recognised as parameter definitions.The AST doesn't preserve comments or formatting, so these will be lost, but the code should still do the same thing. Any code that's not treated as parameters will be moved after the parameter definitions, which might change its behaviour in some rare cases, but hopefully not many.
While I was working on this, I also enabled:
a: int = 7
) as parameters. The annotation is ignored for now - this is just likea = 7
.Closes #17.
cc @philsmt - we had a discussion about this.