Skip to content

[pyupgrade] Do not upgrade functional TypedDicts with private field names to the class-based syntax (UP013) #16219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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 All @@ -15,12 +14,22 @@ use crate::checkers::ast::Checker;
/// Checks for `TypedDict` declarations that use functional syntax.
///
/// ## Why is this bad?
/// `TypedDict` subclasses can be defined either through a functional syntax
/// `TypedDict` types can be defined either through a functional syntax
/// (`Foo = TypedDict(...)`) or a class syntax (`class Foo(TypedDict): ...`).
///
/// The class syntax is more readable and generally preferred over the
/// functional syntax.
///
/// Nonetheless, there are some situations in which it is impossible to use
/// the class-based syntax. This rule will not apply to those cases. Namely,
/// it is impossible to use the class-based syntax if any `TypedDict` fields are:
/// - Not valid [python identifiers] (for example, `@x`)
/// - [Python keywords] such as `in`
/// - [Private names] such as `__id` that would undergo [name mangling] at runtime
/// if the class-based syntax was used
/// - [Dunder names] such as `__int__` that can confuse type checkers if they're used
/// with the class-based syntax.
///
/// ## Example
/// ```python
/// from typing import TypedDict
Expand All @@ -45,6 +54,12 @@ use crate::checkers::ast::Checker;
///
/// ## References
/// - [Python documentation: `typing.TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict)
///
/// [Private names]: https://docs.python.org/3/tutorial/classes.html#private-variables
/// [name mangling]: https://docs.python.org/3/reference/expressions.html#private-name-mangling
/// [python identifiers]: https://docs.python.org/3/reference/lexical_analysis.html#identifiers
/// [Python keywords]: https://docs.python.org/3/reference/lexical_analysis.html#keywords
/// [Dunder names]: https://docs.python.org/3/reference/lexical_analysis.html#reserved-classes-of-identifiers
#[derive(ViolationMetadata)]
pub(crate) struct ConvertTypedDictFunctionalToClass {
name: String,
Expand Down Expand Up @@ -185,7 +200,10 @@ 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()) {
// Converting TypedDict to class-based syntax is not safe if fields contain
// private or dunder names, because private names will be mangled and dunder
// names can confuse type checkers.
if field.to_str().starts_with("__") {
Copy link
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
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})
Loading