(schema: JsonSchema)
return None
Given a schema, returns a positive enumeration set.
    This is very conservative, and even includes negated enum constants
    (since the assumption is that they may, in some contexts, be valid)
Given a schema, returns a positive enumeration set.
    This is very conservative, and even includes negated enum constants
    (since the assumption is that they may, in some contexts, be valid)
Given a schema returns a positive enumeration set . This is very conservative and even includes negated enum constants ( since the assumption is that they may in some contexts be valid )
def schemaToDiscoveredEnums(schema: JsonSchema) -> Optional[DiscoveredEnums]:
    """Given a schema, returns a positive enumeration set.
    This is very conservative, and even includes negated enum constants
    (since the assumption is that they may, in some contexts, be valid)
    """

    def combineDiscoveredEnums(
        combine: Callable[[Iterable[SchemaEnum]], Optional[SchemaEnum]],
        des: Iterable[Optional[DiscoveredEnums]],
    ) -> Optional[DiscoveredEnums]:
        enums: List[SchemaEnum] = []
        children: Dict[str, List[DiscoveredEnums]] = {}
        for de in des:
            if de is None:
                continue
            if de.enums is not None:
                enums.append(de.enums)
            if de.children is not None:
                for cn, cv in de.children.items():
                    if cv is None:
                        continue
                    if cn in children:
                        children[cn].append(cv)
                    else:
                        children[cn] = [cv]

        combined_enums: Optional[SchemaEnum] = None
        if enums:
            combined_enums = combine(enums)

        if not children:
            if combined_enums is None:
                return None
            else:
                return DiscoveredEnums(enums=combined_enums)
        else:
            combined_children: Dict[str, DiscoveredEnums] = {}
            for ccn, ccv in children.items():
                if not ccv:
                    continue

                ccvc = combineDiscoveredEnums(combine, ccv)
                if ccvc is not None:
                    combined_children[ccn] = ccvc
            return DiscoveredEnums(enums=combined_enums, children=combined_children)

    def joinDiscoveredEnums(
        des: Iterable[Optional[DiscoveredEnums]],
    ) -> Optional[DiscoveredEnums]:
        def op(args: Iterable[SchemaEnum]) -> Optional[SchemaEnum]:
            return set.union(*args)

        return combineDiscoveredEnums(op, des)

    def meetDiscoveredEnums(
        des: Tuple[Optional[DiscoveredEnums], ...]
    ) -> Optional[DiscoveredEnums]:
        def op(args: Iterable[SchemaEnum]) -> Optional[SchemaEnum]:
            return set.intersection(*args)

        return combineDiscoveredEnums(op, des)

    if schema is True or schema is False:
        return None
    if "enum" in schema:
        # TODO: we should validate the enum elements according to the schema, like schema2search_space does
        return DiscoveredEnums(enums=set(schema["enum"]))
    if "type" in schema:
        typ = schema["type"]
        if typ == "object" and "properties" in schema:
            props = schema["properties"]
            pret: Dict[str, DiscoveredEnums] = {}
            for p, s in props.items():
                pos = schemaToDiscoveredEnums(s)
                if pos is not None:
                    pret[p] = pos
            if pret:
                return DiscoveredEnums(children=pret)
            else:
                return None
        else:
            return None

    if "not" in schema:
        neg = schemaToDiscoveredEnums(schema["not"])
        return neg

    if "allOf" in schema:
        posl = [schemaToDiscoveredEnums(s) for s in schema["allOf"]]
        return joinDiscoveredEnums(posl)

    if "anyOf" in schema:
        posl = [schemaToDiscoveredEnums(s) for s in schema["anyOf"]]
        return joinDiscoveredEnums(posl)

    if "oneOf" in schema:
        posl = [schemaToDiscoveredEnums(s) for s in schema["oneOf"]]
        return joinDiscoveredEnums(posl)
    return None
def schemaToDiscoveredEnums ( schema : JsonSchema ) -> Optional [ DiscoveredEnums ] : def combineDiscoveredEnums ( combine : Callable [ [ Iterable [ SchemaEnum ] ] , Optional [ SchemaEnum ] ] , des : Iterable [ Optional [ DiscoveredEnums ] ] , ) -> Optional [ DiscoveredEnums ] : enums : List [ SchemaEnum ] = [ ] children : Dict [ str , List [ DiscoveredEnums ] ] = { } for de in des : if de is None : continue if de . enums is not None : enums . append ( de . enums ) if de . children is not None : for cn , cv in de . children . items ( ) : if cv is None : continue if cn in children : children [ cn ] . append ( cv ) else : children [ cn ] = [ cv ] combined_enums : Optional [ SchemaEnum ] = None if enums : combined_enums = combine ( enums ) if not children : if combined_enums is None : return None else : return DiscoveredEnums ( enums = combined_enums ) else : combined_children : Dict [ str , DiscoveredEnums ] = { } for ccn , ccv in children . items ( ) : if not ccv : continue ccvc = combineDiscoveredEnums ( combine , ccv ) if ccvc is not None : combined_children [ ccn ] = ccvc return DiscoveredEnums ( enums = combined_enums , children = combined_children ) def joinDiscoveredEnums ( des : Iterable [ Optional [ DiscoveredEnums ] ] , ) -> Optional [ DiscoveredEnums ] : def op ( args : Iterable [ SchemaEnum ] ) -> Optional [ SchemaEnum ] : return set . union ( * args ) return combineDiscoveredEnums ( op , des ) def meetDiscoveredEnums ( des : Tuple [ Optional [ DiscoveredEnums ] , ... ] ) -> Optional [ DiscoveredEnums ] : def op ( args : Iterable [ SchemaEnum ] ) -> Optional [ SchemaEnum ] : return set . intersection ( * args ) return combineDiscoveredEnums ( op , des ) if schema is True or schema is False : return None if "enum" in schema : # TODO: we should validate the enum elements according to the schema, like schema2search_space does return DiscoveredEnums ( enums = set ( schema [ "enum" ] ) ) if "type" in schema : typ = schema [ "type" ] if typ == "object" and "properties" in schema : props = schema [ "properties" ] pret : Dict [ str , DiscoveredEnums ] = { } for p , s in props . items ( ) : pos = schemaToDiscoveredEnums ( s ) if pos is not None : pret [ p ] = pos if pret : return DiscoveredEnums ( children = pret ) else : return None else : return None if "not" in schema : neg = schemaToDiscoveredEnums ( schema [ "not" ] ) return neg if "allOf" in schema : posl = [ schemaToDiscoveredEnums ( s ) for s in schema [ "allOf" ] ] return joinDiscoveredEnums ( posl ) if "anyOf" in schema : posl = [ schemaToDiscoveredEnums ( s ) for s in schema [ "anyOf" ] ] return joinDiscoveredEnums ( posl ) if "oneOf" in schema : posl = [ schemaToDiscoveredEnums ( s ) for s in schema [ "oneOf" ] ] return joinDiscoveredEnums ( posl ) return None