Skip to content

[pylint] Ignore stub files in import-private-name (PLC2701) - #27196

Closed
Avasam wants to merge 1 commit into
astral-sh:mainfrom
Avasam:Ignore-import-private-name-(PLC2701)-in-stubs
Closed

[pylint] Ignore stub files in import-private-name (PLC2701)#27196
Avasam wants to merge 1 commit into
astral-sh:mainfrom
Avasam:Ignore-import-private-name-(PLC2701)-in-stubs

Conversation

@Avasam

@Avasam Avasam commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

Summary

Test Plan

  1. Look at new test fixtures and snapshots
  2. Ecosystem results unfortunately don't say much ...

Coding Agent disclaimer

Used Claude Opus 5 to help me generate complete test and reword in-code comments. PR description fully handwritten.

Stubs describe the runtime rather than being part of it, so importing a
private name is often the only accurate option — re-exporting a private
base class, or using the helper types in `_typeshed`. In `.py` files
such imports are already exempt when used only in annotations, which
does not help stubs where the import may be unreferenced or aliased.

Closes astral-sh#15294
Closes astral-sh#15295
@astral-sh-bot
astral-sh-bot Bot requested a review from ntBre July 26, 2026 17:58
@astral-sh-bot

astral-sh-bot Bot commented Jul 26, 2026

Copy link
Copy Markdown

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@ntBre

ntBre commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Hmm, I think there's still a decision needed here. The downside of hard-coding this exclusion is that now nobody can enforce this rule for their stub files, even if they wanted to. In some, or maybe most, cases the annotation may be out of the stub author's control, but I can at least imagine cases where it leads you to find a public re-export or something. The allowlist that Micha mentioned here or using per-file-ignores would be other ways of accomplishing this.

I guess the rule is in preview, though, so we could just try it out.

I also didn't think this would close #15294 since that seems to track support for a decorator, but you opened the issue, so I guess you can consider this closing it if you want :)

@ntBre ntBre added rule Implementing or modifying a lint rule needs-decision Awaiting a decision from a maintainer preview Related to preview mode features labels Jul 27, 2026
@Avasam

Avasam commented Jul 27, 2026

Copy link
Copy Markdown
Contributor Author

Concerning #15294 and @type_check_only, that decorator is only usable in stubs, so if stubs are ignored, that request is superseeded.

Another, holistic alternative, _typeshed could be considered a "public underscore prefixed symbol", which #18143 (also PLC2701 related) mentions it would be a good idea to list such symbols for reuse across rules. Which also means no allowlist needed (yet).

Finally, I completely forgot about https://docs.astral.sh/ruff/settings/#lint_per-file-ignores , which I totally already use in my own shared configs. If there's real value in keeping import-private-name (PLC2701) for stubs, then that's the route to go. Even if I wanted to argue that it's a better default to ignore this rule in stubs, there's no way to enable it back.

@MichaReiser

Copy link
Copy Markdown
Member

I feel less comfortable about this exclusion compared to other rules that make stub-file exclusions. I think the difference here is that a stub can use private types to declare its public interface. Those types aren't considered public and the stub maintainer can decide to change/delete these types in any release. E.g. a library can have a type alias to some internal type, where the internal type is different based on python version. The public API only uses the type alias. If your stub now reaches and uses the type alias target (the private type), then this is a private-name violation that won't be caught at the runtime side (and may also not be strictly necessary to truthfully type that module).

@ntBre

ntBre commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Okay, let's close this for now and rely on per-file-ignores for this use case. We can keep the other issues open to track _typeshed handling.

@ntBre ntBre closed this Jul 29, 2026
@Avasam
Avasam deleted the Ignore-import-private-name-(PLC2701)-in-stubs branch July 29, 2026 15:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-decision Awaiting a decision from a maintainer preview Related to preview mode features rule Implementing or modifying a lint rule

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ignoring import-private-name (PLC2701) for _typeshed import import-private-name (PLC2701) does not respect @type_check_only

3 participants