spdx_tools.common.typing.type_checks

 1# SPDX-FileCopyrightText: 2023 spdx contributors
 2#
 3# SPDX-License-Identifier: Apache-2.0
 4from dataclasses import fields
 5
 6from beartype.typing import Any, Dict
 7
 8from spdx_tools.common.typing.constructor_type_errors import ConstructorTypeErrors
 9
10
11def check_types_and_set_values(instance_under_construction: Any, local_variables: Dict) -> None:
12    """
13    Helper method to accumulate all type errors encountered during a constructor call and return them in a
14    ConstructorTypeErrors instance.
15    Background: Our setters are enhanced with runtime typechecks using beartype. However, this means that by
16    default, a TypeError is raised on the first type violation that is encountered. We consider it more helpful to
17    return all type violations in one go.
18    As an aside, defining constructors "manually" using this utility method helps avoid a nasty PyCharm bug:
19    https://youtrack.jetbrains.com/issue/PY-34569
20    """
21    errors = []
22    for field in fields(instance_under_construction):
23        key = field.name
24        value = local_variables.get(key)
25        try:
26            setattr(instance_under_construction, key, value)
27        except TypeError as err:
28            error_message: str = err.args[0]
29            errors.append(error_message)
30    if errors:
31        raise ConstructorTypeErrors(errors)
def check_types_and_set_values(instance_under_construction: Any, local_variables: dict) -> None:
12def check_types_and_set_values(instance_under_construction: Any, local_variables: Dict) -> None:
13    """
14    Helper method to accumulate all type errors encountered during a constructor call and return them in a
15    ConstructorTypeErrors instance.
16    Background: Our setters are enhanced with runtime typechecks using beartype. However, this means that by
17    default, a TypeError is raised on the first type violation that is encountered. We consider it more helpful to
18    return all type violations in one go.
19    As an aside, defining constructors "manually" using this utility method helps avoid a nasty PyCharm bug:
20    https://youtrack.jetbrains.com/issue/PY-34569
21    """
22    errors = []
23    for field in fields(instance_under_construction):
24        key = field.name
25        value = local_variables.get(key)
26        try:
27            setattr(instance_under_construction, key, value)
28        except TypeError as err:
29            error_message: str = err.args[0]
30            errors.append(error_message)
31    if errors:
32        raise ConstructorTypeErrors(errors)

Helper method to accumulate all type errors encountered during a constructor call and return them in a ConstructorTypeErrors instance. Background: Our setters are enhanced with runtime typechecks using beartype. However, this means that by default, a TypeError is raised on the first type violation that is encountered. We consider it more helpful to return all type violations in one go. As an aside, defining constructors "manually" using this utility method helps avoid a nasty PyCharm bug: https://youtrack.jetbrains.com/issue/PY-34569