# Buggy code
def compile_Shape(shape: qlast.Shape, *, ctx: context.ContextLevel) -> irast.Set:
    expr = setgen.ensure_set(dispatch.compile(shape.expr, ctx=ctx), ctx=ctx)
    expr_stype = setgen.get_set_type(expr, ctx=ctx)
    if not isinstance(expr_stype, s_objtypes.ObjectType):
        raise errors.QueryError(
            f"shapes cannot be applied to {expr_stype.get_verbosename(ctx.env.schema)}"
        )
    view_type = viewgen.process_view(
        stype=expr_stype,
        path_id=expr.path_id,
        elements=shape.elements,
        parser_context=shape.context,
        ctx=ctx,
    )

    return setgen.ensure_set(expr, type_override=view_type, ctx=ctx)


# Traceback (InternalServerError)
ERROR: InternalServerError:
Hint: This is most likely a bug in EdgeDB. Please consider opening an issue ticket at https://github.com/edgedb/edgedb/issues/new?template=bug_report.md
Server traceback:
Traceback (most recent call last):
File "/Users/yury/dev/edge/edgedb/edb/server/procpool/worker.py", line 75, in worker
res = await meth(*args)
File "/Users/yury/dev/edge/edgedb/edb/server/compiler/compiler.py", line 1795, in compile_eql_tokens
return self._compile(ctx=ctx, tokens=eql_tokens)
File "/Users/yury/dev/edge/edgedb/edb/server/compiler/compiler.py", line 1429, in _compile
comp: dbstate.BaseQuery = self._compile_dispatch_ql(ctx, stmt)
File "/Users/yury/dev/edge/edgedb/edb/server/compiler/compiler.py", line 1392, in _compile_dispatch_ql
return self._compile_ql_query(ctx, ql)
File "/Users/yury/dev/edge/edgedb/edb/server/compiler/compiler.py", line 592, in _compile_ql_query
ir = qlcompiler.compile_ast_to_ir(
File "/Users/yury/dev/edge/edgedb/edb/edgeql/compiler/__init__.py", line 175, in wrapper
return func(*args, **kwargs)
File "/Users/yury/dev/edge/edgedb/edb/edgeql/compiler/__init__.py", line 223, in compile_ast_to_ir
ir_set = dispatch_mod.compile(tree, ctx=ctx)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/functools.py", line 874, in wrapper
return dispatch(args[0].__class__)(*args, **kw)
File "/Users/yury/dev/edge/edgedb/edb/edgeql/compiler/stmt.py", line 84, in compile_SelectQuery
stmt.result = compile_result_clause(
File "/Users/yury/dev/edge/edgedb/edb/edgeql/compiler/stmt.py", line 1017, in compile_result_clause
ir_result = compile_query_subject(
File "/Users/yury/dev/edge/edgedb/edb/edgeql/compiler/stmt.py", line 1097, in compile_query_subject
view_scls = viewgen.process_view(
File "/Users/yury/dev/edge/edgedb/edb/edgeql/compiler/viewgen.py", line 86, in process_view
view_scls = _process_view(
File "/Users/yury/dev/edge/edgedb/edb/edgeql/compiler/viewgen.py", line 173, in _process_view
pointer = _normalize_view_ptr_expr(
File "/Users/yury/dev/edge/edgedb/edb/edgeql/compiler/viewgen.py", line 549, in _normalize_view_ptr_expr
ptr_target = _process_view(
File "/Users/yury/dev/edge/edgedb/edb/edgeql/compiler/viewgen.py", line 160, in _process_view
assert isinstance(view_scls, s_objtypes.ObjectType)
AssertionError

# Fixed code
def compile_Shape(shape: qlast.Shape, *, ctx: context.ContextLevel) -> irast.Set:
    expr = setgen.ensure_set(dispatch.compile(shape.expr, ctx=ctx), ctx=ctx)
    expr_stype = setgen.get_set_type(expr, ctx=ctx)
    if not isinstance(expr_stype, s_objtypes.ObjectType):
        raise errors.QueryError(
            f"shapes cannot be applied to {expr_stype.get_verbosename(ctx.env.schema)}",
            context=shape.context,
        )
    view_type = viewgen.process_view(
        stype=expr_stype,
        path_id=expr.path_id,
        elements=shape.elements,
        parser_context=shape.context,
        ctx=ctx,
    )

    return setgen.ensure_set(expr, type_override=view_type, ctx=ctx)