Skip to content

self.central_widget.findChildren() has inaccurate output type #216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
HoseynAAmiri opened this issue Dec 28, 2024 · 2 comments
Open

self.central_widget.findChildren() has inaccurate output type #216

HoseynAAmiri opened this issue Dec 28, 2024 · 2 comments

Comments

@HoseynAAmiri
Copy link

HoseynAAmiri commented Dec 28, 2024

if I install the stubs the returned type seems incorrect. For instance, given the code below:

input_widgets = self.central_widget.findChildren(
    (QPushButton, QSlider, QLineEdit, QComboBox))
for widget in input_widgets:
    widget.setEnabled(activate)

pylance shows: (variable) input_widgets: List[QObject] and I see this error:
Cannot access attribute "setEnabled" for class "QObject"
Attribute "setEnabled" is unknown

while without stubs,

pylance says: (variable) input_widgets: List[QPushButton | QSlider | QLineEdit | QComboBox] and I can autocomplete setEnabled()

I had to install PyQt5-stubs because I noticed that if I inherit from QMainWindow and and use it later like this:

from dataclasses import dataclass
from PyQt5.QtWidgets import QMainWindow

class MainWindow(QMainWindow):
    attr1: int
    def __init__(self):
        super().__init__()

@dataclass
class SubWindow:
    main: MainWindow


sub = SubWindow(MainWindow())

pylance cannot see main in SubWindow properly without stubs so you cannot autocomplete attr1 for instance. I tested defining Subwindow regularly without dataclass but it didn't help. The problem will be fixed only if I remove QMainWindow. I don't know why!

But installing stubs fixes the later issue. Yet it causes the former issue of getting objects from children and not the exact queries.

I tested the same code with PyQt6 without stubs, and it worked.

@altendky
Copy link
Collaborator

It does indeed appear that this could be made more aware potentially. I'm not exactly sure offhand how to express this without as many typevars being defined as we want to support, but maybe there's a way. That said, I'm not betting that I personally will get this improved. But who knows, maybe someone, or I, will. Thanks for pointing it out.

@typing.overload
def findChildren(self, type: typing.Type[QObjectT], name: str = ..., options: typing.Union[Qt.FindChildOptions, Qt.FindChildOption] = ...) -> typing.List[QObjectT]: ...
@typing.overload
def findChildren(self, types: typing.Tuple[typing.Type[QObject], ...], name: str = ..., options: typing.Union[Qt.FindChildOptions, Qt.FindChildOption] = ...) -> typing.List['QObject']: ...
@typing.overload
def findChildren(self, type: typing.Type[QObjectT], regExp: 'QRegExp', options: typing.Union[Qt.FindChildOptions, Qt.FindChildOption] = ...) -> typing.List[QObjectT]: ...
@typing.overload
def findChildren(self, types: typing.Tuple[typing.Type[QObject], ...], regExp: 'QRegExp', options: typing.Union[Qt.FindChildOptions, Qt.FindChildOption] = ...) -> typing.List['QObject']: ...
@typing.overload
def findChildren(self, type: typing.Type[QObjectT], re: 'QRegularExpression', options: typing.Union[Qt.FindChildOptions, Qt.FindChildOption] = ...) -> typing.List[QObjectT]: ...
@typing.overload
def findChildren(self, types: typing.Tuple[typing.Type[QObject], ...], re: 'QRegularExpression', options: typing.Union[Qt.FindChildOptions, Qt.FindChildOption] = ...) -> typing.List['QObject']: ...
@typing.overload
def findChild(self, type: typing.Type[QObjectT], name: str = ..., options: typing.Union[Qt.FindChildOptions, Qt.FindChildOption] = ...) -> typing.Optional[QObjectT]: ...
@typing.overload
def findChild(self, types: typing.Tuple[typing.Type[QObject], ...], name: str = ..., options: typing.Union[Qt.FindChildOptions, Qt.FindChildOption] = ...) -> 'typing.Optional[QObject]': ...

@HoseynAAmiri
Copy link
Author

I moved to PyQt6. It has less type errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants