- Notifications
You must be signed in to change notification settings - Fork 175
Python: escape reserved keywords in generated code #321
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
base: master
Are you sure you want to change the base?
Conversation
…rds adding `_` to them to prevent creating syntactically incorrect code
Hey @GreyCat I have to be honest - this feels very frustrating. My PR (#320) fixed this exact issue 11 days ago, with tests and consistent escaping across identifiers. I myself implemented the same fix in 3 reis that depended on Katia's, and took my time to fix it here on the source so it stops happening for everyone. Now I see a new PR that basically re-implements the same logic, but:
This is not an improvement - it’s the same idea, but with less safety. If the goal was broader coverage, that should have been built on top of the existing fix with tests, not a rewrite. This is exactly the kind of thing that makes OSS contributors to fade away: If this review is going to be taken seriously, the right path here is: Otherwise we’re just encouraging people to stop contributing. And just to touch on the “underscore vs numeric suffix” bikeshedding from my original PR: appending _ is the established convention in Python (PEP8, stdlib, countless OSS projects). It’s simple, predictable, and matches what developers already expect — class → class_. Dragging this into abstract arguments about “what if two underscores collide with another identifier” or “should we number them” is over-engineering. Those pathological cases are vanishingly rare compared to the real-world problem of generated code not even importing(!!). Numeric suffixes would actually make things less readable (class1, class2 instead of class_) and less Pythonic. The priority here should be solving the actual bug cleanly, not theoretical purity debates that stall fixes. |
@andrewmonostate I understand and sympathize with frustration (especially in light of lengthy discussions in #320). Now, put yourself in my shoes:
My intent was never to take the credit away from you, nor to downplay the initiative you came up with. As I've mentioned, this is a complicated problem. Yes, it will involve changes in 2 repos instead of 1, and it's a bit more complicated than your average "load a project in IDE -> change code, add tests in same language -> run tests in IDE -> done". We can argue about monorepo vs polyrepo approach is easy to work with, but I will stand firmly by the fact we need to test such things against real target language interpreter (in this case, Python). Now, we can do it either of two ways. Option 1
Option 2
|
Thank you for the response. A few clarifications and concrete requests:
Either way, just please ship the fix, if possible to do so without deleting tests or leaving m as a blind spot, great. The standard and practical Pythonic behavior would be class → class_, and both compiler-local and integration tests should backstop it. PS: I didn't reply earlier or create a patch in the original PR because I was trying to understand where that discussion there was heading. I still don't know. |
PythonCompiler/PythonTranslator: implement escaping of reserved keywords adding
_
to them to prevent creating syntactically incorrect codeRelated test changes (without compiler fixes prove that the problem exists, with compiler fixes prove that the problem is resolved): kaitai-io/kaitai_struct_tests#136
Related prior art PR by @andrewmonostate: #320 — this implementation is strongly inspired by that PR, but this one:
_m_
member