Skip to content
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

Mypy errors since v4.51.0 #37339

Open
jc-louis opened this issue Apr 7, 2025 · 6 comments · May be fixed by #37340
Open

Mypy errors since v4.51.0 #37339

jc-louis opened this issue Apr 7, 2025 · 6 comments · May be fixed by #37340
Labels

Comments

@jc-louis
Copy link

jc-louis commented Apr 7, 2025

@cyyever this is a direct result of #37022 which marked the library as typed.

from typing import reveal_type

from transformers import Trainer, PreTrainedModel

def mypy_errors(trainer: Trainer, model: PreTrainedModel) -> None:
    reveal_type(trainer)
    reveal_type(trainer.train())
    trainer.train()
    model.dequantize()

Using mypy test.py, I get the following errors:

script.py:5: note: Revealed type is "def (*args: Any, **kwargs: Any) -> transformers.utils.dummy_pt_objects.Trainer"
script.py:8: note: Revealed type is "transformers.utils.dummy_pt_objects.Trainer"
script.py:9: error: "Trainer" has no attribute "train"  [attr-defined]
script.py:9: note: Revealed type is "Any"
script.py:10: error: "Trainer" has no attribute "train"  [attr-defined]
script.py:11: error: "PreTrainedModel" has no attribute "dequantize"  [attr-defined]

Theses errors are false positives, it seems like there is a config issue, all types are pulled from dummy_pt_objects.py


  • transformers version: 4.51.0
  • Platform: macOS-14.7.3-arm64-arm-64bit
  • Python version: 3.12.3
@jc-louis jc-louis added the bug label Apr 7, 2025
@cyyever
Copy link
Contributor

cyyever commented Apr 7, 2025

Try

from transformers.trainer import Trainer    
from transformers.modeling_utils  import PreTrainedModel

@jc-louis
Copy link
Author

jc-louis commented Apr 7, 2025

@cyyever this does indeed fix the issue, thanks!

However, doing from transformers import Trainer is the recommended way in the docs, is there any fix planed?

@cyyever
Copy link
Contributor

cyyever commented Apr 7, 2025

We should add __all__ to all exported symbols.

@cyyever cyyever linked a pull request Apr 7, 2025 that will close this issue
@cyyever
Copy link
Contributor

cyyever commented Apr 7, 2025

@jc-louis I submitted a PR for this issue. Can you install it and check?

@jc-louis
Copy link
Author

jc-louis commented Apr 7, 2025

@cyyever I confirm it does fix the issue 👍

@jonoillar
Copy link

Does someone know if mypy lets you ignore errors related to a specific python object ?

Like when I get those errors:

error: "PreTrainedModel" has no attribute "generate"  [attr-defined]
error: "PreTrainedModel" has no attribute "eval"  [attr-defined]
error: "PreTrainedModel" not callable  [operator]

I would simply like to ignore the errors "attr-defined", "operator" related to "PreTrainedModel"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants