From 514f3fac8a85d5d543abfaed9af7181b1a4c89c9 Mon Sep 17 00:00:00 2001 From: Athanasius Date: Sat, 3 Dec 2022 12:16:44 +0000 Subject: [PATCH] myNotebook.py: Now passes flake8 & mypy * Typed `master` to each `__init__()`. * Having to `# type: ignore` the base class for the classes which are "tk on darwin, else ttk" as this 'dynamic' type confuses mypy. See the comment on `class Frame` for a suggested proper fix, which will be more work. --- myNotebook.py | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/myNotebook.py b/myNotebook.py index 7259f93e..eb17bf2f 100644 --- a/myNotebook.py +++ b/myNotebook.py @@ -13,6 +13,7 @@ Entire file may be imported by plugins. import sys import tkinter as tk from tkinter import ttk +from typing import Optional # Can't do this with styles on OSX - http://www.tkdocs.com/tutorial/styles.html#whydifficult if sys.platform == 'darwin': @@ -28,7 +29,7 @@ elif sys.platform == 'win32': class Notebook(ttk.Notebook): """Custom ttk.Notebook class to fix some display issues.""" - def __init__(self, master=None, **kw): + def __init__(self, master: Optional[ttk.Frame] = None, **kw): ttk.Notebook.__init__(self, master, **kw) style = ttk.Style() @@ -51,10 +52,13 @@ class Notebook(ttk.Notebook): self.grid(padx=10, pady=10, sticky=tk.NSEW) -class Frame(sys.platform == 'darwin' and tk.Frame or ttk.Frame): +# FIXME: The real fix for this 'dynamic type' would be to split this whole +# thing into being a module with per-platform files, as we've done with config +# That would also make the code cleaner. +class Frame(sys.platform == 'darwin' and tk.Frame or ttk.Frame): # type: ignore """Custom t(t)k.Frame class to fix some display issues.""" - def __init__(self, master=None, **kw): + def __init__(self, master: Optional[ttk.Frame] = None, **kw): if sys.platform == 'darwin': kw['background'] = kw.pop('background', PAGEBG) tk.Frame.__init__(self, master, **kw) @@ -71,7 +75,7 @@ class Frame(sys.platform == 'darwin' and tk.Frame or ttk.Frame): class Label(tk.Label): """Custom tk.Label class to fix some display issues.""" - def __init__(self, master=None, **kw): + def __init__(self, master: Optional[ttk.Frame] = None, **kw): if sys.platform in ['darwin', 'win32']: kw['foreground'] = kw.pop('foreground', PAGEFG) kw['background'] = kw.pop('background', PAGEBG) @@ -81,10 +85,10 @@ class Label(tk.Label): tk.Label.__init__(self, master, **kw) # Just use tk.Label on all platforms -class Entry(sys.platform == 'darwin' and tk.Entry or ttk.Entry): +class Entry(sys.platform == 'darwin' and tk.Entry or ttk.Entry): # type: ignore """Custom t(t)k.Entry class to fix some display issues.""" - def __init__(self, master=None, **kw): + def __init__(self, master: Optional[ttk.Frame] = None, **kw): if sys.platform == 'darwin': kw['highlightbackground'] = kw.pop('highlightbackground', PAGEBG) tk.Entry.__init__(self, master, **kw) @@ -92,10 +96,10 @@ class Entry(sys.platform == 'darwin' and tk.Entry or ttk.Entry): ttk.Entry.__init__(self, master, **kw) -class Button(sys.platform == 'darwin' and tk.Button or ttk.Button): +class Button(sys.platform == 'darwin' and tk.Button or ttk.Button): # type: ignore """Custom t(t)k.Button class to fix some display issues.""" - def __init__(self, master=None, **kw): + def __init__(self, master: Optional[ttk.Frame] = None, **kw): if sys.platform == 'darwin': kw['highlightbackground'] = kw.pop('highlightbackground', PAGEBG) tk.Button.__init__(self, master, **kw) @@ -105,10 +109,10 @@ class Button(sys.platform == 'darwin' and tk.Button or ttk.Button): ttk.Button.__init__(self, master, **kw) -class ColoredButton(sys.platform == 'darwin' and tk.Label or tk.Button): +class ColoredButton(sys.platform == 'darwin' and tk.Label or tk.Button): # type: ignore """Custom t(t)k.ColoredButton class to fix some display issues.""" - def __init__(self, master=None, **kw): + def __init__(self, master: Optional[ttk.Frame] = None, **kw): if sys.platform == 'darwin': # Can't set Button background on OSX, so use a Label instead kw['relief'] = kw.pop('relief', tk.RAISED) @@ -123,10 +127,10 @@ class ColoredButton(sys.platform == 'darwin' and tk.Label or tk.Button): self._command() -class Checkbutton(sys.platform == 'darwin' and tk.Checkbutton or ttk.Checkbutton): +class Checkbutton(sys.platform == 'darwin' and tk.Checkbutton or ttk.Checkbutton): # type: ignore """Custom t(t)k.Checkbutton class to fix some display issues.""" - def __init__(self, master=None, **kw): + def __init__(self, master: Optional[ttk.Frame] = None, **kw): if sys.platform == 'darwin': kw['foreground'] = kw.pop('foreground', PAGEFG) kw['background'] = kw.pop('background', PAGEBG) @@ -137,10 +141,10 @@ class Checkbutton(sys.platform == 'darwin' and tk.Checkbutton or ttk.Checkbutton ttk.Checkbutton.__init__(self, master, **kw) -class Radiobutton(sys.platform == 'darwin' and tk.Radiobutton or ttk.Radiobutton): +class Radiobutton(sys.platform == 'darwin' and tk.Radiobutton or ttk.Radiobutton): # type: ignore """Custom t(t)k.Radiobutton class to fix some display issues.""" - def __init__(self, master=None, **kw): + def __init__(self, master: Optional[ttk.Frame] = None, **kw): if sys.platform == 'darwin': kw['foreground'] = kw.pop('foreground', PAGEFG) kw['background'] = kw.pop('background', PAGEBG) @@ -151,7 +155,7 @@ class Radiobutton(sys.platform == 'darwin' and tk.Radiobutton or ttk.Radiobutton ttk.Radiobutton.__init__(self, master, **kw) -class OptionMenu(sys.platform == 'darwin' and tk.OptionMenu or ttk.OptionMenu): +class OptionMenu(sys.platform == 'darwin' and tk.OptionMenu or ttk.OptionMenu): # type: ignore """Custom t(t)k.OptionMenu class to fix some display issues.""" def __init__(self, master, variable, default=None, *values, **kw):