Skip to content

Commit 165e09f

Browse files
catalinaperaltacperaltahmsyyc
authored
[bct] add base class properties in code report (Azure#37656)
* add base class properties in code report * add test and assets * license headers --------- Co-authored-by: Catalina Peralta <[email protected]> Co-authored-by: msyyc <[email protected]>
1 parent e6dd52c commit 165e09f

File tree

5 files changed

+58
-1
lines changed

5 files changed

+58
-1
lines changed

scripts/breaking_changes_checker/detect_breaking_changes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def get_properties(cls: Type) -> Dict:
189189
analyzer = ClassTreeAnalyzer(cls.__name__)
190190
analyzer.visit(module)
191191
cls_node = analyzer.cls_node
192-
extract_base_classes = check_base_classes(cls_node)
192+
extract_base_classes = True if hasattr(cls_node, "bases") else False
193193

194194
if extract_base_classes:
195195
base_classes = inspect.getmro(cls) # includes cls itself

scripts/breaking_changes_checker/tests/examples/__init__.py

Whitespace-only changes.

scripts/breaking_changes_checker/tests/examples/base_class_properties/__init__.py

Whitespace-only changes.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env python
2+
3+
# --------------------------------------------------------------------------------------------
4+
# Copyright (c) Microsoft Corporation. All rights reserved.
5+
# Licensed under the MIT License. See License.txt in the project root for license information.
6+
# --------------------------------------------------------------------------------------------
7+
8+
class Foo:
9+
a: int
10+
11+
class Bar(Foo):
12+
b: int
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python
2+
3+
# --------------------------------------------------------------------------------------------
4+
# Copyright (c) Microsoft Corporation. All rights reserved.
5+
# Licensed under the MIT License. See License.txt in the project root for license information.
6+
# --------------------------------------------------------------------------------------------
7+
8+
import os
9+
import sys
10+
import importlib
11+
import inspect
12+
from typing import Dict
13+
from breaking_changes_checker.detect_breaking_changes import create_function_report, create_class_report
14+
15+
# The build_library_report function has been copied from the original implementation in detect_breaking_changes.
16+
# The function has been modified to remove the test_find_modules function and the modules variable. Instead we
17+
# are directly importing the target_module and using it to build the library report.
18+
# Everything else remains the same.
19+
def build_library_report(target_module: str) -> Dict:
20+
module = importlib.import_module(target_module)
21+
# removed for testing purposes
22+
# modules = test_find_modules(module.__path__[0])
23+
module_name = module.__name__
24+
public_api = {}
25+
public_api[module_name] = {"class_nodes": {}, "function_nodes": {}}
26+
module = importlib.import_module(module_name)
27+
importables = [importable for importable in dir(module)]
28+
for importable in importables:
29+
if not importable.startswith("_"):
30+
live_obj = getattr(module, importable)
31+
if inspect.isfunction(live_obj):
32+
public_api[module_name]["function_nodes"].update({importable: create_function_report(live_obj)})
33+
elif inspect.isclass(live_obj):
34+
public_api[module_name]["class_nodes"].update({importable: create_class_report(live_obj)})
35+
# else: # Constants, version, etc. Nothing of interest at the moment
36+
# public_api[module_name]["others"].update({importable: live_obj})
37+
return public_api
38+
39+
def test_base_class_properties():
40+
sys.path.append(os.path.join(os.path.dirname(__file__), "examples", "base_class_properties"))
41+
42+
report = build_library_report("base_class")
43+
# Check the report for properties of the base class in Bar
44+
assert report["base_class"]["class_nodes"]["Foo"]["properties"] == {"a": None}
45+
assert report["base_class"]["class_nodes"]["Bar"]["properties"] == {"a": None, "b": None}

0 commit comments

Comments
 (0)