mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-20 04:29:47 +00:00
[ty] allow any string Literal type expression as a key when constructing a TypedDict (#20792)
This commit is contained in:
parent
75f3c0e8e6
commit
db91ac7dce
2 changed files with 48 additions and 7 deletions
|
|
@ -387,21 +387,22 @@ fn validate_from_keywords<'db, 'ast>(
|
|||
|
||||
/// Validates a `TypedDict` dictionary literal assignment,
|
||||
/// e.g. `person: Person = {"name": "Alice", "age": 30}`
|
||||
pub(super) fn validate_typed_dict_dict_literal<'db, 'ast>(
|
||||
context: &InferContext<'db, 'ast>,
|
||||
pub(super) fn validate_typed_dict_dict_literal<'db>(
|
||||
context: &InferContext<'db, '_>,
|
||||
typed_dict: TypedDictType<'db>,
|
||||
dict_expr: &'ast ast::ExprDict,
|
||||
error_node: AnyNodeRef<'ast>,
|
||||
dict_expr: &ast::ExprDict,
|
||||
error_node: AnyNodeRef,
|
||||
expression_type_fn: impl Fn(&ast::Expr) -> Type<'db>,
|
||||
) -> Result<OrderSet<&'ast str>, OrderSet<&'ast str>> {
|
||||
) -> Result<OrderSet<&'db str>, OrderSet<&'db str>> {
|
||||
let mut valid = true;
|
||||
let mut provided_keys = OrderSet::new();
|
||||
|
||||
// Validate each key-value pair in the dictionary literal
|
||||
for item in &dict_expr.items {
|
||||
if let Some(key_expr) = &item.key {
|
||||
if let ast::Expr::StringLiteral(key_literal) = key_expr {
|
||||
let key_str = key_literal.value.to_str();
|
||||
let key_ty = expression_type_fn(key_expr);
|
||||
if let Type::StringLiteral(key_str) = key_ty {
|
||||
let key_str = key_str.value(context.db());
|
||||
provided_keys.insert(key_str);
|
||||
|
||||
let value_type = expression_type_fn(&item.value);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue