ruff
01a31c08 - Add config option to disable `typing_extensions` imports (#17611)

Comment changes are shownComment changes are hidden
Commit
66 days ago
Add config option to disable `typing_extensions` imports (#17611) Summary -- This PR resolves https://github.com/astral-sh/ruff/issues/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, `typing-extensions`, is `true`, preserving the current behavior. Setting it to `false` will bail out of the new `Checker::typing_importer` method, which has been refactored from the `Checker::import_from_typing` method in https://github.com/astral-sh/ruff/pull/17340), with `None`, which is then handled specially by each rule that calls it. 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. [here]: https://github.com/astral-sh/ruff/issues/9761#issuecomment-2790492853 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.
Author
Parents
  • crates
    • ruff/tests
      • File
        lint.rs
      • snapshots
        • show_settings__display_default_settings.snap
    • ruff_linter/src
      • checkers/ast
        • File
          mod.rs
      • File
        linter.rs
      • rules
        • fastapi/rules
          • File
            fastapi_non_annotated_dependency.rs
        • flake8_annotations
          • File
            helpers.rs
          • rules
            • File
              definition.rs
        • flake8_pyi/rules
          • File
            custom_type_var_for_self.rs
          • File
            duplicate_union_member.rs
          • File
            mod.rs
          • File
            non_self_return_type.rs
          • File
            redundant_none_literal.rs
          • File
            redundant_numeric_union.rs
          • File
            simple_defaults.rs
        • ruff/rules
          • File
            implicit_optional.rs
      • settings
        • File
          mod.rs
      • snapshots
        • ruff_linter__linter__tests__disabled_typing_extensions_fast002_disabled.snap
        • ruff_linter__linter__tests__disabled_typing_extensions_pyi019_adds_typing_extensions.snap
        • ruff_linter__linter__tests__disabled_typing_extensions_pyi019_adds_typing_with_extensions_disabled.snap
        • ruff_linter__linter__tests__disabled_typing_extensions_pyi019_adds_typing_without_extensions_disabled.snap
        • ruff_linter__linter__tests__disabled_typing_extensions_pyi019_does_not_add_typing_extensions.snap
        • ruff_linter__linter__tests__disabled_typing_extensions_pyi034_disabled.snap
        • ruff_linter__linter__tests__disabled_typing_extensions_pyi_pyi026_disabled.snap
    • ruff_workspace/src
      • File
        configuration.rs
      • File
        options.rs
  • File
    ruff.schema.json
Loading