Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SONARPY-2360 Add unit test case on S5655 when compared types has decorators #2173

Merged
merged 3 commits into from
Nov 21, 2024

Conversation

maksim-grebeniuk-sonarsource
Copy link
Contributor

@maksim-grebeniuk-sonarsource maksim-grebeniuk-sonarsource commented Nov 21, 2024

Copy link
Contributor

@thomas-serre-sonarsource thomas-serre-sonarsource left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! I left a minor comment to improve the readability of the test.

I don't approve it yet as I'm unsure that the ticket is really matching what you have implementented. Looking at the ticket, it says that the problem is coming from any decorated class. However looking at the test, it seems to me the problem is more specifically dataclasses.

I suggest either enriching the test with other examples of classes with decorators that cause FPs, or updating the tickets (2360 & 2359) to focus on dataclasses specifically

Comment on lines 270 to 282

def fp_decorated_types():
import dataclasses

@dataclasses.dataclass
class DecoratedClass:
x : int

def decorated_type_expecting_foo(data: DecoratedClass): ...

data = dataclasses.replace(...)
# FP SONARPY-2359
decorated_type_expecting_foo(data) # Noncompliant
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor a small readability improvement

Suggested change
def fp_decorated_types():
import dataclasses
@dataclasses.dataclass
class DecoratedClass:
x : int
def decorated_type_expecting_foo(data: DecoratedClass): ...
data = dataclasses.replace(...)
# FP SONARPY-2359
decorated_type_expecting_foo(data) # Noncompliant
import dataclasses
@dataclasses.dataclass
class DecoratedClass:
x : int
def decorated_type_expecting_foo(data: DecoratedClass): ...
data = dataclasses.replace(DecoratedClass(42))
# FP SONARPY-2359
decorated_type_expecting_foo(data) # Noncompliant

Copy link
Contributor

@thomas-serre-sonarsource thomas-serre-sonarsource left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed on another channel, the FP exists for any decorated class. I suggest updating the test to show it more explicitly. Though, I'm not able to propose a working test 🤔

I have added many comments about renaming, feel free to ignore them if you think there are worse than the current naming.

@@ -267,3 +267,15 @@ class EnumA(Enum):
C = 3

len(EnumA)

def fp_decorated_types():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor as suggested in a previous comment, I really think you can get rid of this line. I was able to raise the FP without this def function

class DecoratedClass:
x : int

def decorated_type_expecting_foo(data: DecoratedClass): ...
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def decorated_type_expecting_foo(data: DecoratedClass): ...
def decorated_class_expected(data: DecoratedClass): ...

def fp_decorated_types():
import dataclasses
@dataclasses.dataclass
class DecoratedClass:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class DecoratedClass:
class DecoratedDataClass:

class DecoratedClass:
x : int

def decorated_type_expecting_foo(data: DecoratedClass): ...
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def decorated_type_expecting_foo(data: DecoratedClass): ...
def decorated_type_expecting_foo(data: DecoratedDataClass): ...

x : int

def decorated_type_expecting_foo(data: DecoratedClass): ...
data = dataclasses.replace(DecoratedClass(42))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
data = dataclasses.replace(DecoratedClass(42))
data = dataclasses.replace(DecoratedDataClass(42))

data = dataclasses.replace(DecoratedClass(42))

# FP SONARPY-2359
decorated_type_expecting_foo(data) # Noncompliant
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
decorated_type_expecting_foo(data) # Noncompliant
decorated_dataclass_expected(data) # Noncompliant

@@ -267,3 +267,15 @@ class EnumA(Enum):
C = 3

len(EnumA)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be more explicit about the scope of the ticket, I suggest to add a more general test case.
I was going to propose this test but I 'm not able to raise the issue with it. Any idea why?

class AnotherClass:
  x: int
  y: float

@decorator
class DecoratedClass:
  x : int

def decorated_class_expected(data: DecoratedClass): ...

data = AnotherClass()
# FP SONARPY-2359
decorated_class_expected(data)  # Noncompliant

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cause in this example it compares AnotherClass to be compatible with DecoratedClass and since it has member x as DecoratedClass has - it says that it is compatible type

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, thanks

Copy link

Quality Gate passed Quality Gate passed

Issues
0 New issues
0 Fixed issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarQube

Copy link
Contributor

@thomas-serre-sonarsource thomas-serre-sonarsource left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM !

data = dataclasses.replace(DecoratedDataClass(42))

# FP SONARPY-2359
decorated_dataclass_expected(data) # Noncompliant
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
decorated_dataclass_expected(data) # Noncompliant
decorated_dataclass_expected(data) # Noncompliant

@maksim-grebeniuk-sonarsource maksim-grebeniuk-sonarsource merged commit 677e9a8 into master Nov 21, 2024
10 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants