astral-sh / ruff

An extremely fast Python linter and code formatter, written in Rust.
https://docs.astral.sh/ruff
MIT License
32.5k stars 1.08k forks source link

RUF013 undocumented limitation `x: CustomClass = None` is not caught #14018

Open jond01 opened 5 days ago

jond01 commented 5 days ago

It seems like the following should be caught or at least be added to the documented limitations:

from enum import Enum
from typing import Optional

class Letter(Enum):
    """ok."""

    A = "A"

class CatchMe:
    """class."""

def f(
    letter: Letter = None,  # <--- should be caught
    word: Optional[str] = None,
    catch: CatchMe = None,  # <--- should be caught
) -> None:
    """Doc."""
MichaReiser commented 5 days ago

Thanks. I agree that this is surprising. I assume this is "intentional" because Ruff doesn't know to what types Letter resolves. E.g. Letter could be defined as type Letter = str | None in which case the assignment is valid. Not sure if there's another reason for it as well (CC: @charliermarsh )

Documenting this restriction is a good idea.

dhruvmanila commented 4 hours ago

I assume this is "intentional" because Ruff doesn't know to what types Letter resolves. E.g. Letter could be defined as type Letter = str | None in which case the assignment is valid. Not sure if there's another reason for it as well

Yeah, I think this is pretty much the reason. The logic considers all custom type to be similar to Any which is the case here.

For context, the fallback branch tries to resolve the name Letter to a qualified name which doesn't exists and thus fallback to using Unknown (similar to Any).

https://github.com/astral-sh/ruff/blob/4ece8e5c1e674eb0d751fa55b5a72950aa9d20f0/crates/ruff_linter/src/rules/ruff/typing.rs#L116-L119