Skip to content
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

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

Closed
Avasam opened this issue Nov 8, 2024 · 0 comments · Fixed by #14217
Closed

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

Avasam opened this issue Nov 8, 2024 · 0 comments · Fixed by #14217
Labels
fixes Related to suggested fixes for violations

Comments

@Avasam
Copy link

Avasam commented Nov 8, 2024

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)
@dhruvmanila dhruvmanila added the fixes Related to suggested fixes for violations label Nov 8, 2024
charliermarsh pushed a commit that referenced this issue Nov 9, 2024
## Summary

Resolves #14184.

## Test Plan

`cargo nextest run` and `cargo insta test`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixes Related to suggested fixes for violations
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants