Skip to content

UP045 doesn't trigger inside of cast #20096

@ntjohnson1

Description

@ntjohnson1

Summary

When running UP045 it doesn't seem to trigger on types within cast which when paired with TC006 leads to inconsistent representations.

Versions

$ ruff --version
ruff 0.12.10
$ python --version
Python 3.11.13e

CLI Command

$ ruff check --select TC006,UP045,I <file_with_contents_below>.py

Minimum reproducer

from __future__ import annotations

from typing import Any, Optional, cast


def cast_example(input_item: Any) -> str | None:
    """
    Shows rule inconsistency.

    Original implementation
    def cast_example(input_item: Any) -> Optional[str]:
        return cast(Optional[str], input_item)
    """
    return cast("Optional[str]", input_item)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingruleImplementing or modifying a lint rule

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions