Allow FlattenSentinel in matcher decorator return annotations - #1465
Allow FlattenSentinel in matcher decorator return annotations#1465Sanjays2402 wants to merge 1 commit into
Conversation
Annotating a @leave-decorated method with FlattenSentinel raised TypeError: issubclass() arg 1 must be a class, even though returning a FlattenSentinel from leave_<Node> is valid. Two causes: the generated TYPED_FUNCTION_RETURN_MAPPING omitted FlattenSentinel entirely (unlike the leave_ signatures in _typed_visitor.py), and _get_possible_annotated_classes passed the parameterized generic FlattenSentinel[BaseStatement] straight to issubclass(), which only accepts classes. gen_type_mapping now mirrors gen_visitor_functions and emits FlattenSentinel for sequence-valued nodes, and generic aliases are unwrapped to their origin class before the subclass check.
|
Hi @Sanjays2402! 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! |
Closes #1075
Summary
Annotating a
@leave-decorated method withFlattenSentinelraisedTypeError: issubclass() arg 1 must be a class, even though returning aFlattenSentinelfromleave_<Node>is valid.Two causes: the generated
TYPED_FUNCTION_RETURN_MAPPINGomittedFlattenSentinelentirely (unlike theleave_signaturesgen_visitor_functionsemits), and_get_possible_annotated_classespassed the parameterized genericFlattenSentinel[BaseStatement]straight toissubclass(), which only accepts classes.gen_type_mappingnow mirrorsgen_visitor_functionsfor sequence-valued nodes, and generic aliases are unwrapped to their origin class before the subclass check._return_types.pyis regenerated output.Test Plan
Two tests in
libcst/matchers/tests/test_visitors.py(parameterized and bareFlattenSentinel) fail on main with the reportedTypeErrorand pass with the fix.libcst/matchers/tests145 passed / 1 skipped;libcst/codemodunchanged from baseline;ufmt checkclean on the touched files.This change was prepared with AI assistance; the regression tests were run locally and fail without the fix.