Repository: astral-sh/ruff
File: scripts/conformance.py (Python)

Code before:
            case Source.NEW:
                return self.new or []
            case Source.OLD:
                return self.old or []
            case Source.EXPECTED:
                return self.expected or []
            case _:
                raise ValueError(f"Invalid source: {source}")

    def classify(self, source: Source) -> Classification:
        if source in self.sources and Source.EXPECTED in self.sources:
            assert self.expected is not None
            distinct_lines = len(
                {
                    diagnostic.location.positions.begin.line
                    for diagnostic in self.diagnostics_by_source(source)
                }
            )
            expected_max = len(self.expected) if self.multi else 1

            if 1 <= distinct_lines <= expected_max:
                return Classification.TRUE_POSITIVE
            else:
                return Classification.FALSE_POSITIVE

        elif source in self.sources and Source.EXPECTED not in self.sources:
            return Classification.FALSE_POSITIVE

        elif Source.EXPECTED in self.sources:
            return Classification.FALSE_NEGATIVE

        else:
            return Classification.TRUE_NEGATIVE

    def _render_row(self, diagnostics: list[Diagnostic]):
        locs = []
        check_names = []
        descriptions = []

        for diagnostic in diagnostics:
            loc = (
                diagnostic.location.as_link()
                if diagnostic.location
                else f"`{diagnostic.tag}`"
            )
            locs.append(loc)
            check_names.append(diagnostic.check_name)
            descriptions.append(diagnostic.description)

        return f"| {'<br>'.join(locs)} | {'<br>'.join(check_names)} | {'<br>'.join(descriptions)} |"


Review comment (suggestion):
```suggestion
        elif source in self.sources:
```

I believe the second part is implicitly given by not taking the first if branch

It might also be more readable if you nested the conditions like so:

```py
if source in self.sources:
	if Source.EXPECTED in self.sources:
		distinct_lines = ... 

	else:
		return Classification.FalsePositive

elif Source.EXPECTED in self.sources:
	return Classification.FALSE_NEGATIVE

else: 
	return Classification.TRUE_NEGATIVE	
```

Code after:
    def multi(self) -> bool:
        return bool(self.expected) and all(
            diagnostic.multi for diagnostic in self.expected
        )

    def diagnostics_by_source(self, source: Source) -> list[Diagnostic]:
        match source:
            case Source.NEW:
                return self.new
            case Source.OLD:
                return self.old
            case Source.EXPECTED:
                return self.expected

    def classify(self, source: Source) -> Evaluation:
        diagnostics = self.diagnostics_by_source(source)

        if source in self.sources:
            if self.optional:
                return Evaluation(
                    classification=Classification.TRUE_POSITIVE,
                    true_positives=len(diagnostics),
                    false_positives=0,
                    true_negatives=0,
                    false_negatives=0,
                )

            if Source.EXPECTED in self.sources:
                distinct_lines = len(
                    {
                        diagnostic.location.positions.begin.line
                        for diagnostic in diagnostics
                    }
                )
                expected_max = len(self.expected) if self.multi else 1

                if 1 <= distinct_lines <= expected_max:
                    return Evaluation(
                        classification=Classification.TRUE_POSITIVE,
                        true_positives=len(diagnostics),
                        false_positives=0,
                        true_negatives=0,
                        false_negatives=0,
                    )
                else:
                    # We select the line with the most diagnostics
                    # as our true positive, while the rest are false positives
                    max_line = max(
                        groupby(
                            diagnostics, key=lambda d: d.location.positions.begin.line
                        ),