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

How to type hint with InferenceData #2415

Open
williambdean opened this issue Jan 22, 2025 · 4 comments
Open

How to type hint with InferenceData #2415

williambdean opened this issue Jan 22, 2025 · 4 comments

Comments

@williambdean
Copy link

How can I avoid this?

Image

Aren't the groups "defined" or restricted to a small set?

Might be related to #1581 but I interpreted that as internal type hinting

@tomicapretto
Copy link
Contributor

What I've done in the past is idata["prior"], etc. But I don't know if that's a real solution.

@amaloney
Copy link
Member

Thanks @tomicapretto for the suggestion. I agree this is the "best" method when using ArviZ and what looks to me is pyright in neovim @wd60622.

The reason why one would get these type errors is because the InferenceData object does not explicitly define anywhere in the code the name prior, posterior, etc. Instead, what we do is inherit the Mapping object from Python into the InferenceData object, with keys as strings and values as xarray Dataset objects, see below.

class InferenceData(Mapping[str, xr.Dataset]):

Allowing you to use "dot" notation is built into the InferenceData object, which is why you can use it, but it will give you the type errors you see. To remove those errors, I would also suggest doing the following.

def function(idata: az.InferenceData) -> None:
    idata["prior"]
    idata["posterior"]
    idata["posterior_predictive"]
    idata["sample_stats"]
    idata["log_likelihood"]

Then you will not get the type errors you see, since you are accessing the InferenceData object like what its inherited type is, a Mapping, or dictionary.

Image

@williambdean
Copy link
Author

I personally dont use brackets. Seems imprecise.

Does this warn with idata["unlikely_key"]?

@amaloney
Copy link
Member

No, but neither would a dictionary, which is what the InferenceData object is based on. This is a deep issue with how Python objects are created, and I do not see an immediate answer. I have seen this type confusion before with InferenceData objects and other users, so you are in good company.

Let me think about it more, since there are ways to allow for this notation in Python. Take for example the following where I've converted the InferenceData object to a SimpleNamespace object. Doing this has many downstream implications, but you no longer have a type error. Doing this is not recommended without further testing and analysis

Image

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

No branches or pull requests

3 participants