Avoid treating keys as annotations in TypedDict

This commit is contained in:
Charlie Marsh 2022-09-10 15:18:53 -04:00
parent 024472d578
commit c247730bf5
2 changed files with 11 additions and 1 deletions

View File

@ -64,4 +64,4 @@ from typing import List, TypedDict
class Item(TypedDict):
nodes: List[TypedDict("Node", {"id": str})]
nodes: List[TypedDict("Node", {"name": str})]

View File

@ -431,6 +431,7 @@ where
fn visit_expr(&mut self, expr: &'b Expr) {
let prev_in_f_string = self.in_f_string;
let prev_in_literal = self.in_literal;
let prev_in_annotation = self.in_annotation;
// Pre-visit.
match &expr.node {
@ -598,7 +599,9 @@ where
if id == "bound" {
self.visit_annotation(value);
} else {
self.in_annotation = false;
self.visit_expr(value);
self.in_annotation = prev_in_annotation;
}
}
}
@ -614,7 +617,10 @@ where
ExprKind::List { elts, .. }
| ExprKind::Tuple { elts, .. } => {
if elts.len() == 2 {
self.in_annotation = false;
self.visit_expr(&elts[0]);
self.in_annotation = prev_in_annotation;
self.visit_annotation(&elts[1]);
}
}
@ -638,7 +644,9 @@ where
if args.len() > 1 {
if let ExprKind::Dict { keys, values } = &args[1].node {
for key in keys {
self.in_annotation = false;
self.visit_expr(key);
self.in_annotation = prev_in_annotation;
}
for value in values {
self.visit_annotation(value);
@ -677,6 +685,8 @@ where
}
_ => {}
};
self.in_annotation = prev_in_annotation;
self.in_literal = prev_in_literal;
self.in_f_string = prev_in_f_string;
}