Skip to content

Commit 8d5977c

Browse files
committed
Add Python type annotations to 27 source modules via MonkeyType
- Annotate utils/, app/, cli/api/, swagger/api/ with return types and params - Annotate command/model/configuration: _arg, _arg_builder, _client, _condition, _fields, _instance_update, _output, _schema, _selector_index, _xml - Annotate command/controller: command_tree, shorthand, workspace_manager - Annotate swagger/model/schema: parameter, reference - Fix circular import in _arg.py / _arg_builder.py using TYPE_CHECKING guard - Widen noisy annotations (-> Any) to avoid cascading mypy strict violations - Re-sync mypy-baseline.txt: 121 violations fixed (5,625 -> 5,491 baseline lines) - Add monkeytype_config.py and monkeytype.sqlite3 to .gitignore
1 parent bd226ab commit 8d5977c

28 files changed

Lines changed: 749 additions & 806 deletions

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,6 @@ src/aaz_dev/ui
138138

139139
.env
140140
*/.env
141+
142+
# MonkeyType trace database
143+
monkeytype.sqlite3

monkeytype_config.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""
2+
MonkeyType configuration for aaz-dev-tools.
3+
4+
Usage — trace, then apply:
5+
PYTHONPATH=src/aaz_dev:src python -m monkeytype run -m pytest \
6+
src/aaz_dev/command/tests/configuration_tests/ \
7+
src/aaz_dev/command/tests/editor_tests/test_serialize_shorthand.py \
8+
--ignore=src/aaz_dev/command/tests/configuration_tests/test_xml.py \
9+
-q
10+
11+
python -m monkeytype list-modules
12+
python -m monkeytype apply <module>
13+
14+
Notes:
15+
- The code_filter restricts tracing to src/aaz_dev/ only, keeping Flask/schematics
16+
stdlib internals out of the trace database.
17+
- Module names in the DB use the unqualified form (e.g. "utils.case") because the
18+
codebase adds src/aaz_dev/ to sys.path. Apply with those unqualified names.
19+
"""
20+
21+
import os
22+
from monkeytype.config import DefaultConfig
23+
from monkeytype.typing import NoOpRewriter
24+
25+
26+
_REPO_ROOT = os.path.dirname(os.path.abspath(__file__))
27+
_SOURCE_ROOT = os.path.join(_REPO_ROOT, "src", "aaz_dev")
28+
29+
30+
class AazDevConfig(DefaultConfig):
31+
def code_filter(self): # type: ignore[override]
32+
"""Only trace code that lives inside src/aaz_dev/."""
33+
source_root = _SOURCE_ROOT
34+
35+
def _filter(code): # type: ignore[no-untyped-def]
36+
filename = code.co_filename or ""
37+
return filename.startswith(source_root)
38+
39+
return _filter
40+
41+
42+
CONFIG = AazDevConfig()

mypy-baseline.txt

Lines changed: 586 additions & 720 deletions
Large diffs are not rendered by default.

requirements-dev.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
# Dev dependencies — not required at runtime
22
mypy>=1.10.0
33
mypy-baseline>=0.6.0
4+
monkeytype>=23.3.0
5+
libcst>=1.0.0

src/aaz_dev/app/app.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
from utils import exceptions
88
from utils.config import Config
99
from aaz_dev.app.run import run_command
10+
import flask.app
1011

1112

12-
def create_app():
13+
def create_app() -> flask.app.Flask:
1314
app = Flask(__name__, static_folder=Config.STATIC_FOLDER, static_url_path=Config.STATIC_URL_PATH)
1415
logger = create_logger(app)
1516

src/aaz_dev/cli/api/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11

2-
def register_blueprints(app):
2+
from flask.app import Flask
3+
4+
def register_blueprints(app: Flask) -> None:
35
from . import az, portal, _cmds
46
app.register_blueprint(_cmds.bp)
57
app.register_blueprint(az.bp)

src/aaz_dev/command/controller/command_tree.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from command.model.specs._command_tree import CMDSpecsSimpleCommand, CMDSpecsSimpleCommandGroup, \
99
CMDSpecsSimpleCommandTree
1010
from utils import exceptions
11+
from typing import List, Any
1112

1213
logger = logging.getLogger(__name__)
1314

@@ -59,7 +60,7 @@ def build_simple_command_tree(aaz_path):
5960

6061

6162
class CMDSpecsPartialCommandGroup:
62-
def __init__(self, names, short_help, uri, aaz_path):
63+
def __init__(self, names: List[str], short_help: str, uri: str, aaz_path: str) -> None:
6364
self.names = names
6465
self.short_help = short_help
6566
self.uri = uri
@@ -316,12 +317,12 @@ def parse_command_info(cls, info, cmd_names):
316317

317318

318319
class CMDSpecsPartialCommandTree:
319-
def __init__(self, aaz_path, root=None):
320+
def __init__(self, aaz_path: str, root: None=None):
320321
self.aaz_path = aaz_path
321322
self._root = root or CMDSpecsPartialCommandGroup(names=["aaz"], short_help='', uri="/Commands/readme.md",
322323
aaz_path=aaz_path).load()
323-
self._modified_command_groups = set()
324-
self._modified_commands = set()
324+
self._modified_command_groups: set[Any] = set()
325+
self._modified_commands: set[Any] = set()
325326

326327
@property
327328
def root(self):

src/aaz_dev/command/controller/shorthand.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
def serialize(obj):
1+
from typing import Any
2+
3+
def serialize(obj: Any) -> str:
24
def dfs(obj):
35
if isinstance(obj, dict):
46
return "{" + ",".join(f'{k}:{dfs(v)}' for k, v in obj.items()) + "}"

src/aaz_dev/command/controller/workspace_manager.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from .specs_manager import AAZSpecsManager
2020
from .workspace_cfg_editor import WorkspaceCfgEditor, build_endpoint_selector_for_client_config
2121
from .workspace_client_cfg_editor import WorkspaceClientCfgEditor
22+
from typing import Any, Optional
2223

2324
logger = logging.getLogger('aaz')
2425

src/aaz_dev/command/model/configuration/_arg.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
from utils import exceptions
1010

1111
import copy
12+
from typing import TYPE_CHECKING, Any, Type, Union
13+
if TYPE_CHECKING:
14+
from command.model.configuration._arg_builder import CMDArgBuilder
1215

1316

1417
class CMDArgEnumItem(Model):
@@ -129,12 +132,12 @@ class Options:
129132
def type(self):
130133
return self._get_type()
131134

132-
def _get_type(self):
135+
def _get_type(self) -> str:
133136
assert self.TYPE_VALUE is not None
134137
return self.TYPE_VALUE
135138

136139
@classmethod
137-
def _claim_polymorphic(cls, data):
140+
def _claim_polymorphic(cls, data: Any) -> bool:
138141
if cls.TYPE_VALUE is None:
139142
return False
140143

@@ -148,7 +151,7 @@ def _claim_polymorphic(cls, data):
148151
return False
149152

150153
@classmethod
151-
def build_arg_base(cls, builder):
154+
def build_arg_base(cls, builder: "CMDArgBuilder") -> Any:
152155
arg_base = cls()
153156
arg_base.nullable = builder.get_nullable()
154157
arg_base.blank = builder.get_blank()
@@ -171,7 +174,7 @@ def __init__(self, **kwargs):
171174
**kwargs
172175
)
173176

174-
def find_model(self, data):
177+
def find_model(self, data: Any) -> Any:
175178
if self.claim_function:
176179
kls = self.claim_function(self, data)
177180
if not kls:
@@ -229,7 +232,7 @@ def __init__(self, *args, **kwargs):
229232
self.ref_schema = None
230233

231234
@classmethod
232-
def _claim_polymorphic(cls, data):
235+
def _claim_polymorphic(cls, data: Any) -> bool:
233236
if super()._claim_polymorphic(data):
234237
if isinstance(data, dict):
235238
# distinguish with CMDArgBase and CMDArg
@@ -239,7 +242,7 @@ def _claim_polymorphic(cls, data):
239242
return False
240243

241244
@classmethod
242-
def build_arg(cls, builder):
245+
def build_arg(cls, builder: "CMDArgBuilder") -> Any:
243246
arg = cls.build_arg_base(builder)
244247
assert isinstance(arg, CMDArg)
245248
arg.var = builder.get_var()
@@ -278,7 +281,7 @@ def __init__(self, *args, **kwargs):
278281
self.implement = None
279282

280283
@classmethod
281-
def _claim_polymorphic(cls, data):
284+
def _claim_polymorphic(cls, data: Any) -> bool:
282285
if isinstance(data, dict):
283286
type_value = data.get('type', None)
284287
if type_value is not None and type_value.startswith("@"):
@@ -356,7 +359,7 @@ class CMDStringArgBase(CMDArgBase):
356359
enum = ModelType(CMDArgEnum)
357360

358361
@classmethod
359-
def build_arg_base(cls, builder):
362+
def build_arg_base(cls, builder: "CMDArgBuilder") -> Any:
360363
arg = super().build_arg_base(builder)
361364
assert isinstance(arg, CMDStringArgBase)
362365
arg.fmt = builder.get_fmt()
@@ -838,7 +841,7 @@ class CMDArrayArgBase(CMDArgBase):
838841
# default
839842
cls = CMDClassField()
840843

841-
def _get_type(self):
844+
def _get_type(self) -> str:
842845
return f"{self.TYPE_VALUE}<{self.item.type}>"
843846

844847
@classmethod

0 commit comments

Comments
 (0)