Pylint incorrectly assumes that type is not subscriptable

Viewed 388

I have the following code:

config = wandb.config

do_something_with_config(config['bar'], config['foo'])

For some reason, every time I access a key of config, pylint gives an unsubscriptable-object error. I can add a # pylint: disable=unsubscriptable-object in every place I access a key of this object, but I'd really rather avoid that since I do so in a ton of places.

I tried changing it to config : Dict[object, str] = wandb.config and that did not resolve the problem. Is there anything I can do to inform mypy that this value is subscriptable?

1 Answers

This smells like a bug in pylint... there's a relevant issue at https://github.com/PyCQA/pylint/issues/1498.


One would expect pylint to work, especially when type-hinting the types of the objects.

After you call, wandb.init(), the wandb.config object is of type wandb.Config. You can set its type upon assignment.

import wandb

wandb.init()

config: wandb.Config = wandb.config
print(config["bar"], config["foo"])

Before you call wandb.init(), wandb.config is a function. The purpose of the function is to inform the user that wandb must be initialized.

Related