Language: Python
Rule #: S5890
Product: SonarLint - Pycharm 2023.1.4 (Professional Edition)
Step to reproduced:
testObjectA.py:
class ObjectA:
def __init__(self, name: str):
self.name = name
testObjectB.py:
class ObjectB:
def __init__(self, name: str):
self.name = name
testConvertToObjectB.py:
from testObjectB import ObjectB
from testObjectA import ObjectA
class ConvertToObjectB:
def __new__(cls, object: ObjectA) -> ObjectB:
result: ObjectB = ObjectB(name=object.name)
return result
test.py:
from typing import List
from testObjectB import ObjectB
from testObjectA import ObjectA
from testConvertToObjectB import ConvertToObjectB
class convert:
def __new__(cls) -> List[ObjectB]:
objectA: ObjectA = ObjectA(name='test')
result: List[ObjectB] = list()
newObject: ObjectB = ConvertToObjectB(object=objectA)
result.append(newObject)
return result
Sonarlint shows:
test.py:
(11,29) Assign to “newObject” a value of type “ObjectB” instead of “ConvertToObjectB” or update its type hint.
ConvertToObjectB.new return an ObjectB and therefore shouldn’t generate a warning.