Skip to content
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

Make codebase work with basic mypy usage #1084

Merged
merged 2 commits into from
Aug 4, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/blueman-assistant.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import os
import sys
import signal
import logging
from locale import bind_textdomain_codeset
from gettext import bind_textdomain_codeset

import gi
gi.require_version("Gtk", "3.0")
Expand Down
2 changes: 0 additions & 2 deletions blueman/Constants.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ RFCOMM_WATCHER_PATH = "@LIBEXECDIR@/blueman-rfcomm-watcher"

import os
import gettext
import builtins

translation = gettext.translation(GETTEXT_PACKAGE, LOCALEDIR, fallback=True)
translation.install()
builtins.ngettext = translation.ngettext

if 'BLUEMAN_SOURCE' in os.environ:
_dirname = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
Expand Down
1 change: 1 addition & 0 deletions blueman/DeviceClass.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# coding=utf-8
from gettext import gettext as _
import logging

service_cls = [
Expand Down
6 changes: 2 additions & 4 deletions blueman/Sdp.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
# coding=utf-8
import gettext
from gettext import gettext as _
from uuid import UUID

from blueman.Constants import GETTEXT_PACKAGE, LOCALEDIR

translation = gettext.translation(GETTEXT_PACKAGE, LOCALEDIR, fallback=True)
try:
translation.install(unicode=True)
except TypeError:
translation.install()
translation.install()

# https://www.bluetooth.com/specifications/assigned-numbers/service-discovery
# http://git.kernel.org/cgit/bluetooth/bluez.git/tree/lib/sdp.h
Expand Down
8 changes: 4 additions & 4 deletions blueman/Service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@


class Service(object):
__group__ = None
__svclass_id__ = None
__group__: str
__svclass_id__: int
__description__ = None
__icon__ = None
__priority__ = None
__icon__: str
__priority__: int
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional[str] = None


def __init__(self, device, uuid):
self.__device = device
Expand Down
4 changes: 3 additions & 1 deletion blueman/bluez/Base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# coding=utf-8
from typing import Dict, Any, Tuple

from gi.repository import Gio, GLib, GObject
from gi.types import GObjectMeta
from blueman.bluez.errors import parse_dbus_error
Expand Down Expand Up @@ -47,7 +49,7 @@ class Base(Gio.DBusProxy, metaclass=BaseMeta):
__name = 'org.bluez'
__bus_type = Gio.BusType.SYSTEM

__gsignals__ = {
__gsignals__: Dict[str, Tuple[Any, GObject.SignalFlags, Tuple]] = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first Tuple starts with GObject.SignalFlags not Any, next is None and then a tuple. For the second tuple we know the first is a str and we know what types to expect from dbus properties which we can represent as a Union. So I think it should be the below which works for Base.
Dict[str, Tuple[GObject.SignalFlags, None, Tuple[str, Union[str, int, bool, List[str], Dict[str, GLib.Variant], Dict[int, GLib.Variant]], str]]]].

But not for subclasses that add their own signals so they will need their own type hints.

'property-changed': (GObject.SignalFlags.NO_HOOKS, None,
(GObject.TYPE_PYOBJECT, GObject.TYPE_PYOBJECT, GObject.TYPE_PYOBJECT))
}
Expand Down
2 changes: 2 additions & 0 deletions blueman/gui/DeviceSelectorDialog.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# coding=utf-8
from gettext import gettext as _

from blueman.gui.DeviceSelectorWidget import DeviceSelectorWidget

import gi
Expand Down
2 changes: 1 addition & 1 deletion blueman/gui/GsmSettings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# coding=utf-8
from locale import bind_textdomain_codeset
from gettext import bind_textdomain_codeset

from blueman.main.Config import Config
from blueman.Constants import *
Expand Down
2 changes: 1 addition & 1 deletion blueman/gui/applet/PluginDialog.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# coding=utf-8
import weakref
import logging
from locale import bind_textdomain_codeset
from gettext import bind_textdomain_codeset

from blueman.Constants import *
from blueman.gui.GenericList import GenericList
Expand Down
8 changes: 5 additions & 3 deletions blueman/gui/manager/ManagerDeviceMenu.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# coding=utf-8
import logging
from locale import bind_textdomain_codeset
from gettext import bind_textdomain_codeset
from operator import itemgetter
from typing import Dict, List

from blueman.Constants import UI_PATH
from blueman.Functions import create_menuitem, e_
from blueman.bluez.Network import AnyNetwork
Expand All @@ -27,8 +29,8 @@


class ManagerDeviceMenu(Gtk.Menu):
__ops__ = {}
__instances__ = []
__ops__: Dict[str, str] = {}
__instances__: List["ManagerDeviceMenu"] = []

def __init__(self, blueman):
super().__init__()
Expand Down
1 change: 1 addition & 0 deletions blueman/gui/manager/ManagerMenu.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# coding=utf-8
from gettext import gettext as _
import logging

import blueman.bluez as bluez
Expand Down
4 changes: 3 additions & 1 deletion blueman/gui/manager/ManagerProgressbar.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# coding=utf-8
from gettext import gettext as _
import logging
from typing import List

import gi
gi.require_version("Gtk", "3.0")
Expand All @@ -15,7 +17,7 @@ class ManagerProgressbar(GObject.GObject):
__gsignals__ = {
'cancelled': (GObject.SignalFlags.RUN_LAST, None, ()),
}
__instances__ = []
__instances__: List["ManagerProgressbar"] = []

def __init__(self, blueman, cancellable=True, text=_("Connecting")):
super().__init__()
Expand Down
2 changes: 1 addition & 1 deletion blueman/main/DBusProxies.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# coding=utf-8
from gi.repository import Gio, GLib
from gi.repository import Gio, GLib # type: ignore
from gi.types import GObjectMeta


Expand Down
4 changes: 3 additions & 1 deletion blueman/main/DhcpClient.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# coding=utf-8
from typing import List

from gi.repository import GObject
from gi.repository import GLib
import socket
Expand All @@ -20,7 +22,7 @@ class DhcpClient(GObject.GObject):
["udhcpc", "-t", "20", "-x", "hostname", socket.gethostname(), "-n", "-i"]
]

quering = []
quering: List[str] = []

def __init__(self, interface, timeout=30):
super().__init__()
Expand Down
2 changes: 1 addition & 1 deletion blueman/main/Manager.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# coding=utf-8
import logging
from locale import bind_textdomain_codeset
from gettext import bind_textdomain_codeset

import blueman.bluez as bluez
from blueman.Functions import *
Expand Down
2 changes: 1 addition & 1 deletion blueman/main/NetworkManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class NMConnectionError(Exception):


class NMConnectionBase(object):
conntype = None
conntype: str
Copy link
Contributor

@infirit infirit Aug 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

conntype: Optional[str] = None


def __init__(self, service, reply_handler=None, error_handler=None):
if self.conntype not in ('dun', 'panu'):
Expand Down
4 changes: 0 additions & 4 deletions blueman/main/PluginManager.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# coding=utf-8
import os
import builtins
import logging
import traceback
import importlib
Expand All @@ -19,9 +18,6 @@ class LoadException(Exception):
pass


builtins.StopException = StopException


class PluginManager(GObject.GObject):
__gsignals__ = {
'plugin-loaded': (GObject.SignalFlags.NO_HOOKS, None, (GObject.TYPE_STRING,)),
Expand Down
2 changes: 1 addition & 1 deletion blueman/main/Sendto.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# coding=utf-8
import time
import logging
from locale import bind_textdomain_codeset
from gettext import bind_textdomain_codeset
from gettext import ngettext

import gi
Expand Down
3 changes: 2 additions & 1 deletion blueman/main/applet/BluezAgent.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# coding=utf-8
import os.path
import logging
from locale import bind_textdomain_codeset
from gettext import bind_textdomain_codeset
from gettext import gettext as _
from html import escape
import random
from xml.etree import ElementTree
Expand Down
31 changes: 25 additions & 6 deletions blueman/plugins/BasePlugin.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,48 @@
# coding=utf-8
import logging
import weakref
from typing import List, TYPE_CHECKING, Type, Dict, Tuple, Any

from blueman.main.Config import Config


class MethodAlreadyExists(Exception):
pass


if TYPE_CHECKING:
from mypy_extensions import TypedDict

class OptionBase(TypedDict):
type: Type
default: Any

class Option(OptionBase, total=False):
name: str
desc: str
range: Tuple[int, int]

class GSettings(TypedDict):
schema: str
path: None
Copy link
Contributor

@infirit infirit Aug 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

path: Optional[str] = None



class BasePlugin(object):
__depends__ = []
__conflicts__ = []
__depends__: List[str] = []
__conflicts__: List[str] = []
__priority__ = 0

__description__ = None
__author__ = None
__description__: str
__author__: str
Copy link
Contributor

@infirit infirit Aug 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional[str] = None


__unloadable__ = True
__autoload__ = True

__instance__ = None

__gsettings__ = None
__gsettings__: "GSettings"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__gsettings__: Optional["Gsettings"] = None


__options__ = {}
__options__: Dict[str, "Option"] = {}

def __init__(self, parent):
self.parent = parent
Expand Down
5 changes: 3 additions & 2 deletions blueman/plugins/ServicePlugin.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# coding=utf-8
from typing import List, Tuple


class ServicePlugin(object):
instances = []
__plugin_info__ = None
instances: List["ServicePlugin"] = []
__plugin_info__: Tuple[str, str]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__plugin_info__: Optional[Tuple[str, str]] = None


def __init__(self, parent):
ServicePlugin.instances.append(self)
Expand Down
2 changes: 2 additions & 0 deletions blueman/plugins/applet/AppIndicator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# coding=utf-8
from gettext import gettext as _

from blueman.plugins.AppletPlugin import AppletPlugin

# Check if Appindicator is available and raise ImportError
Expand Down
2 changes: 2 additions & 0 deletions blueman/plugins/applet/AuthAgent.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# coding=utf-8
from gettext import gettext as _

from blueman.plugins.AppletPlugin import AppletPlugin
from blueman.main.applet.BluezAgent import BluezAgent

Expand Down
2 changes: 2 additions & 0 deletions blueman/plugins/applet/DBusService.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# coding=utf-8
from gettext import gettext as _

from blueman.main.PluginManager import StopException
from blueman.plugins.AppletPlugin import AppletPlugin
from blueman.bluez.Device import Device
Expand Down
2 changes: 2 additions & 0 deletions blueman/plugins/applet/DhcpClient.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# coding=utf-8

from gettext import gettext as _
import logging

from blueman.bluez.Network import AnyNetwork
from blueman.gui.Notification import Notification
from blueman.plugins.AppletPlugin import AppletPlugin
Expand Down
2 changes: 2 additions & 0 deletions blueman/plugins/applet/DiscvManager.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# coding=utf-8
from gettext import gettext as _

from blueman.bluez.errors import DBusNoSuchAdapterError
from blueman.plugins.AppletPlugin import AppletPlugin
from gi.repository import GLib
Expand Down
2 changes: 2 additions & 0 deletions blueman/plugins/applet/ExitItem.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# coding=utf-8
from gettext import gettext as _

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
Expand Down
1 change: 1 addition & 0 deletions blueman/plugins/applet/GameControllerWakelock.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# coding=utf-8
from gettext import gettext as _
import logging

import blueman.bluez as bluez
Expand Down
5 changes: 4 additions & 1 deletion blueman/plugins/applet/KillSwitch.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# coding=utf-8
from gettext import gettext as _
import os
import weakref
from typing import Dict

from gi.repository import GLib, Gio
import struct
import logging
Expand Down Expand Up @@ -47,7 +50,7 @@ class KillSwitch(AppletPlugin):
"checked": {"type": bool, "default": False}
}

_switches = {}
_switches: Dict[int, Switch] = {}
_iom = None
_fd = None
_enabled = True
Expand Down
2 changes: 2 additions & 0 deletions blueman/plugins/applet/Menu.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# coding=utf-8

from gettext import gettext as _

from gi.repository import GLib

from blueman.plugins.AppletPlugin import AppletPlugin
Expand Down
2 changes: 2 additions & 0 deletions blueman/plugins/applet/NMDUNSupport.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# coding=utf-8
from gettext import gettext as _

from blueman.plugins.AppletPlugin import AppletPlugin
from blueman.main.NetworkManager import NMDUNConnection
from blueman.Sdp import DIALUP_NET_SVCLASS_ID
Expand Down
2 changes: 2 additions & 0 deletions blueman/plugins/applet/NMPANSupport.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# coding=utf-8
from gettext import gettext as _

from blueman.plugins.AppletPlugin import AppletPlugin
from blueman.main.NetworkManager import NMPANConnection

Expand Down
Loading