You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
hasattr() returns True even when for the given attribute getattr() always throws an exception. This has the unfortunate side effect that one cannot safely getattr() the value of an attribute, even when guarding it with hasattr() beforehand.
I would expect the following intuitive property to hold:
getattr(x, "foo") returns a value (does not raise an exception) if and only if hasattr(x, "foo") == True.
…sing getattr
Related: bazelbuild/starlark#20 (comment), #5224
It's now possible to call methods in two steps `y = x.f; y()`
Also, `getattr` can now be used to retrieve built-in methods.
Closes#8931.
PiperOrigin-RevId: 259711316
From inspection of the code, it looks like this is fixed by Alan's refactoring. hasattr checks for both annotated methods/fields and Structure fields. And getattr has supported accessing builtin methods for quite a while now.
Description of the problem
hasattr()
returnsTrue
even when for the given attributegetattr()
always throws an exception. This has the unfortunate side effect that one cannot safelygetattr()
the value of an attribute, even when guarding it withhasattr()
beforehand.I would expect the following intuitive property to hold:
But since at least e5e3e91, this
does not hold.
Bugs: what's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.
raises an exception because
hasattr(x, "to_json")
isTrue
butgetattr(x, "to_json")
doesn't work, even when a default value ("method"
) is provided.What's the output of
bazel info release
?v0.13.0
Have you found anything relevant by searching the web?
Nothing in GitHub issues.
The text was updated successfully, but these errors were encountered: