Skip to content
This repository was archived by the owner on Jan 19, 2025. It is now read-only.

Commit 9491c2c

Browse files
authored
feat: migrate @description annotations (#1161)
Closes #1156. ### Summary of Changes - add migration for `@description` annotations to another version, with the possibilities: - If the mapped apiv2 element has the same type as the annotated apiv1 element, the annotation will be migrated as expected. - Else, the element will be marked with a `@todo` annotation. - correct ids and qnames of migration test data ### Testing Instructions run the migrate command or view and run the `test_migration.py` file
1 parent ecae01d commit 9491c2c

File tree

10 files changed

+329
-28
lines changed

10 files changed

+329
-28
lines changed

package-parser/package_parser/processing/migration/_migrate.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from package_parser.processing.migration.annotations import (
99
migrate_boundary_annotation,
1010
migrate_called_after_annotation,
11+
migrate_description_annotation,
1112
migrate_enum_annotation,
1213
migrate_group_annotation,
1314
migrate_move_annotation,
@@ -51,6 +52,14 @@ def migrate_annotations(
5152
):
5253
migrated_annotation_store.add_annotation(annotation)
5354

55+
for description_annotation in annotationsv1.descriptionAnnotations:
56+
mapping = _get_mapping_from_annotation(description_annotation, mappings)
57+
if mapping is not None:
58+
for annotation in migrate_description_annotation(
59+
description_annotation, mapping
60+
):
61+
migrated_annotation_store.add_annotation(annotation)
62+
5463
for enum_annotation in annotationsv1.enumAnnotations:
5564
mapping = _get_mapping_from_annotation(enum_annotation, mappings)
5665
if mapping is not None:

package-parser/package_parser/processing/migration/annotations/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from ._get_migration_text import get_migration_text
33
from ._migrate_boundary_annotation import migrate_boundary_annotation
44
from ._migrate_called_after_annotation import migrate_called_after_annotation
5+
from ._migrate_description_annotation import migrate_description_annotation
56
from ._migrate_enum_annotation import migrate_enum_annotation
67
from ._migrate_group_annotation import migrate_group_annotation
78
from ._migrate_move_annotation import migrate_move_annotation
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
from copy import deepcopy
2+
3+
from package_parser.processing.annotations.model import (
4+
AbstractAnnotation,
5+
DescriptionAnnotation,
6+
EnumReviewResult,
7+
TodoAnnotation,
8+
)
9+
from package_parser.processing.api.model import Attribute, Result
10+
from package_parser.processing.migration.model import (
11+
ManyToOneMapping,
12+
Mapping,
13+
OneToOneMapping,
14+
)
15+
16+
from ._constants import migration_author
17+
from ._get_annotated_api_element import get_annotated_api_element
18+
from ._get_migration_text import get_migration_text
19+
20+
21+
def migrate_description_annotation(
22+
description_annotation: DescriptionAnnotation, mapping: Mapping
23+
) -> list[AbstractAnnotation]:
24+
description_annotation = deepcopy(description_annotation)
25+
authors = description_annotation.authors
26+
authors.append(migration_author)
27+
description_annotation.authors = authors
28+
migrate_text = get_migration_text(description_annotation, mapping)
29+
30+
if isinstance(mapping, (ManyToOneMapping, OneToOneMapping)):
31+
element = mapping.get_apiv2_elements()[0]
32+
if isinstance(element, (Attribute, Result)):
33+
return []
34+
description_annotation.target = element.id
35+
return [description_annotation]
36+
37+
annotated_apiv1_element = get_annotated_api_element(
38+
description_annotation, mapping.get_apiv1_elements()
39+
)
40+
if annotated_apiv1_element is None:
41+
return []
42+
43+
description_annotations: list[AbstractAnnotation] = []
44+
for element in mapping.get_apiv2_elements():
45+
if isinstance(element, type(annotated_apiv1_element)) and not isinstance(
46+
element, (Attribute, Result)
47+
):
48+
description_annotations.append(
49+
DescriptionAnnotation(
50+
element.id,
51+
authors,
52+
description_annotation.reviewers,
53+
description_annotation.comment,
54+
EnumReviewResult.NONE,
55+
newDescription=description_annotation.newDescription,
56+
)
57+
)
58+
elif not isinstance(element, (Attribute, Result)):
59+
description_annotations.append(
60+
TodoAnnotation(
61+
element.id,
62+
authors,
63+
description_annotation.reviewers,
64+
description_annotation.comment,
65+
EnumReviewResult.NONE,
66+
migrate_text,
67+
)
68+
)
69+
return description_annotations

package-parser/tests/processing/migration/annotations/test_boundary_migration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ def migrate_boundary_annotation_data_one_to_many_mapping() -> Tuple[
215215
parameterv1 = Parameter(
216216
id_="test/test.boundary.test4.testv1",
217217
name="testA",
218-
qname="test.boundary.test4.testA",
218+
qname="test.boundary.test4.testv1",
219219
default_value="1",
220220
assigned_by=ParameterAssignment.POSITION_OR_NAME,
221221
is_public=True,
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
from typing import Tuple
2+
3+
from package_parser.processing.annotations.model import (
4+
AbstractAnnotation,
5+
DescriptionAnnotation,
6+
EnumReviewResult,
7+
TodoAnnotation,
8+
)
9+
from package_parser.processing.api.model import (
10+
Class,
11+
ClassDocumentation,
12+
Function,
13+
FunctionDocumentation,
14+
Parameter,
15+
ParameterAssignment,
16+
ParameterDocumentation,
17+
)
18+
from package_parser.processing.migration import (
19+
Mapping,
20+
OneToManyMapping,
21+
OneToOneMapping,
22+
)
23+
from package_parser.processing.migration.annotations import (
24+
get_migration_text,
25+
migration_author,
26+
)
27+
28+
29+
# pylint: disable=duplicate-code
30+
def migrate_description_annotation_data_one_to_one_mapping__function() -> Tuple[
31+
Mapping,
32+
AbstractAnnotation,
33+
list[AbstractAnnotation],
34+
]:
35+
functionv1 = Function(
36+
id="test/test.description.test1.test/test",
37+
qname="test.description.test1.test.test",
38+
decorators=[],
39+
parameters=[],
40+
results=[],
41+
is_public=True,
42+
reexported_by=[],
43+
documentation=FunctionDocumentation("", ""),
44+
code="",
45+
)
46+
47+
functionv2 = Function(
48+
id="test/test.description.test1.test/new_test",
49+
qname="test.description.test1.test.new_test",
50+
decorators=[],
51+
parameters=[],
52+
results=[],
53+
is_public=True,
54+
reexported_by=[],
55+
documentation=FunctionDocumentation("", ""),
56+
code="",
57+
)
58+
59+
mapping = OneToOneMapping(1.0, functionv1, functionv2)
60+
61+
annotationv1 = DescriptionAnnotation(
62+
target="test/test.description.test1.test/test",
63+
authors=["testauthor"],
64+
reviewers=[],
65+
comment="",
66+
reviewResult=EnumReviewResult.NONE,
67+
newDescription="lightbringer",
68+
)
69+
annotationv2 = DescriptionAnnotation(
70+
target="test/test.description.test1.test/new_test",
71+
authors=["testauthor", migration_author],
72+
reviewers=[],
73+
comment="",
74+
reviewResult=EnumReviewResult.NONE,
75+
newDescription="lightbringer",
76+
)
77+
return mapping, annotationv1, [annotationv2]
78+
79+
80+
# pylint: disable=duplicate-code
81+
def migrate_description_annotation_data_one_to_many_mapping__class() -> Tuple[
82+
Mapping,
83+
AbstractAnnotation,
84+
list[AbstractAnnotation],
85+
]:
86+
87+
classv1 = Class(
88+
id_="test/test.description.test2.test/DescriptionTestClass",
89+
qname="test.description.test2.test.DescriptionTestClass",
90+
decorators=[],
91+
superclasses=[],
92+
is_public=True,
93+
reexported_by=[],
94+
documentation=ClassDocumentation("", ""),
95+
code="class DescriptionTestClass:\n pass",
96+
instance_attributes=[],
97+
)
98+
classv2_a = Class(
99+
id_="test/test.description.test2.test/NewDescriptionTestClass",
100+
qname="test.description.test2.test.NewDescriptionTestClass",
101+
decorators=[],
102+
superclasses=[],
103+
is_public=True,
104+
reexported_by=[],
105+
documentation=ClassDocumentation("", ""),
106+
code="class NewDescriptionTestClass:\n pass",
107+
instance_attributes=[],
108+
)
109+
classv2_b = Class(
110+
id_="test/test.description.test2.test/NewDescriptionTestClass2",
111+
qname="test.description.test2.test.NewDescriptionTestClass2",
112+
decorators=[],
113+
superclasses=[],
114+
is_public=True,
115+
reexported_by=[],
116+
documentation=ClassDocumentation("", ""),
117+
code="class NewDescriptionTestClass2:\n pass",
118+
instance_attributes=[],
119+
)
120+
functionv2 = Function(
121+
id="test/test.description.test2.test/TestClass/new_test",
122+
qname="test.description.test2.test.TestClass.new_test",
123+
decorators=[],
124+
parameters=[],
125+
results=[],
126+
is_public=True,
127+
reexported_by=[],
128+
documentation=FunctionDocumentation("", ""),
129+
code="",
130+
)
131+
132+
mapping = OneToManyMapping(1.0, classv1, [classv2_a, classv2_b, functionv2])
133+
134+
annotationv1 = DescriptionAnnotation(
135+
target="test/test.description.test2.test/DescriptionTestClass",
136+
authors=["testauthor"],
137+
reviewers=[],
138+
comment="",
139+
reviewResult=EnumReviewResult.NONE,
140+
newDescription="42",
141+
)
142+
annotationv2_a = DescriptionAnnotation(
143+
target="test/test.description.test2.test/NewDescriptionTestClass",
144+
authors=["testauthor", migration_author],
145+
reviewers=[],
146+
comment="",
147+
reviewResult=EnumReviewResult.NONE,
148+
newDescription="42",
149+
)
150+
annotationv2_b = DescriptionAnnotation(
151+
target="test/test.description.test2.test/NewDescriptionTestClass2",
152+
authors=["testauthor", migration_author],
153+
reviewers=[],
154+
comment="",
155+
reviewResult=EnumReviewResult.NONE,
156+
newDescription="42",
157+
)
158+
annotationv2_c = TodoAnnotation(
159+
target="test/test.description.test2.test/TestClass/new_test",
160+
authors=["testauthor", migration_author],
161+
reviewers=[],
162+
comment="",
163+
reviewResult=EnumReviewResult.NONE,
164+
newTodo=get_migration_text(annotationv1, mapping),
165+
)
166+
return mapping, annotationv1, [annotationv2_a, annotationv2_b, annotationv2_c]
167+
168+
169+
def migrate_description_annotation_data_one_to_one_mapping__parameter() -> Tuple[
170+
Mapping,
171+
AbstractAnnotation,
172+
list[AbstractAnnotation],
173+
]:
174+
parameterv1 = Parameter(
175+
id_="test/test.description.test3/test.test",
176+
qname="test.description.test3.test.test",
177+
name="test",
178+
default_value="value",
179+
assigned_by=ParameterAssignment.POSITION_OR_NAME,
180+
is_public=True,
181+
documentation=ParameterDocumentation("str", "value", "docstring"),
182+
)
183+
184+
parameterv2 = Parameter(
185+
id_="test/test.description.test3/test.new_test",
186+
qname="test.description.test3.test.new_test",
187+
name="new_test",
188+
default_value="value",
189+
assigned_by=ParameterAssignment.POSITION_OR_NAME,
190+
is_public=True,
191+
documentation=ParameterDocumentation("str", "value", "docstring"),
192+
)
193+
194+
mapping = OneToOneMapping(1.0, parameterv1, parameterv2)
195+
196+
annotationv1 = DescriptionAnnotation(
197+
target="test/test.description.test3/test.test",
198+
authors=["testauthor"],
199+
reviewers=[],
200+
comment="",
201+
reviewResult=EnumReviewResult.NONE,
202+
newDescription="test description",
203+
)
204+
annotationv2 = DescriptionAnnotation(
205+
target="test/test.description.test3/test.new_test",
206+
authors=["testauthor", migration_author],
207+
reviewers=[],
208+
comment="",
209+
reviewResult=EnumReviewResult.NONE,
210+
newDescription="test description",
211+
)
212+
return mapping, annotationv1, [annotationv2]

package-parser/tests/processing/migration/annotations/test_group_annotation.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
)
2828

2929

30+
# pylint: disable=duplicate-code
3031
def migrate_group_annotation_data_one_to_many_mapping() -> Tuple[
3132
list[Mapping],
3233
AbstractAnnotation,
@@ -202,7 +203,7 @@ def migrate_group_annotation_data_one_to_many_mapping() -> Tuple[
202203
)
203204
classv2_6 = Class(
204205
id_="test/test.group.test6.test/NewClass",
205-
qname="test.remove.test6.test.NewClass",
206+
qname="test.group.test6.test.NewClass",
206207
decorators=[],
207208
superclasses=[],
208209
is_public=True,
@@ -458,7 +459,7 @@ def migrate_group_annotation_data_one_to_one_mapping__one_mapping_for_parameters
458459
parameterv2_b = Parameter(
459460
id_="test/test.group.test7.test/NewTestClass/test/new_parameter_b",
460461
name="new_parameter_b",
461-
qname="test.group.test6.test.NewTestClass.test.new_parameter_b",
462+
qname="test.group.test7.test.NewTestClass.test.new_parameter_b",
462463
default_value="'test'",
463464
assigned_by=ParameterAssignment.POSITION_OR_NAME,
464465
is_public=True,

package-parser/tests/processing/migration/annotations/test_move_migration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def migrate_move_annotation_data_one_to_one_mapping__class() -> Tuple[
8282
]:
8383
classv1 = Class(
8484
id_="test/test.move.test2.test/MoveTestClass",
85-
qname="test.move.test2.test.TestClass",
85+
qname="test.move.test2.test.MoveTestClass",
8686
decorators=[],
8787
superclasses=[],
8888
is_public=True,

0 commit comments

Comments
 (0)