Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[question] Safe way to check for presence of a key in deps_user_info? #7130

Closed
lyricaljoke opened this issue Jun 2, 2020 · 2 comments · Fixed by #7131
Closed

[question] Safe way to check for presence of a key in deps_user_info? #7130

lyricaljoke opened this issue Jun 2, 2020 · 2 comments · Fixed by #7131
Assignees
Milestone

Comments

@lyricaljoke
Copy link

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.
    for dep_name, dep_cpp_info in self.deps_build_info.dependencies:
        user_info = self.conanfile.deps_user_info[dep_name]
        print(f"Checking {dep_name} dep for attribute_foo.")
        if getattr(user_info, 'attribute_foo', None) is not None:
            # 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:

    def __getattr__(self, name):
        if name.startswith("_") and name.endswith("_"):
            return super(UserInfo, self).__getattr__(name)

        return self._values_[name]

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.

@lyricaljoke 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
@memsharded memsharded self-assigned this Jun 2, 2020
@memsharded
Copy link
Member

Hi @lyricaljoke

Good point, I think it is a bug, and have proposed #7131 for next release, so this works as expected. Will be in next 1.26 release soon.

@memsharded memsharded added this to the 1.26 milestone Jun 2, 2020
uilianries added a commit to uilianries/conan that referenced this issue Jun 2, 2020
Signed-off-by: Uilian Ries <uilianries@gmail.com>
@memsharded
Copy link
Member

Merged, will be available in next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants