Skip to content

Commit 7b48860

Browse files
committed
cc: Add typing to overloaded function implementations
PEP 484 doesn't require an overloaded function implementation to be annotated, but mypy will not check it otherwise. python/mypy#3160 Note that in dispatchlib I replaced overload by type restriction, since that was a simpler way of expressing the same rules.
1 parent 1abcc9d commit 7b48860

File tree

2 files changed

+13
-17
lines changed

2 files changed

+13
-17
lines changed

src/softfab/dispatchlib.py

+7-14
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
from collections import defaultdict
44
from typing import (
5-
TYPE_CHECKING, Dict, Iterable, List, Mapping, Optional, Sequence, Set,
6-
cast, overload
5+
TYPE_CHECKING, DefaultDict, Dict, Iterable, List, Mapping, Optional,
6+
Sequence, Set, TypeVar, cast
77
)
88

99
from softfab.resreq import ResourceClaim, ResourceSpec
@@ -17,18 +17,11 @@
1717
else:
1818
Resource = None
1919

20-
@overload
21-
def _groupByType(
22-
items: Iterable[Resource] # pylint: disable=unused-argument
23-
) -> Mapping[str, Sequence[Resource]]:
24-
pass
25-
@overload
26-
def _groupByType(
27-
items: Iterable[ResourceSpec] # pylint: disable=unused-argument
28-
) -> Mapping[str, Sequence[ResourceSpec]]:
29-
pass
30-
def _groupByType(items):
31-
grouped = defaultdict(list)
20+
ResOrSpec = TypeVar('ResOrSpec', Resource, ResourceSpec)
21+
22+
def _groupByType(items: Iterable[ResOrSpec]
23+
) -> Mapping[str, Sequence[ResOrSpec]]:
24+
grouped = defaultdict(list) # type: DefaultDict[str, List[ResOrSpec]]
3225
for item in items:
3326
grouped[item.typeName].append(item)
3427
return grouped

src/softfab/pageargs.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ def __init__(self, default: _MandatoryValue = mandatory
468468
): # pylint: disable=unused-argument
469469
...
470470

471-
def __init__(self, default=mandatory):
471+
def __init__(self, default: Any = mandatory):
472472
'''Creates a page argument with a given default value.
473473
'''
474474
self.__default = default
@@ -503,7 +503,10 @@ def __get__(self,
503503
) -> Union[ValueT, DefaultT]:
504504
pass
505505

506-
def __get__(self, instance, owner):
506+
def __get__(self,
507+
instance: Optional[PageArgs],
508+
owner: Type[PageArgs]
509+
) -> Any:
507510
if instance is None:
508511
# Class attribute access.
509512
return self
@@ -1234,7 +1237,7 @@ def _externalizeArg(arg: Any, value: Any # pylint: disable=unused-argument
12341237
# type checks we do here just to please mypy.
12351238
pass
12361239

1237-
def _externalizeArg(arg, value):
1240+
def _externalizeArg(arg: Any, value: Any) -> Sequence[str]:
12381241
if isinstance(arg, SingularArgument):
12391242
return (arg.externalize(value),)
12401243
elif isinstance(arg, CollectionArg):

0 commit comments

Comments
 (0)