Lightning-AI / Lightning-AI/pytorch-lightning

Typehints issues with hparams using pylance

Open
#17,852 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug code quality ver: 2.0.x
Dominant language
Python
Stars
31.4k
Forks
3.8k
Avg merge
6d 7h
Merged PRs (30d)
6

Description

### Bug description

Pylance linting/static type checking/etc. is broken with PL code using the `hparams`. As discussed in https://github.com/microsoft/pylance-release/issues/4506 @erictraut and @hmc-cs-mdrissi took at look at the PL source to see what was going on and suggested this could be considered a typehint bug for the PL source (in particular the `AttributeDict` and `MutableMapping`). See [that issue](https://github.com/microsoft/pylance-release/issues/4506#issuecomment-1594254522) for more detailed comments, but I think the basic statement is that since `AttributeDict <: MutableMapping` it would be better to set the returntype of `_to_hparams_dict` as `AttributeDict | Any ` instead of the current code.

### What version are you seeing the problem on?

v2.0

### How to reproduce the bug

```python
Create a file

import pytorch_lightning as pl
class MyModule(pl.LightningModule):
def __init__(self,
N: int):
super().__init__()
self.save_hyperparameters()
def f(self):
print(self.hparams.N)

model = MyModule(N=10)
model.f()
```
Use vscode with the https://marketplace.visualstudio.com/items?itemName=ms-python.vscode-pylance extension. Nothing special about pylance here, it is just the new languageserver implementation.

If you put that code in a `.py` file you will see in the errors pane things like
```
Cannot access member "N" for type "MutableMapping[Unknown, Unknown]"
Member "N" is unknown
```

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the `_to_hparams_dict` return type and the `AttributeDict`/`MutableMapping` definitions, then read the linked Pylance discussion for the typing context. Reproduce the example in a Python file with Pylance and confirm that `self.hparams.N` no longer reports the shown member-access error.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
developer-experience, machine-learning
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.