Skip to content

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

@HoseynAAmiri

Description

@HoseynAAmiri

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions