False positive for python:S6546

Make sure to read this post before raising a thread here:

Then tell us:

  • What language is this for? Python
  • Which rule? python:S6546
  • Why do you believe it’s a false-positive/false-negative? Depending on how I write my code the rule triggers or not
  • Are you using
    • SonarQube Cloud? yes
    • SonarQube Server / Community Build - which version?
    • SonarQube for IDE - which IDE/version?
      • in connected mode with SonarQube Server / Community Build or SonarQube Cloud?
  • How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)

My fastapi code looks more or less like this:

def getresult(
    my_env: str,
    database: Annotated\[Database, Depends(current_database)\],
    regular_class: Annotated\[Dict\[str, ClassA | ClassB\], Depends(a_or_b)\],
    new_class: Annotated\[
        ClassC | ClassD, Depends(c_or_d)
    \],
):
my_code here

I am getting the sur S6546 to trigger on line ClassC | ClassD but not on ClassA | ClassB
I think it should not trigger on any of those lines as the code uses the “or” operator and not the union operator.
It seems that the fact that the code is spreaded on multiple lines creates the issue as I am getting this reported only when the code appears on 3 lines as showed here.

Hi @Stephane_Renou,

Thanks for reaching out. I was able to reproduce the issue based on your snippet. This is indeed a false positive that occurs when a the new syntax is directly used in an Annotated type hint.

I’ve created a ticket in our internal bug tracker to track this.

Cheers,
Sebastian