python:S1192 String literals should not be duplicated - with Type hints (PEP 484)

Code example:

"""
docstring
"""

import httpx

async with httpx.AsyncClient() as client:
    r1: 'httpx.Response' = await client.get('https://www.example.org/')
    r2: 'httpx.Response' = await client.get('https://www.example.org/')
    r3: 'httpx.Response' = await client.get('https://www.example.org/')
    print(r1.status_code, r2.status_code, r3.status_code)

The code above is showing the code smell

python:S1192 String literals should not be duplicated

SonarQube Scanner 4.2.0.1873
Java 11.0.3 AdoptOpenJDK (64-bit)
SonarQube Community Edition Version 8.2 (build 32929)

python v3.8.1
pytest version 5.3.4 (Your code has been rated at 10.00/10)
pytest-pylint-0.14.1


https://docs.python.org/3/library/typing.html

Hi @vorona,

Thank you for reporting this issue and welcome to the community. I confirm that we still have this issue on the last version of the python analyzer (which we just released).

Just to add a little more context. The interesting part of PEP 484 is:

Forward references
When a type hint contains names that have not been defined yet, that definition may be expressed as a string literal, to be resolved later.

Type annotations should not be reported as duplicate strings. Type checkers will raise an issue if the type hint has a typo, and putting these strings in a variable doesn’t help readability.

I created the following ticket: https://jira.sonarsource.com/browse/SONARPY-665

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.