Repository navigation
Fix TypeError when a parenthesized lambda follows a keyword - #1475
abhishekacharya200 wants to merge 1 commit into
Conversation
Lambda._safe_to_use_with_word_operator used zero-argument super(), which breaks under add_slots because the class is recreated and the __class__ cell still refers to the original class. Parsing code such as '() if 0 else(lambda:())' or 'not(lambda: 0)' raised 'TypeError: super(type, obj): obj must be an instance or subtype of type'. Use the explicit super(Lambda, self) form like the rest of the module. Fixes Instagram#1061
|
Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
Summary
Fixes #1061.
Lambda._safe_to_use_with_word_operatorcalled zero-argumentsuper(). Because@add_slotsrebuilds the class, the__class__cell used bysuper()still points at the original class, so the call fails withTypeError: super(type, obj): obj must be an instance or subtype of type. This happens whenever a parenthesized lambda sits right after a keyword with no space, e.g.:The fix uses
super(Lambda, self), which is what the other slotted nodes inexpression.py(andLambda._validate) already do.Test Plan
Added two cases to
libcst/_nodes/tests/test_ifexp.py:foo if bar else(lambda:baz)node (round-trips throughparse_expression)elsewith no space, which now raises the expectedCSTValidationErrorinstead of aTypeErrorWithout the fix the module errors with the
TypeErrorabove; with it, all tests pass. The fullpython -m libcst.testsrun shows no new failures compared tomain.flake8andufmt checkpass on the changed files.