python-qt-tools / PyQt5-stubs

Stubs for PyQt5
GNU General Public License v3.0
69 stars 31 forks source link

Add stubs for uic #150

Open AranaFireheartSNHU opened 3 years ago

AranaFireheartSNHU commented 3 years ago

Steps

Install PyQt5 package and type the import statement: from PyQt5 import QtGui, uic PyCharm will offer to install PyQt5-stubs. Agree. Actual - uic submodule is marked as unresolved. The code runs.

Reported to JetBrains, and listed as an issue: https://youtrack.jetbrains.com/issue/PY-47685

BryceBeagle commented 3 years ago

Looks like we're missing the uic.pyi stub file. Not sure if it's being generated and not properly kept, or if it's not being generated at all.

1j01 commented 10 months ago

As a workaround, I added this to my app's code:

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    # PyQt5-stubs doesn't include the uic module
    # https://github.com/python-qt-tools/PyQt5-stubs/issues/150
    # I tried adding a stub for it, but then QtCore couldn't be resolved,
    # so it seems stubs can't overlap with other stub packages.
    # This is a simple workaround, and I could probably move it to a .pyi module,
    # and just add a different import here instead of defining a class as a stand-in for a module.
    class uic:
        @staticmethod
        def loadUi(uifile: str, baseinstance: QWidget|None = None, package: str = "", resource_suffix: str = "_rc") -> QWidget:
            """loadUi(uifile, baseinstance=None, package='') -> widget

            Load a Qt Designer .ui file and return an instance of the user interface.

            uifile is a file name or file-like object containing the .ui file.
            baseinstance is an optional instance of the Qt base class.  If specified
            then the user interface is created in it.  Otherwise a new instance of the
            base class is automatically created.
            package is the optional package which is used as the base for any relative
            imports of custom widgets.
            resource_suffix is the suffix appended to the basename of any resource file
            specified in the .ui file to create the name of the Python module generated
            from the resource file by pyrcc4.  The default is '_rc', i.e. if the .ui
            file specified a resource file called foo.qrc then the corresponding Python
            module is foo_rc.
            """
            ...

I'm no longer using PyQt5-stubs as I've upgraded to PyQt6, but this could be used as a headstart if someone wants to add this to the type stubs properly.