Skip to content

[Fix] Type Hint in from_pretrained() to Ensure Correct Type Inference #10714

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

Merged
merged 2 commits into from
Feb 4, 2025

Conversation

SahilCarterr
Copy link
Contributor

What does this PR do?

Fixes #10707 Added Self in from_pretrained method so inference will correctly recognize pipeline

Before submitting

Who can review?

Anyone in the community is free to review the PR once the tests have passed. @yiyixuxu

Before Fix:

Before this fix pyright inferred the return type as Any, leading to incorrect type checking:

import torch
from diffusers.pipelines.flux.pipeline_flux import FluxPipeline
from diffusers.utils.loading_utils import load_image

pipe = FluxPipeline.from_pretrained(
    "black-forest-labs/FLUX.1-dev",
    torch_dtype=torch.bfloat16,
)
reveal_type(pipe)

Output

/content/my_script.py
  /content/my_script.py:13:13 - information: Type of "pipe" is "DiffusionPipeline | Any | Unknown"
0 errors, 0 warnings, 1 information 

After Fix

/content/my_script.py
  /content/my_script.py:13:13 - information: Type of "pipe" is "FluxPipeline"
0 errors, 0 warnings, 1 information 

Added Self in from_pretrained method so  inference will correctly recognize pipeline
@SahilCarterr
Copy link
Contributor Author

@hlky

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Copy link
Contributor

@hlky hlky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @SahilCarterr. I've edited this to use typing-extensions as Self was only added to typing in 3.11. This type annotation is certainly useful as it allows code editors to pick up on a pipeline's functions etc. However I'd like to mention that type correctness is not strictly enforced, we do encourage type hints and welcome PRs that add type hints, but we currently do not expect mypy or similar to pass

@hlky hlky requested review from yiyixuxu and sayakpaul February 4, 2025 08:47
@hlky
Copy link
Contributor

hlky commented Feb 4, 2025

@yiyixuxu @sayakpaul We get typing-extensions from torch and probably some other packages, should be ok to not explicitly add to our own dependencies, WDYT?

@sayakpaul
Copy link
Member

We get typing-extensions from torch and probably some other packages, should be ok to not explicitly add to our own dependencies, WDYT?

We already get it from PyTorch but I am not too sure how our dependency tests will act like with this.

@hlky
Copy link
Contributor

hlky commented Feb 4, 2025

Dependency tests are passing.

It looks like numpy has typing-extensions as a dependency and we require numpy, also flax requires typing-extensions, so this should be covered for all backends and minimal install.

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 this pull request may close these issues.

Annotate return type of DiffusionPipeline.from_pretrained as Self | None
5 participants