Skip to content

Fix TypeError when a parenthesized lambda follows a keyword - #1475

Open
abhishekacharya200 wants to merge 1 commit into
Instagram:mainfrom
abhishekacharya200:fix-lambda-super-slots
Open

abhishekacharya200 wants to merge 1 commit into
Instagram:mainfrom
abhishekacharya200:fix-lambda-super-slots

Conversation

@abhishekacharya200

Copy link
Copy Markdown

Summary

Fixes #1061.

Lambda._safe_to_use_with_word_operator called zero-argument super(). Because @add_slots rebuilds the class, the __class__ cell used by super() still points at the original class, so the call fails with TypeError: 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.:

import libcst
libcst.parse_module("() if 0 else(lambda:())")
libcst.parse_module("x = not(lambda: 0)")
libcst.parse_module("x = a or(lambda: 0)")

The fix uses super(Lambda, self), which is what the other slotted nodes in expression.py (and Lambda._validate) already do.

Test Plan

Added two cases to libcst/_nodes/tests/test_ifexp.py:

  • a valid foo if bar else(lambda:baz) node (round-trips through parse_expression)
  • an invalid unparenthesized lambda after else with no space, which now raises the expected CSTValidationError instead of a TypeError
python -m unittest libcst._nodes.tests.test_ifexp libcst._nodes.tests.test_lambda

Without the fix the module errors with the TypeError above; with it, all tests pass. The full python -m libcst.tests run shows no new failures compared to main. flake8 and ufmt check pass on the changed files.

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
@meta-cla

meta-cla Bot commented Oct 16, 2026

Copy link
Copy Markdown

Hi @abhishekacharya200!

Thank you for your pull request and welcome to our community.

Action Required

In 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.

Process

In 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 CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla

meta-cla Bot commented Oct 16, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TypeError on parsing some valid expressions

1 participant