Skip to content

Commit

Permalink
Add is_property check
Browse files Browse the repository at this point in the history
Skip properties to prevent exceptions
  • Loading branch information
kiri11 committed Sep 4, 2024
1 parent 9834694 commit b53f3f5
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions libcst/matchers/_visitors.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ class UnionType:
}


def is_property(obj: object, attr_name: str) -> bool:
return isinstance(getattr(type(obj), attr_name, None), property)


# pyre-ignore We don't care about Any here, its not exposed.
def _match_decorator_unpickler(kwargs: Any) -> "MatchDecoratorMismatch":
return MatchDecoratorMismatch(**kwargs)
Expand Down Expand Up @@ -302,16 +306,12 @@ def _gather_constructed_visit_funcs(
] = {}

for funcname in dir(obj):
try:
possible_func = getattr(obj, funcname)
if not ismethod(possible_func):
continue
func = cast(Callable[[cst.CSTNode], None], possible_func)
except Exception:
# This could be a caculated property, and calling getattr() evaluates it.
# We have no control over the implementation detail, so if it raises, we
# should not crash.
if is_property(obj, funcname):
continue
possible_func = getattr(obj, funcname)
if not ismethod(possible_func):
continue
func = cast(Callable[[cst.CSTNode], None], possible_func)
matchers = getattr(func, CONSTRUCTED_VISIT_MATCHER_ATTR, [])
if matchers:
# Make sure that we aren't accidentally putting a @visit on a visit_Node.
Expand All @@ -337,16 +337,12 @@ def _gather_constructed_leave_funcs(
] = {}

for funcname in dir(obj):
try:
possible_func = getattr(obj, funcname)
if not ismethod(possible_func):
continue
func = cast(Callable[[cst.CSTNode], None], possible_func)
except Exception:
# This could be a caculated property, and calling getattr() evaluates it.
# We have no control over the implementation detail, so if it raises, we
# should not crash.
if is_property(obj, funcname):
continue
possible_func = getattr(obj, funcname)
if not ismethod(possible_func):
continue
func = cast(Callable[[cst.CSTNode], None], possible_func)
matchers = getattr(func, CONSTRUCTED_LEAVE_MATCHER_ATTR, [])
if matchers:
# Make sure that we aren't accidentally putting a @leave on a leave_Node.
Expand Down

0 comments on commit b53f3f5

Please sign in to comment.