Skip to content

Commit

Permalink
Add back string value for string type annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
Glyphack committed Oct 10, 2024
1 parent b8c2bb0 commit 7545c4f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion parser/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ pub enum ConstantValue {
None,
Ellipsis,
Bool,
Str,
Str(String),
Bytes,
Tuple,
// Numbers are string because we don't care about the value rn.
Expand Down
12 changes: 6 additions & 6 deletions parser/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ pub fn concat_string_exprs(lhs: Expression, rhs: Expression) -> Result<Expressio
end: rhs.node.end,
};
let concatnated_string = match (lhs.value, rhs.value) {
(ConstantValue::Str, ConstantValue::Str) => {
(ConstantValue::Str(s), ConstantValue::Str(s2)) => {
Expression::Constant(Box::new(Constant {
node,
value: ConstantValue::Str,
value: ConstantValue::Str(s + s2.as_str()),
}))
}
(ConstantValue::Bytes, ConstantValue::Bytes) => {
Expand Down Expand Up @@ -100,10 +100,10 @@ pub fn concat_string_exprs(lhs: Expression, rhs: Expression) -> Result<Expressio
(Expression::JoinedStr(fstring_lhs), Expression::Constant(const_rhs)) => {
let mut values = fstring_lhs.values;
match const_rhs.value {
ConstantValue::Str => {
ConstantValue::Str(s) => {
values.push(Expression::Constant(Box::new(Constant {
node: const_rhs.node,
value: ConstantValue::Str,
value: ConstantValue::Str(s),
})));
}
ConstantValue::Bytes => {
Expand All @@ -121,9 +121,9 @@ pub fn concat_string_exprs(lhs: Expression, rhs: Expression) -> Result<Expressio
}
(Expression::Constant(const_lhs), Expression::JoinedStr(fstring_rhs)) => {
let const_expr = match const_lhs.value {
ConstantValue::Str => Expression::Constant(Box::new(Constant {
ConstantValue::Str(s) => Expression::Constant(Box::new(Constant {
node: const_lhs.node,
value: ConstantValue::Str,
value: ConstantValue::Str(s),
})),
ConstantValue::Bytes => {
panic!("Cannot concat string and bytes");
Expand Down
6 changes: 3 additions & 3 deletions parser/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2495,7 +2495,7 @@ impl<'a> Parser<'a> {
self.bump_any();
Expression::Constant(Box::new(Constant {
node: self.finish_node(start),
value: ConstantValue::Str,
value: ConstantValue::Str(string_val),
}))
}

Expand Down Expand Up @@ -2657,7 +2657,7 @@ impl<'a> Parser<'a> {
self.bump_any();
Expression::Constant(Box::new(Constant {
node: self.finish_node(start),
value: ConstantValue::Str,
value: ConstantValue::Str(string_val),
}))
}

Expand Down Expand Up @@ -3387,7 +3387,7 @@ impl<'a> Parser<'a> {
self.bump(Kind::FStringMiddle);
Ok(Expression::Constant(Box::new(Constant {
node: self.finish_node(node),
value: ConstantValue::Str,
value: ConstantValue::Str(str_val),
})))
}
Kind::LeftBracket => self.parse_fstring_replacement_field(),
Expand Down
10 changes: 5 additions & 5 deletions typechecker/src/type_evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl<'a> TypeEvaluator<'a> {
// typing.readthedocs.io/en/latest/spec/literal.html#backwards-compatibility
ast::ConstantValue::Int => self.get_builtin_type("int"),
ast::ConstantValue::Float => self.get_builtin_type("float"),
ast::ConstantValue::Str => self.get_builtin_type("str"),
ast::ConstantValue::Str(_) => self.get_builtin_type("str"),
ast::ConstantValue::Bool => self.get_builtin_type("bool"),
ast::ConstantValue::None => Some(PythonType::None),
ast::ConstantValue::Bytes => self.get_builtin_type("bytes"),
Expand Down Expand Up @@ -148,7 +148,7 @@ impl<'a> TypeEvaluator<'a> {
};
let type_name = match first_arg {
ast::Expression::Constant(str) => match &str.value {
ast::ConstantValue::Str => str.get_value(&self.file.source),
ast::ConstantValue::Str(s) => str.get_value(s),
_ => panic!("TypeVar first arg must be a string"),
},
_ => panic!("TypeVar must be called with at least one arg"),
Expand Down Expand Up @@ -564,8 +564,8 @@ impl<'a> TypeEvaluator<'a> {
// 2. Module is preferred over local scope so we first check module scope and
// then local scope.
// https://peps.python.org/pep-0563/#backwards-compatibility
ast::ConstantValue::Str => {
let mut parser = Parser::new(&self.file.source);
ast::ConstantValue::Str(ref s) => {
let mut parser = Parser::new(s);
// Wrap the parsing logic inside a `catch_unwind` block
let parse_result = catch_unwind(AssertUnwindSafe(|| parser.parse()));

Expand Down Expand Up @@ -1326,7 +1326,7 @@ impl<'a> TypeEvaluator<'a> {
ast::ConstantValue::Bool => LiteralValue::Bool,
ast::ConstantValue::Int => LiteralValue::Int,
ast::ConstantValue::Float => LiteralValue::Float,
ast::ConstantValue::Str => LiteralValue::Str,
ast::ConstantValue::Str(_) => LiteralValue::Str,
ast::ConstantValue::Bytes => LiteralValue::Bytes,
ast::ConstantValue::None => LiteralValue::None,
// Tuple is illegal if it has parentheses, otherwise it's allowed and the output
Expand Down

0 comments on commit 7545c4f

Please sign in to comment.