Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/pyupgrade/UP013.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,9 @@
X = TypedDict("X", {
"some_config": int, # important
})

# Private names should not be reported (OK)
WithPrivate = TypedDict("WithPrivate", {"__x": int})

# Dunder names should not be reported (OK)
WithDunder = TypedDict("WithDunder", {"__x__": int})
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use ruff_diagnostics::{Applicability, Diagnostic, Edit, Fix, FixAvailability, Violation};
use ruff_macros::{derive_message_formats, ViolationMetadata};
use ruff_python_ast::helpers::is_dunder;
use ruff_python_ast::{self as ast, Arguments, Expr, ExprContext, Identifier, Keyword, Stmt};
use ruff_python_codegen::Generator;
use ruff_python_semantic::SemanticModel;
Expand Down Expand Up @@ -185,7 +184,7 @@ fn fields_from_dict_literal(items: &[ast::DictItem]) -> Option<Vec<Stmt>> {
if !is_identifier(field.to_str()) {
return None;
}
if is_dunder(field.to_str()) {
if field.to_str().starts_with("__") {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be good to add a comment here explaining why it's not safe to convert TypedDicts with these fields to the class-based syntax. And it might be useful to add a sentence or two to the docs explaining why not all functional TypedDicts can be converted (and that therefore this rule deliberately does not flag them), as well!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

return None;
}
Some(create_field_assignment_stmt(field.to_str(), value))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ UP013.py:46:1: UP013 [*] Convert `X` from `TypedDict` functional to class syntax
47 | | "some_config": int, # important
48 | | })
| |__^ UP013
49 |
50 | # Private names should not be reported (OK)
|
= help: Convert `X` to class syntax

Expand All @@ -276,3 +278,6 @@ UP013.py:46:1: UP013 [*] Convert `X` from `TypedDict` functional to class syntax
48 |-})
46 |+class X(TypedDict):
47 |+ some_config: int
49 48 |
50 49 | # Private names should not be reported (OK)
51 50 | WithPrivate = TypedDict("WithPrivate", {"__x": int})