astral-sh / ruff

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

ERA001 false positive on examples in comments #10072

Open 9999years opened 5 months ago

9999years commented 5 months ago

Ruff treats this as commented-out code, despite being an example.

# NOTE: Only upper-case fields in `extra` will be sent to `journald`.
# For instance, this:
#
#     logging.error("Uh oh!", extra={"payload": "my-payload"})
#
# Will not show up as a `payload` field in `journalctl`, but this will:
#
#     logging.error("Uh oh!", extra={"PAYLOAD": "my-payload"})
journal_handler = JournalHandler()

It would be nice if Ruff could recognize "for example" or ```python-blocks as "example code".

MichaReiser commented 5 months ago

Hmm that's an interesting one. The rule is working as intended but I can see how it is undesired in this specific case.

I would favor the solution where the rule ignores Python code blocks, although that would require that the rule supports some form of markdown parsing, which seems a bit much.

ottaviohartman commented 5 months ago

It's a bit of a hack, but you can use triple quotes to get around this case https://play.ruff.rs/42481cc7-c07b-4158-9b6b-c65f0be7c09c

kurellajunior commented 3 months ago

Mine is different but matches the title. Maybe the rule is too eager? It worked on ruff 0.2.1, FP on 0.3.7

    # case 1: both codes are CREATE
    if cond1:
        pass
    # case 2: one code is a CREATE, the other is an existing 
    elif cond2:
        pass
    # case 3: both codes are existing
    elif cond3:
        pass

removing the colons removes the FP, is that how it is meant to be?