Skip to content
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

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

Merged
merged 3 commits into from
Feb 18, 2025

Conversation

sobolevn
Copy link
Contributor

Summary

CPython mangles private names defined in TypedDict class body, so it is not recommended to use class form with such names, see python/cpython#129567

But, now ruff tries to warn about converting such examples to class form. See graphql-python/graphql-core#234

Test Plan

I included a test case.

Copy link
Contributor

github-actions bot commented Feb 17, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@sobolevn
Copy link
Contributor Author

CC @AlexWaygood

@ntBre ntBre added the rule Implementing or modifying a lint rule label Feb 17, 2025
@@ -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
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

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

Thanks!

@AlexWaygood AlexWaygood changed the title UP013: do not report functional form with private names [pyupgrade] Do not upgrade functional TypedDicts with private names to the class-based syntax (UP018) Feb 18, 2025
@AlexWaygood AlexWaygood changed the title [pyupgrade] Do not upgrade functional TypedDicts with private names to the class-based syntax (UP018) [pyupgrade] Do not upgrade functional TypedDicts with private names to the class-based syntax (UP013) Feb 18, 2025
@AlexWaygood AlexWaygood added the bug Something isn't working label Feb 18, 2025
@AlexWaygood AlexWaygood enabled auto-merge (squash) February 18, 2025 13:01
@AlexWaygood AlexWaygood disabled auto-merge February 18, 2025 13:01
@AlexWaygood AlexWaygood changed the title [pyupgrade] Do not upgrade functional TypedDicts with private names to the class-based syntax (UP013) [pyupgrade] Do not upgrade functional TypedDicts with private field names to the class-based syntax (UP013) Feb 18, 2025
@AlexWaygood AlexWaygood enabled auto-merge (squash) February 18, 2025 13:01
@AlexWaygood AlexWaygood merged commit d8e3fcc into astral-sh:main Feb 18, 2025
20 checks passed
dcreager added a commit that referenced this pull request Feb 18, 2025
* main: (60 commits)
  [`refurb`] Manual timezone monkeypatching (`FURB162`) (#16113)
  [`pyupgrade`] Do not upgrade functional TypedDicts with private field names to the class-based syntax (`UP013`) (#16219)
  Improve docs for PYI019 (#16229)
  Refactor `CallOutcome` to `Result` (#16161)
  Fix minor punctuation errors (#16228)
  Include document specific debug info (#16215)
  Update server to return the debug info as string (#16214)
  [`airflow`] Group `ImportPathMoved` and `ProviderName` to avoid misusing (`AIR303`) (#16157)
  Fix unstable formatting of trailing end-of-line comments of parenthesized attribute values (#16187)
  Ignore source code actions for a notebook cell (#16154)
  Add FAQ entry for `source.*` code actions in Notebook (#16212)
  red-knot: move symbol lookups in `symbol.rs` (#16152)
  better error messages while loading configuration `extend`s (#15658)
  Format `index.css` (#16207)
  Improve API exposed on `ExprStringLiteral` nodes (#16192)
  Update Rust crate tempfile to v3.17.0 (#16202)
  Update cloudflare/wrangler-action action to v3.14.0 (#16203)
  Update NPM Development dependencies (#16199)
  Update Rust crate smallvec to v1.14.0 (#16201)
  Update Rust crate codspeed-criterion-compat to v2.8.0 (#16200)
  ...
dhruvmanila added a commit that referenced this pull request Feb 24, 2025
## Summary

This is mainly on me for not noticing this during the last release but I
noticed in the last changelog that there's only 1 bug fix which didn't
seem correct as I saw multiple of them so I looked at a couple of PRs
that are in "Rule changes" section and the PRs that were marked with the
`bug` label was categorized there because

1. It _also_ had other labels like `rule` and `fixes`
(#16080,
#16110,
#16219, etc.)
2. Some PRs didn't have the `bug` label (but the issue as marked as
`bug`) but _only_ labels like "fixes"
(#16011,
#16132, etc.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants