diff options
author | Walter Rowe <walter.rowe@gmail.com> | 2022-05-31 15:36:14 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-31 21:36:14 +0200 |
commit | 3fa330cc341bbedb163ba757c7f6578d735c6efb (patch) | |
tree | 6a27d234a22e77233990ba89991aa41d438c2528 /gitlab/utils.py | |
parent | 37eb8e0a4f0fd5fc7e221163b84df3461e64475b (diff) | |
download | gitlab-3fa330cc341bbedb163ba757c7f6578d735c6efb.tar.gz |
feat: support mutually exclusive attributes and consolidate validation to fix board lists (#2037)
add exclusive tuple to RequiredOptional data class to support for
mutually exclusive attributes
consolidate _check_missing_create_attrs and _check_missing_update_attrs
from mixins.py into _validate_attrs in utils.py
change _create_attrs in board list manager classes from
required=('label_ld',) to
exclusive=('label_id','asignee_id','milestone_id')
closes https://github.com/python-gitlab/python-gitlab/issues/1897
Diffstat (limited to 'gitlab/utils.py')
-rw-r--r-- | gitlab/utils.py | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/gitlab/utils.py b/gitlab/utils.py index a05cb22..e8eb941 100644 --- a/gitlab/utils.py +++ b/gitlab/utils.py @@ -19,7 +19,7 @@ import pathlib import traceback import urllib.parse import warnings -from typing import Any, Callable, Dict, Optional, Tuple, Type, Union +from typing import Any, Callable, Dict, List, Optional, Tuple, Type, Union import requests @@ -161,3 +161,30 @@ def warn( stacklevel=stacklevel, source=source, ) + + +def _validate_attrs( + data: Dict[str, Any], + attributes: types.RequiredOptional, + excludes: Optional[List[str]] = None, +) -> None: + if excludes is None: + excludes = [] + + if attributes.required: + required = [k for k in attributes.required if k not in excludes] + missing = [attr for attr in required if attr not in data] + if missing: + raise AttributeError(f"Missing attributes: {', '.join(missing)}") + + if attributes.exclusive: + exclusives = [attr for attr in data if attr in attributes.exclusive] + if len(exclusives) > 1: + raise AttributeError( + f"Provide only one of these attributes: {', '.join(exclusives)}" + ) + if not exclusives: + raise AttributeError( + "Must provide one of these attributes: %(attrs)s" + % {"attrs": ", ".join(attributes.exclusive)} + ) |