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

no-member false positive with super() with huggingface's transformers.Trainer #9973

Open
Hnasar opened this issue Sep 25, 2024 · 1 comment
Labels
Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling

Comments

@Hnasar
Copy link
Contributor

Hnasar commented Sep 25, 2024

Bug description

Pylint seems to improperly parse transformers.Trainer so it issues a false positive no-member for this code:

# test_module.py
from transformers import Trainer

class B(Trainer):
    def evaluation_loop(self):
        super().evaluation_loop()
        print('bye')

but the method definitely exists.

I thought it was maybe astroid failing to parse the Trainer class which has inline function imports

    from .trainer_pt_utils import _get_learning_rate, log_metrics, metrics_format, save_metrics, save_state

because I tried putting a breakpoint in pylint/checkers/typecheck.py before the _emit_no_member and noticing that owner.super_mro()[0].locals was missing nearly all of the methods,

but then I manually tried parsing the transformers/trainer.py then the method is defined.

import astroid
from pathlib import Path
print(list(astroid.parse(Path("venv/lib/python3.11/site-packages/transformers/trainer.py").read_text()).get_children())[-1].locals)

So there seems to be some other bug with how pylint is analyzing this code.

Configuration

No response

Command used

pylint test_module.py

Pylint output

************* Module test_module
test_module.py:5:8: E1101: Super of 'B' has no 'evaluation_loop' member (no-member)

Expected behavior

No error.

Pylint version

pylint 4.0.0-dev0
astroid 3.3.2
Python 3.11.10 (main, Sep  9 2024, 05:30:15) [GCC 11.2.0]

OS / Environment

Running from commit
0972ba5

but it's also reproducible with pylint 3.2.6

Additional dependencies

transformers==4.45.0
@Hnasar Hnasar added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Sep 25, 2024
@Hnasar Hnasar changed the title no-member false positive with super() from with huggingface's transformers.Trainer no-member false positive with super() with huggingface's transformers.Trainer Sep 26, 2024
@Hnasar
Copy link
Contributor Author

Hnasar commented Sep 26, 2024

In further investigation, it was pointed out to me that there's an alternate Trainer class
https://github.com/huggingface/transformers/blob/main/src/transformers/utils/dummy_pt_objects.py#L10029

# transformers/__init__.py
    try:
        if not is_torch_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        from .utils.dummy_pt_objects import *   # alternate Trainer class
    else:
        …
        from .trainer import Trainer
# transformers/utils/dummy_pt_objects.py
class Trainer(metaclass=DummyObject):
    _backends = ["torch"]

    def __init__(self, *args, **kwargs):
        requires_backends(self, ["torch"])

So the workaround is to explicitly import from transformers.trainer import Trainer but still it would be nice if pylint handled this case better — maybe it could always assume is_torch_available is True

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling
Projects
None yet
Development

No branches or pull requests

1 participant