Skip to content

[flake8-self] Allow private access on object.__new__(cls) instances (SLF001) - #29001

Merged
ntBre merged 2 commits into
astral-sh:mainfrom
dor-sr:fix/slf001-object-new
Oct 2, 2026
Merged

ntBre merged 2 commits into
astral-sh:mainfrom
dor-sr:fix/slf001-object-new

Conversation

@dor-sr

@dor-sr dor-sr commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Closes #28989

SLF001 already skips instances created with super().__new__(cls), but not the same pattern written with object:

class A:
    def __new__(cls):
        obj = object().__new__(cls)
        obj._private_attr = "bar"  # SLF001 before this change

This extends the special case to object().__new__(...) and object.__new__(...), as suggested in the issue. object has to resolve to the builtin, and object() has to be called without arguments.

Test Plan

Added cases to SLF001_1.py: the two object forms with cls, object.__new__(Amet) inside a classmethod, and two that should still be flagged (object(1) and a shadowed object). The snapshot only gains those two diagnostics.

Also checked the reproducer from the issue with a local build: flagged before, clean after.

@astral-sh-bot
astral-sh-bot Bot requested a review from ntBre September 30, 2026 02:28
@astral-sh-bot

astral-sh-bot Bot commented Sep 30, 2026

Copy link
Copy Markdown

ruff-ecosystem results

Linter (stable)

ℹ️ ecosystem check detected linter changes. (+1 -2 violations, +0 -0 fixes in 2 projects; 56 projects unchanged)

apache/airflow (+0 -2 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --no-fix --output-format concise --no-preview --select ALL

- task-sdk/src/airflow/sdk/io/path.py:139:9: SLF001 Private member accessed: `_conn_id`
- task-sdk/src/airflow/sdk/io/path.py:145:13: SLF001 Private member accessed: `_inject_authenticated_fs`

home-assistant/core (+1 -0 violations, +0 -0 fixes)

+ homeassistant/config_entries.py:177:41: RUF100 [*] Unused `noqa` directive (unused: `SLF001`)

Changes by rule (2 rules affected)

code total + violation - violation + fix - fix
SLF001 2 0 2 0 0
RUF100 1 1 0 0 0

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+1 -3 violations, +0 -0 fixes in 2 projects; 56 projects unchanged)

apache/airflow (+0 -2 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --no-fix --output-format concise --preview --select ALL

- task-sdk/src/airflow/sdk/io/path.py:139:9: private-member-access Private member accessed: `_conn_id`
- task-sdk/src/airflow/sdk/io/path.py:145:13: private-member-access Private member accessed: `_inject_authenticated_fs`

home-assistant/core (+1 -1 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --no-fix --output-format concise --preview

- homeassistant/config_entries.py:177:41: noqa-comments [*] `noqa` comment used instead of `ruff: ignore`
+ homeassistant/config_entries.py:177:41: unused-noqa [*] Unused `noqa` directive (unused: `SLF001`)

Changes by rule (3 rules affected)

code total + violation - violation + fix - fix
private-member-access 2 0 2 0 0
unused-noqa 1 1 0 0 0
noqa-comments 1 0 1 0 0

@ntBre ntBre left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks, this looks good to me overall, just one question.

Comment on lines +345 to +347
arg.as_name_expr().is_some_and(|name| {
matches!(&*name.id, "cls" | "mcs")
|| Self::is_current_class_name(name, semantic)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This same validation could be applied to the super version right? I think I'd either apply it to both or omit this additional change from the PR, and I'd probably lean toward the latter since it's unrelated to the issue.

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.

Makes sense, I dropped it. super().__new__ and object.__new__ are handled the same way now, with no check on the argument, and I removed the other-class test case.

@ntBre ntBre added the bug An issue describing something that isn't working, or a PR that fixes a bug label Oct 1, 2026
@ntBre ntBre changed the title [flake8-self] Allow private access on object.__new__(cls) instances (SLF001) [flake8-self] Allow private access on object.__new__(cls) instances (SLF001) Oct 1, 2026

@ntBre ntBre left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you!

@ntBre
ntBre enabled auto-merge (squash) October 2, 2026 14:27
@ntBre
ntBre merged commit 4de8297 into astral-sh:main Oct 2, 2026
51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug An issue describing something that isn't working, or a PR that fixes a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SLF001 false positives in __new__

2 participants