Skip to content

Commit

Permalink
[pyupgrade] Do not upgrade functional TypedDicts with private field…
Browse files Browse the repository at this point in the history
… names to the class-based syntax (`UP013`) (#16219)
  • Loading branch information
sobolevn authored Feb 18, 2025
1 parent 66a0467 commit d8e3fcc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
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("__") {
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})

0 comments on commit d8e3fcc

Please sign in to comment.