Skip to content

Bad type guess in VS Code with ModelHubMixin inheritance #2694

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

Closed
quentinblampey opened this issue Dec 5, 2024 · 2 comments · Fixed by #2695
Closed

Bad type guess in VS Code with ModelHubMixin inheritance #2694

quentinblampey opened this issue Dec 5, 2024 · 2 comments · Fixed by #2695

Comments

@quentinblampey
Copy link

quentinblampey commented Dec 5, 2024

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

@Wauplin
Copy link
Contributor

Wauplin commented Dec 5, 2024

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

@quentinblampey
Copy link
Author

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

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

Successfully merging a pull request may close this issue.

2 participants