Skip to content

Add config option to disable typing_extensions imports #17611

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

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

ntBre
Copy link
Contributor

@ntBre ntBre commented Apr 24, 2025

Summary

This PR resolves #9761 by adding a linter configuration option to disable
typing_extensions imports. As mentioned here, it would be ideal if we could
detect whether or not typing_extensions is available as a dependency
automatically, but this seems like a much easier fix in the meantime.

The default for the new option, disable-typing-extensions, is false,
preserving the current behavior. Setting it to true will bail out of the new
Checker::import_from_typing method (#17340)
with Ok(None), which had to be handled specially by each rule that calls import_from_typing.

I'm not particularly happy with this API. It's too easy to pass the result to Diagnostic::try_set_optional_fix when you really need to return without a diagnostic in the Ok(None) case because the lint is not actionable if the user has explicitly disabled typing_extensions. On the other hand, this is actually a benefit when you don't need to consider this case, such as when PythonVersion::lowest is used and the Ok(None) case is impossible.

I considered some alternatives to a config option, such as checking if typing_extensions has been imported or checking for a TYPE_CHECKING block we could use, but I think defaulting to allowing typing_extensions imports and allowing the user to disable this with an option is both simple to implement and pretty intuitive.

Test Plan

New linter tests exercising several combinations of Python versions and the new config option for PYI019. I also added tests for the other affected rules, but only in the case where the new config option is enabled. The rules' existing tests also cover the default case.

ntBre added 12 commits April 24, 2025 10:23
Summary
--

This PR resolves #9761 by adding a linter configuration option to disable
`typing_extensions` imports. As mentioned [here], it would be ideal if we could
detect whether or not `typing_extensions` is available as a dependency
automatically, but this seems like a much easier fix in the meantime.

The default for the new option `disable-typing-extensions` is `false`,
preserving the current behavior. Setting it to `true` will bail out of the new
`Checker::import_from_typing` method (#17340) with a new
`TypingImportError::TypingExtensionsDisabled` error, which should log an
explanatory message whenever `Diagnostic::try_set_fix` is used.

[here]: #9761 (comment)

Test Plan
--

New linter tests exercising several combinations of Python versions and the new
config option for PYI019. We could also test other rules, but any rule using
`import_from_typing` should behave the same way.
this has more to do with the python version than the config option. changing the
option doesn't actually have any effect here. the version must be validated
somewhere else along the way
@ntBre ntBre added rule Implementing or modifying a lint rule configuration Related to settings and configuration labels Apr 24, 2025
Copy link
Contributor

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@ntBre ntBre marked this pull request as ready for review April 24, 2025 16:02
@ntBre ntBre requested a review from AlexWaygood as a code owner April 24, 2025 16:02
@ntBre ntBre requested a review from MichaReiser April 25, 2025 15:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
configuration Related to settings and configuration rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PYI019 causes false positives on .py files if you support Python <=3.10
1 participant