Skip to content

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

@Avasam

Description

@Avasam

https://docs.astral.sh/ruff/rules/non-self-return-type/

Very similar to #14183
I feel like logically a fix for this rule should be feasible without too much complexity? (at least in pyi files and maybe py files above 3.10, see #9761). The fix should be unsafe as there might be a specific intent, or a stub shows that the runtime returns an instance not actually based on self/cls. Marking it unsafe may also simplify the logic to apply the fix (if Self already exists in scope).

Maybe it can be restricted to where the return type to replace is the same as the class and not overloaded ?

class ArrayDerivative(Derivative):
    def __new__(cls, expr, *variables, **kwargs) -> ArrayDerivative: ... # unsafe autofix to replace with self
class CaptureOutput:
    @overload
    def __new__(
        cls, backend: Literal[CaptureOutputs.PIL] = CaptureOutputs.PIL
    ) -> PILCaptureOutput: ...
    @overload
    def __new__(cls, backend: Literal[CaptureOutputs.NUMPY]) -> NumpyCaptureOutput: ...
    @overload
    def __new__(cls, backend: Literal[CaptureOutputs.NUMPY_FLOAT]) -> NumpyFloatCaptureOutput: ...
    @overload
    def __new__(cls, backend: Literal[CaptureOutputs.PYTORCH]) -> PytorchCaptureOutput: ...
    @overload
    def __new__(
        cls, backend: Literal[CaptureOutputs.PYTORCH_FLOAT]
    ) -> PytorchFloatCaptureOutput: ...
    @overload
    def __new__(cls, backend: Literal[CaptureOutputs.PYTORCH_GPU]) -> PytorchGPUCaptureOutput: ...
    @overload
    def __new__(
        cls, backend: Literal[CaptureOutputs.PYTORCH_FLOAT_GPU]
    ) -> PytorchFloatGPUCaptureOutput: ...
    @overload
    def __new__(cls, backend: CaptureOutputs) -> CaptureOutput: ...
    def __new__(cls, backend: CaptureOutputs = CaptureOutputs.PIL) -> CaptureOutput:  # no autofix
        return cls._initialize_backend(backend)

Metadata

Metadata

Assignees

No one assigned

    Labels

    fixesRelated to suggested fixes for violations

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions