huggingface/huggingface_hub

Bad type guess in VS Code with ModelHubMixin inheritance

quentinblampey opened this issue · 2 comments

Hello,

When creating a class that inherits from ModelHubMixin, I don't have any auto-completion in VS Code (although I think it's probably not related to VS Code).

For instance, if I create a class class Dummy(ModelHubMixin) as below, then VS Code believes any instance of Dummy is of type ModelHubMixin, instead of Dummy.

from huggingface_hub import ModelHubMixin

class Dummy(ModelHubMixin):
    def dummy_example(self, x: str) -> str:
        return x

a = Dummy()
a.dummy_example("Hello, world!") # no auto-completion

After some quick investigation, I think it's because of the __new__ method in ModelHubMixin. I found an ugly way to fix that, by setting the return type of __new__ in the Dummy class as below.

from huggingface_hub import ModelHubMixin

class Dummy(ModelHubMixin):
    def __new__(cls, *args, **kwargs) -> "Dummy":
        return super().__new__(cls, *args, **kwargs)
    
    def dummy_example(self, x: str) -> str:
        return x

a = Dummy()
a.dummy_example("Hello, world!") # now, auto-completion works fine

What do you think? Is there a way to fix that without overwriting the __new__ method?

Maybe I'm missing something, but I spent quite some time to understand that the problem comes from ModelHubMixin, and I was very annoyed about not having auto-completion...

Hi @quentinblampey , thanks for reporting and sorry for the lost hours on this 😬 I've opened a PR to fix the type annotation: #2695.

Thanks @Wauplin for the very quick answer and PR, awesome!