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
Operating system: MSYS2 on Windows 10
Conan version: 1.25.0
Python version: 3.7.5
I'm confused about whether there's a recommended way to check whether a certain attribute is present in the user_info object of dependencies while running in a Generator context. In my use case, I'm writing a generator which is meant to support a new user_info attribute in some of my recipes. However, the generator should also work if that attribute is not set, in order to support legacy recipes and recipes outside of my control.
My code looks something like:
# Approach: use 'getattr' with a default.fordep_name, dep_cpp_infoinself.deps_build_info.dependencies:
user_info=self.conanfile.deps_user_info[dep_name]
print(f"Checking {dep_name} dep for attribute_foo.")
ifgetattr(user_info, 'attribute_foo', None) isnotNone:
# do stuff with user_info.attribute_foo
The getattr method throws a KeyError if the attribute is not present, however. hasattr runs into the same issue because it uses getattr internally. I see that __getattr__ is overridden in Conan's user_info.py implementation like so:
It seems like this breaks the typical convention for overriding __getattr__, as I understand it, because it raises a KeyError rather than an AttributeError if the attribute is missing.
Given all this, I'm not sure how to safely check for whether a user_info object has a specific attribute, except for wrapping it with a broad try / except Exception, which seems unidiomatic. Looking for any advice / recommendation.
The text was updated successfully, but these errors were encountered:
lyricaljoke
changed the title
[question] Safe way to check for presence key in deps_user_info?
[question] Safe way to check for presence of a key in deps_user_info?
Jun 2, 2020
Operating system: MSYS2 on Windows 10
Conan version: 1.25.0
Python version: 3.7.5
I'm confused about whether there's a recommended way to check whether a certain attribute is present in the user_info object of dependencies while running in a Generator context. In my use case, I'm writing a generator which is meant to support a new user_info attribute in some of my recipes. However, the generator should also work if that attribute is not set, in order to support legacy recipes and recipes outside of my control.
My code looks something like:
The
getattr
method throws aKeyError
if the attribute is not present, however.hasattr
runs into the same issue because it usesgetattr
internally. I see that__getattr__
is overridden in Conan's user_info.py implementation like so:It seems like this breaks the typical convention for overriding
__getattr__
, as I understand it, because it raises aKeyError
rather than anAttributeError
if the attribute is missing.Given all this, I'm not sure how to safely check for whether a user_info object has a specific attribute, except for wrapping it with a broad
try
/except Exception
, which seems unidiomatic. Looking for any advice / recommendation.The text was updated successfully, but these errors were encountered: