#πŸ”’ Set type hinting in IDE when an object is loaded from a unknown function?

27 messages Β· Page 1 of 1 (latest)

hearty charm
#

I have some generic code for loading config in different projects, and part of that monkey patches a class with a load() class function. The problem is that the IDE doesn't see that connection and will always tag the variable as "Any" - even with type hinting on the variable.

CONF: Config = Config.load()

With that, since load() is unknown, the type of CONF will always be "any", and autocomplete suggestions will always be empty. Is there a way to tell the typing system / IDE that "This IS an instance of this class, really"?

I'm using VS Code with Python plugins for IDE

wispy patioBOT
#

@hearty charm

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

signal coral
hearty charm
#

Hm.. Not here, it still identifies it as Any. Which IDE do you use?

eager solar
#

Where is Config defined? Is it in a separate package?

hearty charm
#
# pip install git+https://github.com/TheTerrasque/python-configclass
from configclass import configclass

@configclass()
class ExampleConfig:
    setting1: int
    setting2: str

CONF: ExampleConfig = ExampleConfig.load()
hearty charm
#

If I could fix the configclass code to give right return value that would be the best, but last I looked into it it was ... complicated.. to say the least

eager solar
#

Can you try doing reveal_type(Config)? It’ll produce an error, but also list what type Pyright thinks the class is.

hearty charm
hearty charm
eager solar
#

reveal_type is a special function type checkers understand.

hearty charm
#

aah, cool.

eager solar
#

Okay so this is probably an issue with it following the import, not with the specific load line.

hearty charm
eager solar
#

What about reveal_type(ExampleConfig), or reveal_type(configclass)?

#

You may need to go add type hints to your package to get this to work right. In particular, decorate configclass with typing.dataclass_transform(), which tells type checkers to treat your function like @dataclass.

hearty charm
eager solar
#

The other thing is to instead of making load a method on the class, instead just have it as a function in your module that gets passed in the class. Then you could define it like so:

def load[ClassT](cls: type[ClassT], data: dict | None = None) -> ClassT:
    ...
hearty charm
#

is there a way to tell typing the dataclass will have an attached function that returns an instance of the class?

eager solar
#

Not currently, that'd need an intersection feature in typing.

hearty charm
#

ok

eager solar
#

It's part of the reason why dataclasses doesn't add methods itself, instead using functions. Also this way you can define an option named load.

hearty charm
#

I'll make a load function following your suggestion, thanks. Too bad, I really liked the elegance of the class.load() :/

wispy patioBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.