Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type hint a specific module in PyCharm

Consider the following code:

enter image description here

PyCharm automatically senses that this is a module that could refer to one or more backing libraries.

Is it possible to specify this manually for other module variables as well? Something like the following:

from pyqtgraph.Qt import QtWidgets
QtWidgets: Union[PyQt5.QtWidgets.pyi, PySide2.QtWidgets.pyi] # <-- I want to add this type hint

Behind the scenes, pyqtgraph is dynamically constructing dummy QtWidgets to point to one of several libraries at runtime, but PyCharm doesn't know this. Is there a way for me to manually add that type hint (QtWidgets.pyi) to a module that I import (In this case, pyqtgraph.Qt.QtWidgets)?

I posted here as well, for reference.

like image 414
ntjess Avatar asked Jan 27 '26 19:01

ntjess


1 Answers

enter image description here

As you can see here, it doesn't show me hints for sys.Test, as it doesn't exist. What you can do - you can use Stubs, aka .pyi files. i just added a sys.pyi file, [it must be named the same as your module, pyqtgraph.pyi in your case]. it doesn't evaluate the file but uses it to gather type hintings. the result is enter image description here

And the content of sys.pyi is:

Test: bytes

You can read about stubs, Here: https://www.jetbrains.com/help/pycharm/stubs.html

like image 79
Jonathan1609 Avatar answered Jan 29 '26 10:01

Jonathan1609