nle244 / 4GP-Password-Manager

MIT License
3 stars 0 forks source link

Allow type hinting for `ctrl` member variable in MainWindow #3

Open dkim286 opened 2 years ago

dkim286 commented 2 years ago

Description

Code completion tools (such as IntelliSense) are unable to recognize the ctrl member variable's type:

image

This does not break anything, but it's annoying to look at.

Cause

Code completion tool wrongly believes that ctrl is None due to its default value:

image

Fix

Use typing library to allow for type hinting without running into potential circular dependency issues.

Before:

class MainWindow(ttk.Frame):
    def __init__(self, parent):
        super().__init__(parent)

        self.__create_home_widgets()
        self.__ctrl = None

    @property
    def ctrl(self):
        '''Controller.'''
        return self.__ctrl

'''snip'''

Suggestion:

from typing import TYPE_CHECKING
if TYPE_CHECKING:
    from pm.controller import Controller

class MainWindow(ttk.Frame):
    def __init__(self, parent):
        super().__init__(parent)

        self.__create_home_widgets()
        self.__ctrl = None

    @property
    def ctrl(self) -> 'Controller':
        '''Controller.'''
        return self.__ctrl

'''snip'''

More info: https://adamj.eu/tech/2021/05/13/python-type-hints-how-to-fix-circular-imports/