Skip to content

[flake8-pyi] Add "replace with Self" fix (PYI034)#14217

Merged
charliermarsh merged 2 commits intoastral-sh:mainfrom
InSyncWithFoo:PYI034
Nov 9, 2024
Merged

[flake8-pyi] Add "replace with Self" fix (PYI034)#14217
charliermarsh merged 2 commits intoastral-sh:mainfrom
InSyncWithFoo:PYI034

Conversation

@InSyncWithFoo
Copy link
Contributor

Summary

Resolves #14184.

Test Plan

cargo nextest run and cargo insta test.

@github-actions
Copy link
Contributor

github-actions bot commented Nov 9, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Comment on lines +106 to +109
let source_module = if checker.source_type.is_stub() || target_version < (3, 11) {
"typing_extensions"
} else {
"typing"
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
let source_module = if checker.source_type.is_stub() || target_version < (3, 11) {
"typing_extensions"
} else {
"typing"
let source_module = if checker.source_type.is_stub() || target_version >= (3, 11) {
"typing"
} else {
"typing_extensions"

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 we do want to use typing in stubs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think Self can be imported from typing on 3.10 and lower, even in stubs. How about if target_version >= (3, 11) { "typing" } else { "typing_extensions" }?

Copy link
Member

Choose a reason for hiding this comment

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

@InSyncWithFoo is correct -- Self cannot be imported from typing in a .py file or a .pyi file if you want the code to support Python 3.10 or lower (see also: #9761).

In typeshed we always import Self from typing_extensions, because typeshed pretends that typing_extensions is part of the stdlib, and we want our stubs to support Python 3.8-3.13.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks both! I assumed the semantics were similar to PEP 585.

Copy link
Member

@charliermarsh charliermarsh left a comment

Choose a reason for hiding this comment

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

Thanks!

@charliermarsh charliermarsh enabled auto-merge (squash) November 9, 2024 02:08
@charliermarsh charliermarsh merged commit eea6b31 into astral-sh:main Nov 9, 2024
charliermarsh added a commit that referenced this pull request Nov 9, 2024
## Summary

See:
#14217 (comment).

This means we're recommending `typing_extensions` in non-stubs pre-3.11,
which may not be a valid project dependency, but that's a separate issue
(#9761).
@InSyncWithFoo InSyncWithFoo deleted the PYI034 branch November 10, 2024 04:36
@dhruvmanila dhruvmanila added the fixes Related to suggested fixes for violations label Nov 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fixes Related to suggested fixes for violations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Request: Unsafe fix for non-self-return-type/PYI034

4 participants