Skip dir() names that raise AttributeError when gathering visitors - #1460
Skip dir() names that raise AttributeError when gathering visitors#1460Sanjays2402 wants to merge 1 commit into
Conversation
_gather_matchers, _gather_constructed_visit_funcs and _gather_constructed_leave_funcs iterate over dir(obj) and getattr each name. A name reported by dir() is not guaranteed to be retrievable: zope.interface installs a __provides__ descriptor that raises AttributeError on access, so merely importing Twisted made every MatcherDecoratableVisitor/Transformer constructor crash with AttributeError: __provides__. Such names cannot be visit/leave methods, so fetch them through a _safe_getattr helper and skip them instead of propagating the error. Fixes Instagram#1458
|
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 #1458
Summary
_gather_matchersand the two_gather_constructed_*_funcshelpers iterate overdir(obj)andgetattrevery name. A name reported bydir()isn't guaranteed to be retrievable — zope.interface installs a__provides__descriptor that raisesAttributeErroron access — so merely importing Twisted made everyMatcherDecoratableVisitor/Transformerconstructor crash withAttributeError: __provides__. Such names can't be visit/leave methods, so they're now fetched through a_safe_getattrhelper and skipped.Test Plan
New
test_inaccessible_attribute_is_skippedinlibcst/matchers/tests/test_visitors.pyuses a descriptor that raisesAttributeErroron access; it fails with the reportedAttributeError: __provides__without the fix and passes with it.libcst/matchers/tests/+test_add_imports.py= 180 passed; black and flake8 clean on the changed files.