def _type_from_runtime(val: Any, ctx: Context, is_typeddict: bool=False) -> Value:
    if isinstance(val, str):
        return _eval_forward_ref(val, ctx, is_typeddict=is_typeddict)
    elif isinstance(val, tuple):
        if (not val):
            return KnownValue(())
        origin = val[0]
        if (len(val) == 2):
            args = (val[1],)
        else:
            args = val[1:]
        return _value_of_origin_args(origin, args, val, ctx)
    elif ((GenericAlias is not None) and isinstance(val, GenericAlias)):
        origin = get_origin(val)
        args = get_args(val)
        if ((origin is tuple) and (not args)):
            return SequenceIncompleteValue(tuple, [])
        return _value_of_origin_args(origin, args, val, ctx)
    elif typing_inspect.is_literal_type(val):
        args = typing_inspect.get_args(val)
        if (len(args) == 0):
            return KnownValue(args[0])
        else:
            return unite_values(*[KnownValue(arg) for arg in args])
    elif typing_inspect.is_union_type(val):
        args = typing_inspect.get_args(val)
        return unite_values(*[_type_from_runtime(arg, ctx) for arg in args])
    elif typing_inspect.is_tuple_type(val):
        args = typing_inspect.get_args(val)
        if (not args):
            return TypedValue(tuple)
        elif ((len(args) == 2) and (args[1] is Ellipsis)):
            return GenericValue(tuple, [_type_from_runtime(args[0], ctx)])
        elif ((len(args) == 1) and (args[0] == ())):
            return SequenceIncompleteValue(tuple, [])
        else:
            args_vals = [_type_from_runtime(arg, ctx) for arg in args]
            return SequenceIncompleteValue(tuple, args_vals)
    elif is_instance_of_typing_name(val, '_TypedDictMeta'):
        required_keys = getattr(val, '__required_keys__', None)
        total = getattr(val, '__total__', True)
        return TypedDictValue({key: _get_typeddict_value(value, ctx, key, required_keys, total) for (key, value) in val.__annotations__.items()})
    elif (val is InitVar):
        return AnyValue(AnySource.inference)
    elif isinstance(val, InitVar):
        return type_from_runtime(val.type)
    elif is_instance_of_typing_name(val, 'AnnotatedMeta'):
        (origin, metadata) = val.__args__
        return _make_annotated(_type_from_runtime(origin, ctx), [KnownValue(v) for v in metadata], ctx)
    elif is_instance_of_typing_name(val, '_AnnotatedAlias'):
        return _make_annotated(_type_from_runtime(val.__origin__, ctx), [KnownValue(v) for v in val.__metadata__], ctx)
    elif typing_inspect.is_generic_type(val):
        origin = typing_inspect.get_origin(val)
        args = typing_inspect.get_args(val)
        if getattr(val, '_special', False):
            args = []
        return _value_of_origin_args(origin, args, val, ctx, is_typeddict=is_typeddict)
    elif typing_inspect.is_callable_type(val):
        args = typing_inspect.get_args(val)
        return _value_of_origin_args(Callable, args, val, ctx)
    elif (val is AsynqCallable):
        return CallableValue(Signature.make([ELLIPSIS_PARAM], is_asynq=True))
    elif isinstance(val, type):
        return _maybe_typed_value(val)
    elif (val is None):
        return KnownValue(None)
    elif (is_typing_name(val, 'NoReturn') or is_typing_name(val, 'Never')):
        return NO_RETURN_VALUE
    elif is_typing_name(val, 'Self'):
        return SelfTVV
    elif (val is typing.Any):
        return AnyValue(AnySource.explicit)
    elif hasattr(val, '__supertype__'):
        if isinstance(val.__supertype__, type):
            return NewTypeValue(val)
        elif typing_inspect.is_tuple_type(val.__supertype__):
            return AnyValue(AnySource.inference)
        else:
            ctx.show_error(f'Invalid NewType {val}')
            return AnyValue(AnySource.error)
    elif typing_inspect.is_typevar(val):
        tv = cast(TypeVar, val)
        return make_type_var_value(tv, ctx)
    elif is_instance_of_typing_name(val, 'ParamSpec'):
        return TypeVarValue(val, is_paramspec=True)
    elif is_instance_of_typing_name(val, 'ParamSpecArgs'):
        return ParamSpecArgsValue(val.__origin__)
    elif is_instance_of_typing_name(val, 'ParamSpecKwargs'):
        return ParamSpecKwargsValue(val.__origin__)
    elif (is_typing_name(val, 'Final') or is_typing_name(val, 'ClassVar')):
        return AnyValue(AnySource.incomplete_annotation)
    elif (typing_inspect.is_classvar(val) or typing_inspect.is_final_type(val)):
        if hasattr(val, '__type__'):
            typ = val.__type__
        else:
            typ = val.__args__[0]
        return _type_from_runtime(typ, ctx)
    elif (is_instance_of_typing_name(val, '_ForwardRef') or is_instance_of_typing_name(val, 'ForwardRef')):
        with ctx.suppress_undefined_names():
            try:
                code = ast.parse(val.__forward_arg__)
            except SyntaxError:
                ctx.show_error(f'Syntax error in forward reference: {val.__forward_arg__}')
                return AnyValue(AnySource.error)
            return _type_from_ast(code.body[0], ctx, is_typeddict=is_typeddict)
    elif (val is Ellipsis):
        return AnyValue(AnySource.explicit)
    elif is_instance_of_typing_name(val, '_TypeAlias'):
        return GenericValue(val.impl_type, [_type_from_runtime(val.type_var, ctx)])
    elif isinstance(val, TypeGuard):
        return AnnotatedValue(TypedValue(bool), [TypeGuardExtension(_type_from_runtime(val.guarded_type, ctx))])
    elif is_instance_of_typing_name(val, '_TypeGuard'):
        return AnnotatedValue(TypedValue(bool), [TypeGuardExtension(_type_from_runtime(val.__type__, ctx))])
    elif isinstance(val, AsynqCallable):
        params = _callable_args_from_runtime(val.args, 'AsynqCallable', ctx)
        sig = Signature.make(params, _type_from_runtime(val.return_type, ctx), is_asynq=True)
        return CallableValue(sig)
    elif isinstance(val, ExternalType):
        try:
            typ = qcore.helpers.object_from_string(val.type_path)
        except Exception:
            ctx.show_error(f'Cannot resolve type {val.type_path!r}')
            return AnyValue(AnySource.error)
        return _type_from_runtime(typ, ctx)
    elif is_instance_of_typing_name(val, '_MaybeRequired'):
        required = is_instance_of_typing_name(val, '_Required')
        if is_typeddict:
            return Pep655Value(required, _type_from_runtime(val.__type__, ctx))
        else:
            cls = ('Required' if required else 'NotRequired')
            ctx.show_error(f'{cls}[] used in unsupported context')
            return AnyValue(AnySource.error)
    elif is_typing_name(val, 'TypeAlias'):
        return AnyValue(AnySource.incomplete_annotation)
    elif is_typing_name(val, 'TypedDict'):
        return KnownValue(TypedDict)
    else:
        origin = get_origin(val)
        if isinstance(origin, type):
            return _maybe_typed_value(origin)
        elif (val is NamedTuple):
            return TypedValue(tuple)
        ctx.show_error(f'Invalid type annotation {val}')
        return AnyValue(AnySource.error)
def _type_from_runtime(val: Any, ctx: Context, is_typeddict: bool=False) -> Value:
    if isinstance(val, str):
        return _eval_forward_ref(val, ctx, is_typeddict=is_typeddict)
    elif isinstance(val, tuple):
        if (not val):
            return KnownValue(())
        origin = val[0]
        if (len(val) == 2):
            args = (val[1],)
        else:
            args = val[1:]
        return _value_of_origin_args(origin, args, val, ctx)
    elif ((GenericAlias is not None) and isinstance(val, GenericAlias)):
        origin = get_origin(val)
        args = get_args(val)
        if ((origin is tuple) and (not args)):
            return SequenceIncompleteValue(tuple, [])
        return _value_of_origin_args(origin, args, val, ctx)
    elif typing_inspect.is_literal_type(val):
        args = typing_inspect.get_args(val)
        if (len(args) == 0):
            return KnownValue(args[0])
        else:
            return unite_values(*[KnownValue(arg) for arg in args])
    elif typing_inspect.is_union_type(val):
        args = typing_inspect.get_args(val)
        return unite_values(*[_type_from_runtime(arg, ctx) for arg in args])
    elif typing_inspect.is_tuple_type(val):
        args = typing_inspect.get_args(val)
        if (not args):
            return TypedValue(tuple)
        elif ((len(args) == 2) and (args[1] is Ellipsis)):
            return GenericValue(tuple, [_type_from_runtime(args[0], ctx)])
        elif ((len(args) == 1) and (args[0] == ())):
            return SequenceIncompleteValue(tuple, [])
        else:
            args_vals = [_type_from_runtime(arg, ctx) for arg in args]
            return SequenceIncompleteValue(tuple, args_vals)
    elif is_instance_of_typing_name(val, '_TypedDictMeta'):
        required_keys = getattr(val, '__required_keys__', None)
        total = getattr(val, '__total__', True)
        return TypedDictValue({key: _get_typeddict_value(value, ctx, key, required_keys, total) for (key, value) in val.__annotations__.items()})
    elif (val is InitVar):
        return AnyValue(AnySource.inference)
    elif isinstance(val, InitVar):
        return type_from_runtime(val.type)
    elif is_instance_of_typing_name(val, 'AnnotatedMeta'):
        (origin, metadata) = val.__args__
        return _make_annotated(_type_from_runtime(origin, ctx), [KnownValue(v) for v in metadata], ctx)
    elif is_instance_of_typing_name(val, '_AnnotatedAlias'):
        return _make_annotated(_type_from_runtime(val.__origin__, ctx), [KnownValue(v) for v in val.__metadata__], ctx)
    elif typing_inspect.is_generic_type(val):
        origin = typing_inspect.get_origin(val)
        args = typing_inspect.get_args(val)
        if getattr(val, '_special', False):
            args = []
        return _value_of_origin_args(origin, args, val, ctx, is_typeddict=is_typeddict)
    elif typing_inspect.is_callable_type(val):
        args = typing_inspect.get_args(val)
        return _value_of_origin_args(Callable, args, val, ctx)
    elif (val is AsynqCallable):
        return CallableValue(Signature.make([ELLIPSIS_PARAM], is_asynq=True))
    elif isinstance(val, type):
        return _maybe_typed_value(val)
    elif (val is None):
        return KnownValue(None)
    elif (is_typing_name(val, 'NoReturn') or is_typing_name(val, 'Never')):
        return NO_RETURN_VALUE
    elif is_typing_name(val, 'Self'):
        return SelfTVV
    elif (val is typing.Any):
        return AnyValue(AnySource.explicit)
    elif hasattr(val, '__supertype__'):
        if isinstance(val.__supertype__, type):
            return NewTypeValue(val)
        elif typing_inspect.is_tuple_type(val.__supertype__):
            return AnyValue(AnySource.inference)
        else:
            ctx.show_error(f'Invalid NewType {val}')
            return AnyValue(AnySource.error)
    elif typing_inspect.is_typevar(val):
        tv = cast(TypeVar, val)
        return make_type_var_value(tv, ctx)
    elif is_instance_of_typing_name(val, 'ParamSpec'):
        return TypeVarValue(val, is_paramspec=True)
    elif is_instance_of_typing_name(val, 'ParamSpecArgs'):
        return ParamSpecArgsValue(val.__origin__)
    elif is_instance_of_typing_name(val, 'ParamSpecKwargs'):
        return ParamSpecKwargsValue(val.__origin__)
    elif (is_typing_name(val, 'Final') or is_typing_name(val, 'ClassVar')):
        return AnyValue(AnySource.incomplete_annotation)
    elif (typing_inspect.is_classvar(val) or typing_inspect.is_final_type(val)):
        if hasattr(val, '__type__'):
            typ = val.__type__
        else:
            typ = val.__args__[0]
        return _type_from_runtime(typ, ctx)
    elif (is_instance_of_typing_name(val, '_ForwardRef') or is_instance_of_typing_name(val, 'ForwardRef')):
        with ctx.suppress_undefined_names():
            try:
                code = ast.parse(val.__forward_arg__)
            except SyntaxError:
                ctx.show_error(f'Syntax error in forward reference: {val.__forward_arg__}')
                return AnyValue(AnySource.error)
            return _type_from_ast(code.body[0], ctx, is_typeddict=is_typeddict)
    elif (val is Ellipsis):
        return AnyValue(AnySource.explicit)
    elif is_instance_of_typing_name(val, '_TypeAlias'):
        return GenericValue(val.impl_type, [_type_from_runtime(val.type_var, ctx)])
    elif isinstance(val, TypeGuard):
        return AnnotatedValue(TypedValue(bool), [TypeGuardExtension(_type_from_runtime(val.guarded_type, ctx))])
    elif is_instance_of_typing_name(val, '_TypeGuard'):
        return AnnotatedValue(TypedValue(bool), [TypeGuardExtension(_type_from_runtime(val.__type__, ctx))])
    elif isinstance(val, AsynqCallable):
        params = _callable_args_from_runtime(val.args, 'AsynqCallable', ctx)
        sig = Signature.make(params, _type_from_runtime(val.return_type, ctx), is_asynq=True)
        return CallableValue(sig)
    elif isinstance(val, ExternalType):
        try:
            typ = qcore.helpers.object_from_string(val.type_path)
        except Exception:
            ctx.show_error(f'Cannot resolve type {val.type_path!r}')
            return AnyValue(AnySource.error)
        return _type_from_runtime(typ, ctx)
    elif is_instance_of_typing_name(val, '_MaybeRequired'):
        required = is_instance_of_typing_name(val, '_Required')
        if is_typeddict:
            return Pep655Value(required, _type_from_runtime(val.__type__, ctx))
        else:
            cls = ('Required' if required else 'NotRequired')
            ctx.show_error(f'{cls}[] used in unsupported context')
            return AnyValue(AnySource.error)
    elif is_typing_name(val, 'TypeAlias'):
        return AnyValue(AnySource.incomplete_annotation)
    elif is_typing_name(val, 'TypedDict'):
        return KnownValue(TypedDict)
    else:
        origin = get_origin(val)
        if isinstance(origin, type):
            return _maybe_typed_value(origin)
        elif (val is NamedTuple):
            return TypedValue(tuple)
        ctx.show_error(f'Invalid type annotation {val}')
        return AnyValue(AnySource.error)