Skip to content

Commit

Permalink
SONARPY-2315 Add unit test case on S5890 when annotated type is impor…
Browse files Browse the repository at this point in the history
…ted and has field members only (#2143)
  • Loading branch information
maksim-grebeniuk-sonarsource authored Nov 8, 2024
1 parent 02ac99b commit d8b1171
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,19 @@
*/
package org.sonar.python.checks;

import java.util.List;
import org.junit.jupiter.api.Test;
import org.sonar.python.checks.utils.PythonCheckVerifier;

class InconsistentTypeHintCheckTest {

@Test
void test() {
PythonCheckVerifier.verify("src/test/resources/checks/inconsistentTypeHint.py", new InconsistentTypeHintCheck());
PythonCheckVerifier.verify(
List.of(
"src/test/resources/checks/inconsistentTypeHintImported.py",
"src/test/resources/checks/inconsistentTypeHint.py"
),
new InconsistentTypeHintCheck());
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import SupportsFloat, List, Iterable, Generator, Set, Union, Type, TypedDict
from inconsistentTypeHintImported import ClassWithFieldOnly, ClassWithMethodOnly

def assigned_directly():
foo : int = None # Noncompliant {{Replace the type hint "int" with "Optional[int]" or don't assign "None" to "foo"}}
Expand All @@ -8,6 +9,8 @@ def assigned_directly():
my_str_ok: str = 42 # Noncompliant
my_int_ok: int = 42 # OK
my_str_ok: str = "hello" # OK
a : ClassWithFieldOnly = None # FN
b : ClassWithMethodOnly = None # Noncompliant

def return_union() -> Union[str, float]:
...
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ClassWithFieldOnly:
v : int = 10

class ClassWithMethodOnly:
def foo(self): ...

0 comments on commit d8b1171

Please sign in to comment.