Skip to content

Commit 33365a4

Browse files
authored
Merge pull request #121 from mulkieran/monkeytype
Add a monkeytype configuration file and a type rewriter
2 parents 440ca49 + cfe1c20 commit 33365a4

File tree

4 files changed

+84
-2
lines changed

4 files changed

+84
-2
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ jobs:
2222
task: make -f Makefile yamllint
2323
- dependencies: >
2424
bandit
25+
monkeytype
2526
pylint
2627
python3-dbus
2728
python3-dbus-signature-pyparsing

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ MONKEYTYPE_MODULES = into_dbus_python._signature
99
.PHONY: lint
1010
lint:
1111
pylint setup.py
12+
pylint monkeytype_config.py
1213
pylint src/into_dbus_python
1314
pylint tests
1415
bandit setup.py
16+
bandit monkeytype_config.py
1517
# Ignore B101 errors. We do not distribute optimized code, i.e., .pyo
1618
# files in Fedora, so we do not need to have concerns that assertions
1719
# are removed by optimization.
@@ -31,7 +33,7 @@ coverage:
3133

3234
.PHONY: fmt
3335
fmt:
34-
isort setup.py src tests
36+
isort setup.py src tests monkeytype_config.py
3537
black .
3638

3739
.PHONY: fmt-travis

monkeytype_config.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
"""
2+
Monkeytype Configuration File
3+
"""
4+
5+
# isort: STDLIB
6+
from typing import Any, Union
7+
8+
# isort: THIRDPARTY
9+
from monkeytype.config import DefaultConfig
10+
from monkeytype.typing import (
11+
ChainedRewriter,
12+
RemoveEmptyContainers,
13+
RewriteConfigDict,
14+
RewriteGenerator,
15+
RewriteLargeUnion,
16+
TypeRewriter,
17+
)
18+
19+
20+
class CanonicalizeUnionElementOrder(TypeRewriter):
21+
"""
22+
Monkeytype type rewriter to sort union elements in canonical order.
23+
"""
24+
25+
@staticmethod
26+
def type_order(typ) -> str:
27+
"""
28+
Return a value to be used as a key for sorting.
29+
"""
30+
31+
print(f"BIG: {typ}", flush=True)
32+
if typ is Any:
33+
return "Any"
34+
35+
try:
36+
under_name = typ._name # pylint: disable=protected-access
37+
except AttributeError:
38+
under_name = None
39+
40+
try:
41+
dunder_name = typ.__name__
42+
except AttributeError:
43+
dunder_name = None
44+
45+
return (
46+
under_name
47+
if under_name is not None
48+
else (dunder_name if dunder_name is not None else "")
49+
)
50+
51+
def rewrite_Union(self, union):
52+
return Union[
53+
tuple(
54+
sorted(
55+
list(union.__args__),
56+
key=CanonicalizeUnionElementOrder.type_order,
57+
)
58+
)
59+
]
60+
61+
62+
class MyConfig(DefaultConfig):
63+
"""
64+
Monkeytype configuration for this project
65+
"""
66+
67+
def type_rewriter(self):
68+
return ChainedRewriter(
69+
(
70+
RemoveEmptyContainers(),
71+
RewriteConfigDict(),
72+
RewriteLargeUnion(),
73+
RewriteGenerator(),
74+
CanonicalizeUnionElementOrder(),
75+
)
76+
)
77+
78+
79+
CONFIG = MyConfig()

src/into_dbus_python/_xformer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ def __init__(self):
299299
_XFORMER = _ToDbusXformer()
300300

301301

302-
def xformers(sig: str) -> List[Union[Tuple[Callable, str], Any]]:
302+
def xformers(sig: str) -> List[Union[Any, Tuple[Callable, str]]]:
303303
"""
304304
Get the list of xformer functions for the given signature.
305305

0 commit comments

Comments
 (0)