Skip to content

Commit cb0c527

Browse files
committed
Add type hints to doctest runner and filter
1 parent 5e1beec commit cb0c527

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

shapefile.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
__version__ = "2.4.0"
1010

1111
import array
12+
import doctest
1213
import io
1314
import logging
1415
import os
@@ -18,13 +19,16 @@
1819
import zipfile
1920
from datetime import date
2021
from struct import Struct, calcsize, error, pack, unpack
22+
from typing import Iterable, Iterator
2123
from urllib.error import HTTPError
2224
from urllib.parse import urlparse, urlunparse
2325
from urllib.request import Request, urlopen
2426

2527
# Create named logger
2628
logger = logging.getLogger(__name__)
2729

30+
doctest.NORMALIZE_WHITESPACE = 1
31+
2832
# Module settings
2933
VERBOSE = True
3034

@@ -2714,11 +2718,7 @@ def field(self, name, fieldType="C", size="50", decimal=0):
27142718

27152719

27162720
# Begin Testing
2717-
def _get_doctests():
2718-
import doctest
2719-
2720-
doctest.NORMALIZE_WHITESPACE = 1
2721-
2721+
def _get_doctests() -> doctest.DocTest:
27222722
# run tests
27232723
with open("README.md", "rb") as fobj:
27242724
tests = doctest.DocTestParser().get_doctest(
@@ -2732,7 +2732,11 @@ def _get_doctests():
27322732
return tests
27332733

27342734

2735-
def _filter_network_doctests(examples, include_network=False, include_non_network=True):
2735+
def _filter_network_doctests(
2736+
examples: Iterable[doctest.Example],
2737+
include_network: bool = False,
2738+
include_non_network: bool = True,
2739+
) -> Iterator[doctest.Example]:
27362740
globals_from_network_doctests = set()
27372741

27382742
if not (include_network or include_non_network):
@@ -2773,16 +2777,16 @@ def _filter_network_doctests(examples, include_network=False, include_non_networ
27732777

27742778

27752779
def _replace_remote_url(
2776-
old_url,
2780+
old_url: str,
27772781
# Default port of Python http.server and Python 2's SimpleHttpServer
2778-
port=8000,
2779-
scheme="http",
2780-
netloc="localhost",
2781-
path=None,
2782-
params="",
2783-
query="",
2784-
fragment="",
2785-
):
2782+
port: int = 8000,
2783+
scheme: str = "http",
2784+
netloc: str = "localhost",
2785+
path: str | None = None,
2786+
params: str = "",
2787+
query: str = "",
2788+
fragment: str = "",
2789+
) -> str:
27862790
old_parsed = urlparse(old_url)
27872791

27882792
# Strip subpaths, so an artefacts
@@ -2806,15 +2810,12 @@ def _replace_remote_url(
28062810
return new_url
28072811

28082812

2809-
def _test(args=sys.argv[1:], verbosity=0):
2813+
def _test(args: list[str] = sys.argv[1:], verbosity: bool = False) -> int:
28102814
if verbosity == 0:
28112815
print("Getting doctests...")
28122816

2813-
import doctest
28142817
import re
28152818

2816-
doctest.NORMALIZE_WHITESPACE = 1
2817-
28182819
tests = _get_doctests()
28192820

28202821
if len(args) >= 2 and args[0] == "-m":

0 commit comments

Comments
 (0)