def validate_and_transform_accessors(self, temp_ast: Expression, original_repl: Expression, spec: ConversionSpecifier, ctx: Context) -> bool: """Validate and transform (in-place) format field accessors. On error, report it and return False. The transformations include replacing the dummy variable with actual replacement expression and translating any name expressions in an index into strings, so that this will work: class User(TypedDict): name: str id: int u: User '{[id]:d} -> {[name]}'.format(u) """ if not isinstance(temp_ast, (MemberExpr, IndexExpr)): self.msg.fail('Only index and member expressions are allowed in' ' format field accessors; got "{}"'.format( spec.field), ctx, code=codes.STRING_FORMATTING) return False if isinstance(temp_ast, MemberExpr): node = temp_ast.expr else: node = temp_ast.base if not isinstance(temp_ast.index, (NameExpr, IntExpr)): assert spec.key, "Call this method only after auto-generating keys!" assert spec.field self.msg.fail('Invalid index expression in format field' ' accessor "{}"'.format( spec.field[len(spec.key):]), ctx, code=codes.STRING_FORMATTING) return False if isinstance(temp_ast.index, NameExpr): temp_ast.index = StrExpr(temp_ast.index.name) if isinstance(node, NameExpr) and node.name == DUMMY_FIELD_NAME: # Replace it with the actual replacement expression. assert isinstance( temp_ast, (IndexExpr, MemberExpr)) # XXX: this is redundant if isinstance(temp_ast, IndexExpr): temp_ast.base = original_repl else: temp_ast.expr = original_repl return True node.line = ctx.line node.column = ctx.column return self.validate_and_transform_accessors( node, original_repl=original_repl, spec=spec, ctx=ctx)
def process_typealias(self, lvalue: NameExpr, rvalue: Expression) -> None: p = AliasPrinter(self) self.add("{} = {}\n".format(lvalue.name, rvalue.accept(p))) self.record_name(lvalue.name) self._vars[-1].append(lvalue.name)
def expr(self, expr: Expression) -> Expression: new = expr.accept(self) assert isinstance(new, Expression) new.set_line(expr.line, expr.column) return new
def literal_hash(e: Expression) -> Optional[Key]: return e.accept(_hasher)
def expr(self, expr: Expression) -> Expression: new = expr.accept(self) assert isinstance(new, Expression) new.set_line(expr.line) return new