Hm... Nothing comes to mind right now that quite fits the bill of "object that has attributes corresponding to a given dict's keys".

I expect the best thing to do is to adorn the stub class for `Bunch` with a `__getattr__` method that returns `Any`, to avoid false positives in the example you mention.

And for functions like `load_iris()`, assuming you know the attributes it returns, you could use a `Protocol`.

On Fri, Aug 21, 2020 at 3:03 PM Graham Wheeler <> wrote:
Hi all

I have been looking at writing typings for sklearn and am wondering if there is a good solution for

Clearly it can be typed as just the class itself; however, there are many examples with sklearn that start with loading a sample dataset, in which case the specific members are useful to specify. For example:

    from sklearn.datasets import load_iris

    iris = load_iris()
    X =
    y =

NamedDictionary seems about the closest but its not ideal. Protocols might be another option. Wondering if there is a better solution.

This might be useful at some point in doing dynamic typing of things like pandas DataFrames where columns could be treated in a similar way (e.g. in a live environment like a Jupyter notebook).

Typing-sig mailing list --
To unsubscribe send an email to
Member address:

--Guido van Rossum (
Pronouns: he/him (why is my pronoun here?)