Incorrect type hints for LightningModule.load_from_checkpoint
See original GitHub issueBug 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
Issue Analytics
- State:
- Created 10 months ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
Incorrect type annotation for DataLoader · Issue #52806 - GitHub
Incorrect type annotation for DataLoader #52806 ... Fix type hints of the callable arguments for DataLoader #52924.
Read more >Tests aren't enough: Case study after adding type hints to urllib3
Even when using type hints the “type safety” they provide is completely opt-in with tools like Mypy or Pyright. You can continue incorrectly...
Read more >How to handle incorrect type hint warning from Pylance when ...
I have a Python function whose return type varies depending on the values of the arguments. Pylance type-checking seems to require that ...
Read more >Some Thoughts About Using Type Hints With PyTorch
The main disadvantage is that using type hints clutters up the code, making it more difficult to read.
Read more >PEP 484 – Type Hints - Python Enhancement Proposals
In its basic form, type hinting is used by filling function annotation slots with classes: ... T = TypeVar('T') class Pair(Generic[T, T]): #...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@adamjstewart This was recently added in #15496. See the explanation here: https://github.com/Lightning-AI/lightning/pull/15496#discussion_r1012788951
cc @carmocca
Decided to manually cast things to the right type for now. Will look out for the new mypy release. Thanks for the pointers!