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

Add mypy type annotations via monkeytype #344

Merged
8 changes: 8 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[flake8]
ignore = W291,W503,E265,E266,E302,E722
max-line-length = 127
exclude =
.git,
__pycache__,
.mypy_cache,
.pytest_cache,
7 changes: 5 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.6, 3.7, 3.8, 3.9]
python-version: ['3.6', '3.7', '3.8', '3.9', '3.10']
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
Expand All @@ -37,13 +37,16 @@ jobs:
pip install netifaces
pip install python-dotenv
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
if [ -f requirements-mypy.txt ]; then pip install -r requirements-mypy.txt; fi
pip install .
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Type check with mypy
run: mypy -p BAC0
- name: Test with pytest
run: |
coverage run --source BAC0 -m pytest -v
Expand All @@ -55,7 +58,7 @@ jobs:
GITHUB_TOKEN: ${{ secrets.COVERALLS_TOKEN }}
COVERALLS_FLAG_NAME: ${{ matrix.test-name }}


# coveralls:
# name: Indicate completion to coveralls.io
# needs: build
Expand Down
2 changes: 1 addition & 1 deletion BAC0/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@

connect = gui
else:
connect = lite
connect = lite # type: ignore[assignment, misc]
web = lambda: print(
"All features not available to run BAC0.web(). Some modules are missing (flask, flask-bootstrap, bokeh, pandas). See docs for details. To start BAC0, use BAC0.lite()"
)
Expand Down
31 changes: 17 additions & 14 deletions BAC0/core/app/ScriptApplication.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,38 @@
# Licensed under LGPLv3, see file LICENSE in this source tree.
#
"""
SimpleApplication
SimpleApplication
=================

A basic BACnet application (bacpypes BIPSimpleApplication) for interacting with
the bacpypes BACnet stack. It enables the base-level BACnet functionality
A basic BACnet application (bacpypes BIPSimpleApplication) for interacting with
the bacpypes BACnet stack. It enables the base-level BACnet functionality
(a.k.a. device discovery) - meaning it can send & receive WhoIs & IAm messages.

Additional functionality is enabled by inheriting this application, and then
Additional functionality is enabled by inheriting this application, and then
extending it with more functions. [See BAC0.scripts for more examples of this.]

"""
# --- standard Python modules ---
from collections import defaultdict
import typing as t

# --- 3rd party modules ---
from bacpypes.app import ApplicationIOController
from bacpypes.pdu import Address
from bacpypes.service.object import ReadWritePropertyMultipleServices
from bacpypes.service.cov import ChangeOfValueServices
from bacpypes.netservice import NetworkServiceAccessPoint, NetworkServiceElement
from bacpypes.netservice import NetworkServiceAccessPoint
from bacpypes.bvllservice import (
BIPSimple,
BIPForeign,
BIPBBMD,
AnnexJCodec,
UDPMultiplexer,
)
from bacpypes.apdu import SubscribeCOVRequest, SimpleAckPDU, RejectPDU, AbortPDU
from bacpypes.apdu import IAmRequest, SimpleAckPDU

from bacpypes.appservice import StateMachineAccessPoint, ApplicationServiceAccessPoint
from bacpypes.comm import ApplicationServiceElement, bind, Client
from bacpypes.comm import bind, Client
from bacpypes.iocb import IOCB
from bacpypes.core import deferred

Expand All @@ -46,6 +47,8 @@
# --- this application's modules ---
from ..utils.notes import note_and_log
from ..functions.Discover import NetworkServiceElementWithRequests
from bacpypes.local.device import LocalDeviceObject
from typing import Any, Dict, Optional

# ------------------------------------------------------------------------------

Expand Down Expand Up @@ -168,15 +171,15 @@ class BAC0Application(

def __init__(
self,
localDevice,
localAddress,
localDevice: LocalDeviceObject,
localAddress: Address,
bbmdAddress=None,
bbmdTTL=0,
bbmdTTL: int = 0,
deviceInfoCache=None,
aseID=None,
iam_req=None,
subscription_contexts=None,
):
iam_req: Optional[IAmRequest] = None,
subscription_contexts: Optional[Dict[Any, Any]] = None,
) -> None:

ApplicationIOController.__init__(
self, localDevice, deviceInfoCache, aseID=aseID
Expand Down Expand Up @@ -222,7 +225,7 @@ def __init__(
# bind the BIP stack to the network, no network number
self.nsap.bind(self.bip, address=self.localAddress)

self.i_am_counter = defaultdict(int)
self.i_am_counter: t.Dict[t.Tuple[str, int], int] = defaultdict(int)
self.i_have_counter = defaultdict(int)
self.who_is_counter = defaultdict(int)

Expand Down
23 changes: 11 additions & 12 deletions BAC0/core/devices/Device.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
"""
# --- standard Python modules ---
from collections import namedtuple
from datetime import datetime
import weakref

import os.path

Expand All @@ -21,10 +19,9 @@
_PANDAS = True
except ImportError:
_PANDAS = False
import logging

try:
from xlwings import Workbook, Sheet, Range, Chart
from xlwings import Workbook, Sheet, Range, Chart # noqa E401

_XLWINGS = True
except ImportError:
Expand Down Expand Up @@ -176,7 +173,9 @@ def __init__(
self._polling_task.task = None
self._polling_task.running = False

self._find_overrides_progress = 0.0
self._find_overrides_running = False
self._release_overrides_progress = 0.0
self._release_overrides_running = False

self.note("Controller initialized")
Expand Down Expand Up @@ -250,7 +249,7 @@ def simulated_points(self):
:rtype: BAC0.core.devices.Points.Point
"""
for each in self.points:
if each.properties.simulated:
if each.properties.simulated[0]:
gnmerritt marked this conversation as resolved.
Show resolved Hide resolved
yield each

def _buildPointList(self):
Expand Down Expand Up @@ -383,7 +382,7 @@ def find_overrides(self, force=False):
)
return
lst = []
self._find_overrides_progress = 0
self._find_overrides_progress = 0.0
self._find_overrides_running = True
total = len(self.points)

Expand All @@ -400,11 +399,11 @@ def _find_overrides():
)
self.properties.points_overridden = lst
self._find_overrides_running = False
self._find_overrides_progress = 1
self._find_overrides_progress = 1.0

self.do(_find_overrides)

def find_overrides_progress(self):
def find_overrides_progress(self) -> float:
return self._find_overrides_progress

def release_all_overrides(self, force=False):
Expand All @@ -416,7 +415,7 @@ def release_all_overrides(self, force=False):
)
return
self._release_overrides_running = True
self._release_overrides_progress = 0
self._release_overrides_progress = 0.0

def _release_all_overrides():
self.find_overrides()
Expand Down Expand Up @@ -539,15 +538,15 @@ def _buildPointList(self):
self.points,
self._list_of_trendlogs,
) = self._discoverPoints(self.custom_object_list)
if self.properties.pollDelay > 0:
if self.properties.pollDelay is not None and self.properties.pollDelay > 0:
self.poll(delay=self.properties.pollDelay)
self.update_history_size(size=self.properties.history_size)
# self.clear_histories()
except NoResponseFromController as error:
except NoResponseFromController:
self._log.error("Cannot retrieve object list, disconnecting...")
self.segmentation_supported = False
self.new_state(DeviceDisconnected)
except IndexError as error:
except IndexError:
if self._reconnect_on_failure:
self._log.error("Device creation failed... re-connecting")
self.new_state(DeviceDisconnected)
Expand Down
Loading