Repository: astral-sh/ruff
File: crates/ruff_linter/src/checkers/ast/mod.rs (Rust)

Code before:
    ///
    /// A stringized annotation is one enclosed in string quotes:
    /// `foo: "typing.Any"` means the same thing to a type checker as `foo: typing.Any`.
    pub(crate) fn match_maybe_stringized_annotation(
        &self,
        expr: &ast::Expr,
        match_fn: impl FnOnce(&ast::Expr) -> bool,
    ) -> bool {
        if let ast::Expr::StringLiteral(string_annotation) = expr {
            let Some(parsed_annotation) = self.parse_type_annotation(string_annotation).ok() else {
                return false;
            };
            match_fn(parsed_annotation.expression())
        } else {
            match_fn(expr)
        }
    }

    /// Push `diagnostic` if the checker is not in a `@no_type_check` context.
    pub(crate) fn report_type_diagnostic(&self, diagnostic: Diagnostic) {
        if !self.semantic.in_no_type_check() {
            self.report_diagnostic(diagnostic);
        }
    }

    /// Return the [`PythonVersion`] to use for version-related checks.
    pub(crate) fn target_version(&self) -> PythonVersion {
        self.target_version.unwrap_or_default()
    }

    fn with_semantic_checker(&mut self, f: impl FnOnce(&mut SemanticSyntaxChecker, &Checker)) {
        let mut checker = std::mem::take(&mut self.semantic_checker);
        f(&mut checker, self);
        self.semantic_checker = checker;
    }

    /// Create a [`TypingImporter`] that will import `member` from either `typing` or
    /// `typing_extensions`.
    ///
    /// On Python <`version_added_to_typing`, `member` is imported from `typing_extensions`, while
    /// on Python >=`version_added_to_typing`, it is imported from `typing`.
    ///
    /// If the Python version is less than `version_added_to_typing` but
    /// `LinterSettings::typing_extensions` is `false`, this method returns `None`.
    pub(crate) fn typing_importer<'b>(
        &'b self,
        member: &'b str,
        version_added_to_typing: PythonVersion,
    ) -> Option<TypingImporter<'b, 'a>> {
        let source_module = if self.target_version() >= version_added_to_typing {
            "typing"

Review comment (suggestion):
It might be useful to add some more documentation here of what the version represents and to what it defaults

Code after:
    ///
    /// A stringized annotation is one enclosed in string quotes:
    /// `foo: "typing.Any"` means the same thing to a type checker as `foo: typing.Any`.
    pub(crate) fn match_maybe_stringized_annotation(
        &self,
        expr: &ast::Expr,
        match_fn: impl FnOnce(&ast::Expr) -> bool,
    ) -> bool {
        if let ast::Expr::StringLiteral(string_annotation) = expr {
            let Some(parsed_annotation) = self.parse_type_annotation(string_annotation).ok() else {
                return false;
            };
            match_fn(parsed_annotation.expression())
        } else {
            match_fn(expr)
        }
    }

    /// Push `diagnostic` if the checker is not in a `@no_type_check` context.
    pub(crate) fn report_type_diagnostic(&self, diagnostic: Diagnostic) {
        if !self.semantic.in_no_type_check() {
            self.report_diagnostic(diagnostic);
        }
    }

    /// Return the [`PythonVersion`] to use for version-related lint rules.
    ///
    /// If the user did not provide a target version, this defaults to the lowest supported Python
    /// version ([`PythonVersion::default`]).
    ///
    /// Note that this method should not be used for version-related syntax errors emitted by the
    /// parser or the [`SemanticSyntaxChecker`], which should instead default to the _latest_
    /// supported Python version.
    pub(crate) fn target_version(&self) -> PythonVersion {
        self.target_version.linter_version()
    }

    fn with_semantic_checker(&mut self, f: impl FnOnce(&mut SemanticSyntaxChecker, &Checker)) {
        let mut checker = std::mem::take(&mut self.semantic_checker);
        f(&mut checker, self);
        self.semantic_checker = checker;
    }

    /// Create a [`TypingImporter`] that will import `member` from either `typing` or
    /// `typing_extensions`.
    ///
    /// On Python <`version_added_to_typing`, `member` is imported from `typing_extensions`, while
    /// on Python >=`version_added_to_typing`, it is imported from `typing`.
    ///
    /// If the Python version is less than `version_added_to_typing` but
    /// `LinterSettings::typing_extensions` is `false`, this method returns `None`.