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 <gram@geekraver.com> wrote:
Hi all

I have been looking at writing typings for sklearn and am wondering if there is a good solution for https://scikit-learn.org/stable/modules/generated/sklearn.utils.Bunch.html

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 = iris.data
    y = iris.target

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).

Thanks
Graham
_______________________________________________
Typing-sig mailing list -- typing-sig@python.org
To unsubscribe send an email to typing-sig-leave@python.org
https://mail.python.org/mailman3/lists/typing-sig.python.org/
Member address: guido@python.org


--
--Guido van Rossum (python.org/~guido)
Pronouns: he/him (why is my pronoun here?)