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

Incorrect type hints for LightningModule.load_from_checkpoint #15730

Closed
adamjstewart opened this issue Nov 18, 2022 · 5 comments
Closed

Incorrect type hints for LightningModule.load_from_checkpoint #15730

adamjstewart opened this issue Nov 18, 2022 · 5 comments
Labels
3rd party Related to a 3rd-party code quality

Comments

@adamjstewart
Copy link
Contributor

adamjstewart commented Nov 18, 2022

Bug description

The LightningModule.load_from_checkpoint function appears to return type Self? instead of LightningModule.

How to reproduce the bug

First, create a test.py file containing the following code:

import pytorch_lightning as pl

model1 = pl.LightningModule()
reveal_type(model1)
print(model1.hparams)

model2 = pl.LightningModule.load_from_checkpoint("/foo/bar")
reveal_type(model2)
print(model2.hparams)

Then, run mypy on this file:

$ mypy test.py
test.py:5: note: Revealed type is "pytorch_lightning.core.module.LightningModule"
test.py:9: note: Revealed type is "Self?"
test.py:10: error: Self? has no attribute "hparams"  [attr-defined]
Found 1 error in 1 file (checked 1 source file)

You'll notice that the type of the second model is incorrect, so mypy raises an error when you try to access LightningModule attributes like hparams.

Error messages and logs

We hit this issue in CI after the release of 1.8.1, so this may have been introduced in 1.8.1.

https://github.com/microsoft/torchgeo/actions/runs/3499809303/jobs/5861774881

Environment


#- Lightning Component (e.g. Trainer, LightningModule, LightningApp, LightningWork, LightningFlow): LightningModule
#- PyTorch Lightning Version (e.g., 1.5.0): 1.8.2
#- Lightning App Version (e.g., 0.5.2): N/A
#- PyTorch Version (e.g., 1.10): 1.12.1
#- Python version (e.g., 3.9): 3.10.8
#- OS (e.g., Linux): macOS and Linux
#- CUDA/cuDNN version: N/A
#- GPU models and configuration: N/A
#- How you installed Lightning(`conda`, `pip`, source): Spack
#- Running environment of LightningApp (e.g. local, cloud): local

More info

I'm happy to submit a PR to fix this issue if someone wants to point me in the right direction. I have dozens of other typing issues with PL, so would love to be able to fix those as well.

cc @Borda

@adamjstewart
Copy link
Contributor Author

I'm able to reproduce this outside of PL, so I opened an issue with typing-extensions: python/typing_extensions#96

@adamjstewart
Copy link
Contributor Author

adamjstewart commented Nov 18, 2022

Feedback from python/typing_extensions#96 is that the latest release of mypy does not yet know how to handle Self. Support was added in python/mypy#14041 which should be in the next release. So we either need to manually cast to the right type and wait for the next mypy release, or use TypeVar instead.

@awaelchli
Copy link
Contributor

@adamjstewart This was recently added in #15496. See the explanation here: #15496 (comment)

cc @carmocca

@awaelchli awaelchli added feature Is an improvement or enhancement code quality and removed needs triage Waiting to be triaged by maintainers labels Nov 20, 2022
@carmocca
Copy link
Contributor

carmocca commented Nov 21, 2022

Exactly. You can install the latest mypy by following the instructions at https://github.com/python/mypy#quick-start. I expect they'll make a release within the next month.

@carmocca carmocca added 3rd party Related to a 3rd-party and removed feature Is an improvement or enhancement labels Nov 21, 2022
@adamjstewart
Copy link
Contributor Author

Decided to manually cast things to the right type for now. Will look out for the new mypy release. Thanks for the pointers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3rd party Related to a 3rd-party code quality
Projects
None yet
Development

No branches or pull requests

3 participants