8000 Introduce some static type analysis to the codebase · python-zeroconf/python-zeroconf@1f33c4f · GitHub
[go: up one dir, main page]

Skip to content

Commit 1f33c4f

Browse files
committed
Introduce some static type analysis to the codebase
The main purpose of this change is to make the code easier to read and explore. Preventing some classes of bugs is a bonus. On top of just adding type hints and enabling mypy to verify them the following changes were needed: * casts in places where we know what we're doing but mypy can't know it * RecordUpdateListener interfaces extracted out of ServiceBrowser and ServiceInfo classes so that we have a common name we can use in places where we only need an instance of one of those classes to call to call update_record() on it. This way we can keep mypy happy * assert isinstance(...) blocks to provide hints for mypy as to what concrete types we're dealing with * some local type mixing removed (previously we'd first assign a value of one type to a variable and then overwrite with another type) * explicit "return None" in case of function that returns optionally - mypy enforces explicit return in this case
1 parent 23fdcce commit 1f33c4f

File tree

8 files changed

+112
-70
lines changed

8 files changed

+112
-70
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ Thumbs.db
99
.idea
1010
.vslick
1111
.cache
12-
12+
.mypy_cache/

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ matrix:
88
fast_finish: true
99
install:
1010
- pip install -r requirements-dev.txt
11+
# mypy can't be installed on pypy
12+
- if [[ "${TRAVIS_PYTHON_VERSION}" != "pypy3.5-5.8.0" ]] ; then pip install mypy ; fi
1113
script:
1214
- make test_coverage
1315
- flake8 --version
1416
- make flake8
17+
- if [[ "${TRAVIS_PYTHON_VERSION}" != "pypy3.5-5.8.0" ]] ; then make mypy ; fi
1518
after_success:
1619
- coveralls

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ env:
1313
flake8:
1414
flake8 --max-line-length=$(MAX_LINE_LENGTH) examples *.py
1515

16+
mypy:
17+
mypy examples/*.py zeroconf.py
18+
1619
test:
1720
nosetests -v
1821

examples/browser.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@
66
import socket
77
import sys
88
from time import sleep
9+
from typing import cast
910

1011
from zeroconf import ServiceBrowser, ServiceStateChange, Zeroconf
1112

1213

13-
def on_service_state_change(zeroconf, service_type, name, state_change):
14+
def on_service_state_change(
15+
zeroconf: Zeroconf, service_type: str, name: str, state_change: ServiceStateChange,
16+
) -> None:
1417
print("Service %s of type %s state changed: %s" % (name, service_type, state_change))
1518

1619
if state_change is ServiceStateChange.Added:
1720
info = zeroconf.get_service_info(service_type, name)
1821
if info:
19-
print(" Address: %s:%d" % (socket.inet_ntoa(info.address), info.port))
22+
print(" Address: %s:%d" % (socket.inet_ntoa(cast(bytes, info.address)), cast(int, info.port)))
2023
print(" Weight: %d, priority: %d" % (info.weight, info.priority))
2124
print(" Server: %s" % (info.server,))
2225
if info.properties:

examples/self_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
r.get_service_info("_http._tcp.local.", "ZOE._http._tcp.local.")))
3030
print(" Query done.")
3131
print("3. Testing query of own service...")
32-
info = r.get_service_info("_http._tcp.local.", "My Service Name._http._tcp.local.")
33-
assert info
34-
print(" Getting self: %s" % (info,))
32+
queried_info = r.get_service_info("_http._tcp.local.", "My Service Name._http._tcp.local.")
33+
assert queried_info
34+
print(" Getting self: %s" % (queried_info,))
3535
print(" Query done.")
3636
print("4. Testing unregister of service information...")
3737
r.unregister_service(info)

mypy.ini

Lines changed: 7 additions & 0 deletions
< 9BC2 td data-grid-cell-id="diff-6f2d4ba9ca9a357d31014946667b7bed1bfdbc6d2530afc77778fa0a36bee457-empty-1-2" data-line-anchor="diff-6f2d4ba9ca9a357d31014946667b7bed1bfdbc6d2530afc77778fa0a36bee457R1" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-additionLine-bgColor, var(--diffBlob-addition-bgColor-line));padding-right:24px" tabindex="-1" valign="top" class="focusable-grid-cell diff-text-cell right-side-diff-cell left-side">+
[mypy]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1
2+
ignore_missing_imports = true
3+
follow_imports = error
4+
warn_no_return = true
5+
warn_redundant_casts = true
6+
# TODO: disallow untyped defs once we have full type hint coverage
7+
disallow_untyped_defs = false

requirements-dev.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ coverage
44
flake8
55
flake8-blind-except
66
flake8-import-order
7-
mypy
87
# See setup.py comment for why this version is restricted
98
netifaces!=0.10.5
109
nose

0 commit comments

Comments
 (0)
0