Skip to content

Remove runtime conditions for type checking#37340

Merged
ArthurZucker merged 6 commits intohuggingface:mainfrom
cyyever:typing_check
Jul 16, 2025
Merged

Remove runtime conditions for type checking#37340
ArthurZucker merged 6 commits intohuggingface:mainfrom
cyyever:typing_check

Conversation

@cyyever
Copy link
Copy Markdown
Contributor

@cyyever cyyever commented Apr 7, 2025

What does this PR do?

Linters aren't smart enough to evaluate the conditions.

Fixes #37339

@github-actions github-actions bot marked this pull request as draft April 7, 2025 10:25
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 7, 2025

Hi 👋, thank you for opening this pull request! The pull request is converted to draft by default. The CI will be paused while the PR is in draft mode. When it is ready for review, please click the Ready for review button (at the bottom of the PR page). This will assign reviewers and trigger CI.

@cyyever cyyever force-pushed the typing_check branch 2 times, most recently from a314785 to 328bc1c Compare April 7, 2025 10:39
@cyyever cyyever marked this pull request as ready for review April 7, 2025 10:39
@jc-louis
Copy link
Copy Markdown

jc-louis commented Apr 7, 2025

@cyyever I confirm, locally installing this branch fixes the issue #37339 👍

@Rocketknight1
Copy link
Copy Markdown
Member

@cyyever if we remove the dynamic conditions for type checking, will this cause exceptions when those dependencies aren't present?

@cyyever
Copy link
Copy Markdown
Contributor Author

cyyever commented Apr 7, 2025

@cyyever if we remove the dynamic conditions for type checking, will this cause exceptions when those dependencies aren't present?

There are wrapped in type_checking and used by linter, so that only linters would complain if there are missing symbols.

@cyyever cyyever changed the title Remove dynamic conditions for type checking Remove runtime conditions for type checking Apr 8, 2025
Copy link
Copy Markdown
Collaborator

@ArthurZucker ArthurZucker left a comment

Choose a reason for hiding this comment

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

Init are being heavily refactor #36827 would wait a bit!

@farzadab
Copy link
Copy Markdown

farzadab commented May 1, 2025

@ArthurZucker what's the status here? I get the errors after the PR you mentioned landed as well.

@cyyever cyyever force-pushed the typing_check branch 3 times, most recently from 1ecb0f7 to 89a66e4 Compare May 16, 2025 10:17
@cyyever
Copy link
Copy Markdown
Contributor Author

cyyever commented May 16, 2025

@ArthurZucker The CI failures are unrelated.

@ArthurZucker
Copy link
Copy Markdown
Collaborator

sorry everyone I am not sure I understand what is the issue here / setup to reproduce?

@ArthurZucker
Copy link
Copy Markdown
Collaborator

Happy to merge if this is compatible with the recent init refactoring!

@cyyever
Copy link
Copy Markdown
Contributor Author

cyyever commented May 20, 2025

The rebase is quite recent

@cyyever cyyever force-pushed the typing_check branch 2 times, most recently from 97ccd93 to f50b7de Compare May 23, 2025 07:53
Signed-off-by: cyy <cyyever@outlook.com>
@cyyever
Copy link
Copy Markdown
Contributor Author

cyyever commented May 23, 2025

@ArthurZucker just rebased it.

@cyyever cyyever requested a review from ArthurZucker May 23, 2025 08:31
@cyyever
Copy link
Copy Markdown
Contributor Author

cyyever commented May 23, 2025

@ydshieh have a look?

@ydshieh
Copy link
Copy Markdown
Collaborator

ydshieh commented May 23, 2025

@cyyever Got to say I am less familiar on this aspect 😅 . I see you answered

There are wrapped in type_checking and used by linter, so that only linters would complain if there are missing symbols.

to @Rocketknight1 question

@cyyever if we remove the dynamic conditions for type checking, will this cause exceptions when those dependencies aren't present?

But I don't understand well:

  • In Mypy errors since v4.51.0 #37339, do they have all the dependencies but yet still get the dummy objects (due to linter isn't smart enough?)
  • Linters aren't smart enough to evaluate the conditions.: Is this mypy specific or any type checkers?
  • I don't think we want to have exceptions when linters would complain if there are missing symbols.. I see you mentioned __all__. But in the meantime, do we have a way to avoid this? In other words, there is no way for linters to evaluate the conditions correctly ..?

Overall, I am open to merge, but we should resolve the case @Rocketknight1 mentioned at some point in the near future.

@cyyever
Copy link
Copy Markdown
Contributor Author

cyyever commented May 23, 2025

if we remove the dynamic conditions for type checking, will this cause exceptions when those dependencies aren't present?
No, because the import are wrapped inside TYPE_CHECKING, according to the doc:

 typing.TYPE_CHECKING

    A special constant that is assumed to be True by 3rd party static type checkers. It is False at runtime.
In
https://github.com/huggingface/transformers/issues/37339, do they have all the dependencies but yet still get the dummy objects (due to linter isn't smart enough?)
Linters aren't smart enough to evaluate the conditions.: Is this mypy specific or any type checkers?

Linters have to evaluate XXX_is_available in the old code and then catch an exception to deal with dummy objects or import the normal symbols, but I don't think most linters would be coded do that. At least mypy would fail, but I haven't check others.

I don't think we want to have exceptions when linters would complain if there are missing symbols.. I see you mentioned __all__. But in the meantime, do we have a way to avoid this? In other words, there is no way for linters to evaluate the conditions correctly ..?

It depends on what linters are allowed to do, and I don't consider dynamic evaluation is generally supported.

@bibz bibz mentioned this pull request Jun 17, 2025
@cyyever
Copy link
Copy Markdown
Contributor Author

cyyever commented Jun 17, 2025

Is it possible to merge it? Because I have to recheck the changes in case init.py has more changes.

@cyyever
Copy link
Copy Markdown
Contributor Author

cyyever commented Jul 3, 2025

Could we have some progress on this work because users are complaining about mypy...

@Rocketknight1
Copy link
Copy Markdown
Member

cc @ArthurZucker @Cyrilvallez the init refactor is done, I think this is ready for final review/approval!

Copy link
Copy Markdown
Collaborator

@ArthurZucker ArthurZucker left a comment

Choose a reason for hiding this comment

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

let's go!

@ArthurZucker ArthurZucker enabled auto-merge (squash) July 15, 2025 15:09
@ArthurZucker
Copy link
Copy Markdown
Collaborator

Can you check if the failing test is related or not?

@HuggingFaceDocBuilderDev
Copy link
Copy Markdown

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.

@cyyever
Copy link
Copy Markdown
Contributor Author

cyyever commented Jul 15, 2025

@ArthurZucker It is a test numerical error, not an import error, so it is unrelated.

@cyyever
Copy link
Copy Markdown
Contributor Author

cyyever commented Jul 16, 2025

@ArthurZucker No failure

@ArthurZucker ArthurZucker disabled auto-merge July 16, 2025 11:36
@ArthurZucker ArthurZucker merged commit 6116309 into huggingface:main Jul 16, 2025
23 checks passed
@ArthurZucker
Copy link
Copy Markdown
Collaborator

Merging!

@ArthurZucker
Copy link
Copy Markdown
Collaborator

Thanks 🤗

@cyyever cyyever deleted the typing_check branch July 17, 2025 13:47
rjgleaton pushed a commit to rjgleaton/transformers that referenced this pull request Jul 17, 2025
Remove dynamic conditions for type checking

Signed-off-by: cyy <cyyever@outlook.com>
Co-authored-by: Arthur <48595927+ArthurZucker@users.noreply.github.com>
zucchini-nlp pushed a commit to zucchini-nlp/transformers that referenced this pull request Jul 22, 2025
Remove dynamic conditions for type checking

Signed-off-by: cyy <cyyever@outlook.com>
Co-authored-by: Arthur <48595927+ArthurZucker@users.noreply.github.com>
@cyyever cyyever mentioned this pull request Jul 28, 2025
zaristei pushed a commit to zaristei/transformers that referenced this pull request Sep 9, 2025
Remove dynamic conditions for type checking

Signed-off-by: cyy <cyyever@outlook.com>
Co-authored-by: Arthur <48595927+ArthurZucker@users.noreply.github.com>
zaristei pushed a commit to zaristei/transformers that referenced this pull request Sep 9, 2025
Remove dynamic conditions for type checking

Signed-off-by: cyy <cyyever@outlook.com>
Co-authored-by: Arthur <48595927+ArthurZucker@users.noreply.github.com>
zaristei pushed a commit to zaristei/transformers that referenced this pull request Sep 9, 2025
Remove dynamic conditions for type checking

Signed-off-by: cyy <cyyever@outlook.com>
Co-authored-by: Arthur <48595927+ArthurZucker@users.noreply.github.com>
zaristei pushed a commit to zaristei/transformers that referenced this pull request Sep 9, 2025
Remove dynamic conditions for type checking

Signed-off-by: cyy <cyyever@outlook.com>
Co-authored-by: Arthur <48595927+ArthurZucker@users.noreply.github.com>
zaristei pushed a commit to zaristei/transformers that referenced this pull request Sep 9, 2025
Remove dynamic conditions for type checking

Signed-off-by: cyy <cyyever@outlook.com>
Co-authored-by: Arthur <48595927+ArthurZucker@users.noreply.github.com>
zaristei pushed a commit to zaristei/transformers that referenced this pull request Sep 9, 2025
Remove dynamic conditions for type checking

Signed-off-by: cyy <cyyever@outlook.com>
Co-authored-by: Arthur <48595927+ArthurZucker@users.noreply.github.com>
zaristei pushed a commit to zaristei/transformers that referenced this pull request Sep 9, 2025
Remove dynamic conditions for type checking

Signed-off-by: cyy <cyyever@outlook.com>
Co-authored-by: Arthur <48595927+ArthurZucker@users.noreply.github.com>
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.

Mypy errors since v4.51.0

8 participants