| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,122 @@ | |||
| 1 | + """ | ||
| 2 | + Ensure objects defined in gitlab.v4.objects have REST* as last item in class | ||
| 3 | + definition | ||
| 4 | + | ||
| 5 | + Original notes by John L. Villalovos | ||
| 6 | + | ||
| 7 | + An example of an incorrect definition: | ||
| 8 | + class ProjectPipeline(RESTObject, RefreshMixin, ObjectDeleteMixin): | ||
| 9 | + ^^^^^^^^^^ This should be at the end. | ||
| 10 | + | ||
| 11 | + Correct way would be: | ||
| 12 | + class ProjectPipeline(RefreshMixin, ObjectDeleteMixin, RESTObject): | ||
| 13 | + Correctly at the end ^^^^^^^^^^ | ||
| 14 | + | ||
| 15 | + | ||
| 16 | + Why this is an issue: | ||
| 17 | + | ||
| 18 | + When we do type-checking for gitlab/mixins.py we make RESTObject or | ||
| 19 | + RESTManager the base class for the mixins | ||
| 20 | + | ||
| 21 | + Here is how our classes look when type-checking: | ||
| 22 | + | ||
| 23 | + class RESTObject(object): | ||
| 24 | + def __init__(self, manager: "RESTManager", attrs: Dict[str, Any]) -> None: | ||
| 25 | + ... | ||
| 26 | + | ||
| 27 | + class Mixin(RESTObject): | ||
| 28 | + ... | ||
| 29 | + | ||
| 30 | + # Wrong ordering here | ||
| 31 | + class Wrongv4Object(RESTObject, RefreshMixin): | ||
| 32 | + ... | ||
| 33 | + | ||
| 34 | + If we actually ran this in Python we would get the following error: | ||
| 35 | + class Wrongv4Object(RESTObject, Mixin): | ||
| 36 | + TypeError: Cannot create a consistent method resolution | ||
| 37 | + order (MRO) for bases RESTObject, Mixin | ||
| 38 | + | ||
| 39 | + When we are type-checking it fails to understand the class Wrongv4Object | ||
| 40 | + and thus we can't type check it correctly. | ||
| 41 | + | ||
| 42 | + Almost all classes in gitlab/v4/objects/*py were already correct before this | ||
| 43 | + check was added. | ||
| 44 | + """ | ||
| 45 | + import inspect | ||
| 46 | + | ||
| 47 | + import pytest | ||
| 48 | + | ||
| 49 | + import gitlab.v4.objects | ||
| 50 | + | ||
| 51 | + | ||
| 52 | + def test_show_issue(): | ||
| 53 | + """Test case to demonstrate the TypeError that occurs""" | ||
| 54 | + | ||
| 55 | + class RESTObject(object): | ||
| 56 | + def __init__(self, manager: str, attrs: int) -> None: | ||
| 57 | + ... | ||
| 58 | + | ||
| 59 | + class Mixin(RESTObject): | ||
| 60 | + ... | ||
| 61 | + | ||
| 62 | + with pytest.raises(TypeError) as exc_info: | ||
| 63 | + # Wrong ordering here | ||
| 64 | + class Wrongv4Object(RESTObject, Mixin): | ||
| 65 | + ... | ||
| 66 | + | ||
| 67 | + # The error message in the exception should be: | ||
| 68 | + # TypeError: Cannot create a consistent method resolution | ||
| 69 | + # order (MRO) for bases RESTObject, Mixin | ||
| 70 | + | ||
| 71 | + # Make sure the exception string contains "MRO" | ||
| 72 | + assert "MRO" in exc_info.exconly() | ||
| 73 | + | ||
| 74 | + # Correctly ordered class, no exception | ||
| 75 | + class Correctv4Object(Mixin, RESTObject): | ||
| 76 | + ... | ||
| 77 | + | ||
| 78 | + | ||
| 79 | + def test_mros(): | ||
| 80 | + """Ensure objects defined in gitlab.v4.objects have REST* as last item in | ||
| 81 | + class definition. | ||
| 82 | + | ||
| 83 | + We do this as we need to ensure the MRO (Method Resolution Order) is | ||
| 84 | + correct. | ||
| 85 | + """ | ||
| 86 | + | ||
| 87 | + failed_messages = [] | ||
| 88 | + for module_name, module_value in inspect.getmembers(gitlab.v4.objects): | ||
| 89 | + if not inspect.ismodule(module_value): | ||
| 90 | + # We only care about the modules | ||
| 91 | + continue | ||
| 92 | + # Iterate through all the classes in our module | ||
| 93 | + for class_name, class_value in inspect.getmembers(module_value): | ||
| 94 | + if not inspect.isclass(class_value): | ||
| 95 | + continue | ||
| 96 | + | ||
| 97 | + # Ignore imported classes from gitlab.base | ||
| 98 | + if class_value.__module__ == "gitlab.base": | ||
| 99 | + continue | ||
| 100 | + | ||
| 101 | + mro = class_value.mro() | ||
| 102 | + | ||
| 103 | + # We only check classes which have a 'gitlab.base' class in their MRO | ||
| 104 | + has_base = False | ||
| 105 | + for count, obj in enumerate(mro, start=1): | ||
| 106 | + if obj.__module__ == "gitlab.base": | ||
| 107 | + has_base = True | ||
| 108 | + base_classname = obj.__name__ | ||
| 109 | + if has_base: | ||
| 110 | + filename = inspect.getfile(class_value) | ||
| 111 | + # NOTE(jlvillal): The very last item 'mro[-1]' is always going | ||
| 112 | + # to be 'object'. That is why we are checking 'mro[-2]'. | ||
| 113 | + if mro[-2].__module__ != "gitlab.base": | ||
| 114 | + failed_messages.append( | ||
| 115 | + ( | ||
| 116 | + f"class definition for {class_name!r} in file {filename!r} " | ||
| 117 | + f"must have {base_classname!r} as the last class in the " | ||
| 118 | + f"class definition" | ||
| 119 | + ) | ||
| 120 | + ) | ||
| 121 | + failed_msg = "\n".join(failed_messages) | ||
| 122 | + assert not failed_messages, failed_msg | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -159,7 +159,7 @@ class ProjectCommitCommentManager(ListMixin, CreateMixin, RESTManager): | |||
| 159 | 159 | ) | |
| 160 | 160 | ||
| 161 | 161 | ||
| 162 | - class ProjectCommitStatus(RESTObject, RefreshMixin): | ||
| 162 | + class ProjectCommitStatus(RefreshMixin, RESTObject): | ||
| 163 | 163 | pass | |
| 164 | 164 | ||
| 165 | 165 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,7 +8,7 @@ | |||
| 8 | 8 | ] | |
| 9 | 9 | ||
| 10 | 10 | ||
| 11 | - class ProjectDeployment(RESTObject, SaveMixin): | ||
| 11 | + class ProjectDeployment(SaveMixin, RESTObject): | ||
| 12 | 12 | pass | |
| 13 | 13 | ||
| 14 | 14 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,7 +10,7 @@ | |||
| 10 | 10 | ] | |
| 11 | 11 | ||
| 12 | 12 | ||
| 13 | - class ProjectJob(RESTObject, RefreshMixin): | ||
| 13 | + class ProjectJob(RefreshMixin, RESTObject): | ||
| 14 | 14 | @cli.register_custom_action("ProjectJob") | |
| 15 | 15 | @exc.on_http_error(exc.GitlabJobCancelError) | |
| 16 | 16 | def cancel(self, **kwargs): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -30,7 +30,7 @@ | |||
| 30 | 30 | ] | |
| 31 | 31 | ||
| 32 | 32 | ||
| 33 | - class ProjectPipeline(RESTObject, RefreshMixin, ObjectDeleteMixin): | ||
| 33 | + class ProjectPipeline(RefreshMixin, ObjectDeleteMixin, RESTObject): | ||
| 34 | 34 | _managers = ( | |
| 35 | 35 | ("jobs", "ProjectPipelineJobManager"), | |
| 36 | 36 | ("bridges", "ProjectPipelineBridgeManager"), | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -24,7 +24,7 @@ class ProjectReleaseManager(NoUpdateMixin, RESTManager): | |||
| 24 | 24 | ) | |
| 25 | 25 | ||
| 26 | 26 | ||
| 27 | - class ProjectReleaseLink(RESTObject, ObjectDeleteMixin, SaveMixin): | ||
| 27 | + class ProjectReleaseLink(ObjectDeleteMixin, SaveMixin, RESTObject): | ||
| 28 | 28 | pass | |
| 29 | 29 | ||
| 30 | 30 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments