예제 #1
0
    def _check_set_elements(
        self,
        node: Union[ast.Set, ast.Dict],
        keys_or_elts: Sequence[ast.AST],
    ) -> None:
        elements: List[str] = []
        element_values = []

        for set_item in keys_or_elts:
            real_item = unwrap_unary_node(set_item)
            if isinstance(real_item, self._elements_in_sets):
                # Similar look:
                source = astor.to_source(set_item)
                elements.append(source.strip().strip('(').strip(')'))

            real_item = unwrap_starred_node(real_item)

            # Non-constant nodes raise ValueError,
            # unhashables raise TypeError:
            with suppress(ValueError, TypeError):
                # Similar value:
                real_item = safe_eval.literal_eval_with_names(
                    real_item,
                ) if isinstance(
                    real_item, self._elements_to_eval,
                ) else set_item
                element_values.append(real_item)
        self._report_set_elements(node, elements, element_values)
예제 #2
0
    def _check_set_elements(
        self,
        node: Union[ast.Set, ast.Dict],
        keys_or_elts: _HashItems,
    ) -> None:
        elements: List[str] = []
        element_values = []

        for set_item in keys_or_elts:
            if set_item is None:
                continue  # happens for `{**a}`

            real_item = unwrap_unary_node(set_item)
            if isinstance(real_item, self._elements_in_sets):
                # Similar look:
                node_repr = source.node_to_string(set_item)
                elements.append(node_repr.strip().strip('(').strip(')'))

            real_item = unwrap_starred_node(real_item)

            # Non-constant nodes raise ValueError,
            # unhashables raise TypeError:
            with suppress(ValueError, TypeError):
                # Similar value:
                element_values.append(
                    safe_eval.literal_eval_with_names(real_item, )
                    if isinstance(
                        real_item,
                        self._elements_to_eval,
                    ) else set_item, )
        self._report_set_elements(node, elements, element_values)