コード例 #1
0
def get_from_python_type(dt, optional: bool = None, overrider=None):
    if dt is None:
        return None

    bc = overrider or get_instantiated_type
    typedt = type(dt)

    if dt == str or typedt == str:
        return String(optional=optional)
    if dt == bool or typedt == bool:
        return Boolean(optional=optional)
    if dt == int or typedt == int:
        return Int(optional=optional)
    if dt == float or typedt == float:
        return Float(optional=optional)

    if is_qualified_generic(dt):

        if str(dt).startswith("typing.List"):
            nt = bc(dt.__args__[0])
            return Array(nt, optional=optional)

        args = dt.__args__
        if len(args) > 2:
            raise Exception(
                f"Janis is unsure how to parse qualfied generic '{dt}'")

        aridxofnonetype = [
            i for i, val in enumerate(a == type(None) for a in args) if val
        ]
        optional = len(aridxofnonetype) > 0

        if len(aridxofnonetype) > 1 and optional is False:
            raise Exception("Janis cannot accept union ")

        idxofsubtype = (len(args) - 1 - aridxofnonetype[0]) if optional else 0
        subtype = args[idxofsubtype]

        nt = bc(subtype, optional=optional)
        return nt

    elif is_generic(dt):
        raise Exception(f"Generic {dt} was generic typing, but unqualified")
コード例 #2
0
 def test_qualified_generic_optional(self):
     self.assertFalse(is_base_generic(Optional[str]))
     self.assertTrue(is_qualified_generic(Optional[str]))
     self.assertTrue(is_generic(Optional[str]))
コード例 #3
0
 def test_qualified_generic_list(self):
     self.assertFalse(is_base_generic(List[str]))
     self.assertTrue(is_qualified_generic(List[str]))
     self.assertTrue(is_generic(List[str]))
コード例 #4
0
 def test_qualified_generic_union(self):
     self.assertFalse(is_base_generic(Union[str, None]))
     self.assertTrue(is_qualified_generic(Union[str, None]))
     self.assertTrue(is_generic(Union[str, None]))
コード例 #5
0
def get_from_python_type(dt, optional: bool = None, overrider=None):
    if dt is None:
        return Boolean(optional=True)

    bc = overrider or get_instantiated_type
    dtt = dt if type(dt) == type else None
    typedt = type(dt)

    try:
        if dtt == str or typedt == str:
            return String(optional=optional)
    except Exception as e:
        print(e)
    if dtt == bool or typedt == bool:
        return Boolean(optional=optional)
    if dtt == int or typedt == int:
        return Int(optional=optional)
    if dtt == float or typedt == float:
        return Float(optional=optional)

    if is_qualified_generic(dt):

        if str(dt).startswith("typing.List"):
            nt = bc(dt.__args__[0], overrider=bc)
            return Array(nt, optional=optional)

        elif str(dt).startswith("typing.Union"):
            subtypes = dt.__args__
            # Filter out None or NoneType
            try:
                new_subtypes = [
                    t for t in subtypes if t is not None and type(None) != t
                ]
            except Exception as e:
                Logger.critical(
                    f"Couldn't determine the appropriate internal types from {str(dt)}, failed with error: {str(e)}"
                )
                raise
            optional = len(subtypes) != len(new_subtypes)

            if len(new_subtypes) == 0:
                raise TypeError(
                    "Unsure how to parse generic: '{str(dt)}', please raise an issue if you think this is in error"
                )

            if len(new_subtypes) == 1:
                return get_instantiated_type(
                    new_subtypes[0], optional=optional, overrider=bc
                )

            nts = [bc(n, overrider=bc) for n in new_subtypes]
            return UnionType(*nts, optional=optional)

        args = dt.__args__
        if len(args) > 2:
            raise Exception(f"Janis is unsure how to parse qualfied generic '{dt}'")

        aridxofnonetype = [
            i for i, val in enumerate(a == type(None) for a in args) if val
        ]
        optional = len(aridxofnonetype) > 0

        if len(aridxofnonetype) > 1 and optional is False:
            raise Exception("Janis cannot accept union ")

        idxofsubtype = (len(args) - 1 - aridxofnonetype[0]) if optional else 0
        subtype = args[idxofsubtype]

        nt = bc(subtype, optional=optional)
        return nt

    elif is_generic(dt):
        raise Exception(f"Generic {dt} was generic typing, but unqualified")