<commit_before>#!/usr/bin/env python3

##
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from copy import copy
from enum import Enum
from typing import Container, Iterable, List, Optional, Set, cast

from avro.errors import AvroRuntimeException
from avro.schema import (
    ArraySchema,
    EnumSchema,
    Field,
    FixedSchema,
    MapSchema,
    NamedSchema,
    RecordSchema,
    Schema,
    UnionSchema,
)


class SchemaType(str, Enum):
    ARRAY = "array"
    BOOLEAN = "boolean"
    BYTES = "bytes"
    DOUBLE = "double"
    ENUM = "enum"
    FIXED = "fixed"
    FLOAT = "float"
    INT = "int"
    LONG = "long"
    MAP = "map"
    NULL = "null"
    RECORD = "record"
    STRING = "string"
    UNION = "union"


class SchemaCompatibilityType(Enum):
    compatible = "compatible"
    incompatible = "incompatible"
    recursion_in_progress = "recursion_in_progress"


class SchemaIncompatibilityType(Enum):
    name_mismatch = "name_mismatch"
    fixed_size_mismatch = "fixed_size_mismatch"
    missing_enum_symbols = "missing_enum_symbols"
    reader_field_missing_default_value = "reader_field_missing_default_value"
    type_mismatch = "type_mismatch"
    missing_union_branch = "missing_union_branch"


PRIMITIVE_TYPES = {
    SchemaType.NULL,
    SchemaType.BOOLEAN,
    SchemaType.INT,
    SchemaType.LONG,
    SchemaType.FLOAT,
    SchemaType.DOUBLE,
    SchemaType.BYTES,
    SchemaType.STRING,
}


class SchemaCompatibilityResult:
    def __init__(
        self,
        compatibility: SchemaCompatibilityType = SchemaCompatibilityType.recursion_in_progress,
        incompatibilities: List[SchemaIncompatibilityType] = None,
        messages: Optional[Set[str]] = None,
        locations: Optional[Set[str]] = None,
    ):
        self.locations = locations or {"/"}
        self.messages = messages or set()
        self.compatibility = compatibility
        self.incompatibilities = incompatibilities or []


def merge(this: SchemaCompatibilityResult, that: SchemaCompatibilityResult) -> SchemaCompatibilityResult:
    """
    Merges two {@code SchemaCompatibilityResult} into a new instance, combining the list of Incompatibilities
    and regressing to the SchemaCompatibilityType.incompatible state if any incompatibilities are encountered.
    :param this: SchemaCompatibilityResult
    :param that: SchemaCompatibilityResult<commit_msg>AVRO-3667: [Python] Python 3.10 CI test fails since a while (#1962)<commit_after>#!/usr/bin/env python3

##
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from copy import copy
from enum import Enum
from typing import Container, Iterable, List, Optional, Set, cast

from avro.errors import AvroRuntimeException
from avro.schema import (
    ArraySchema,
    EnumSchema,
    Field,
    FixedSchema,
    MapSchema,
    NamedSchema,
    RecordSchema,
    Schema,
    UnionSchema,
)


class SchemaType(str, Enum):
    ARRAY = "array"
    BOOLEAN = "boolean"
    BYTES = "bytes"
    DOUBLE = "double"
    ENUM = "enum"
    FIXED = "fixed"
    FLOAT = "float"
    INT = "int"
    LONG = "long"
    MAP = "map"
    NULL = "null"
    RECORD = "record"
    STRING = "string"
    UNION = "union"


class SchemaCompatibilityType(Enum):
    compatible = "compatible"
    incompatible = "incompatible"
    recursion_in_progress = "recursion_in_progress"


class SchemaIncompatibilityType(Enum):
    name_mismatch = "name_mismatch"
    fixed_size_mismatch = "fixed_size_mismatch"
    missing_enum_symbols = "missing_enum_symbols"
    reader_field_missing_default_value = "reader_field_missing_default_value"
    type_mismatch = "type_mismatch"
    missing_union_branch = "missing_union_branch"


PRIMITIVE_TYPES = {
    SchemaType.NULL,
    SchemaType.BOOLEAN,
    SchemaType.INT,
    SchemaType.LONG,
    SchemaType.FLOAT,
    SchemaType.DOUBLE,
    SchemaType.BYTES,
    SchemaType.STRING,
}


class SchemaCompatibilityResult:
    def __init__(
        self,
        compatibility: SchemaCompatibilityType = SchemaCompatibilityType.recursion_in_progress,
        incompatibilities: Optional[List[SchemaIncompatibilityType]] = None,
        messages: Optional[Set[str]] = None,
        locations: Optional[Set[str]] = None,
    ):
        self.locations = locations or {"/"}
        self.messages = messages or set()
        self.compatibility = compatibility
        self.incompatibilities = incompatibilities or []


def merge(this: SchemaCompatibilityResult, that: SchemaCompatibilityResult) -> SchemaCompatibilityResult:
    """
    Merges two {@code SchemaCompatibilityResult} into a new instance, combining the list of Incompatibilities
    and regressing to the SchemaCompatibilityType.incompatible state if any incompatibilities are encountered.
    :param this: SchemaCompatibilityResult
    :param that: SchemaCompatibilityResult
{'fraction_non_alphanumeric': 0.07672634271099744, 'fraction_numerical': 0.003356777493606138, 'mean_word_length': 3.816782140107775, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6861375', 'n_tokens_mistral': 1790, 'n_tokens_neox': 1750, 'n_words': 557}