Skip to content

Commit 7449e23

Browse files
SONARPY-780 ClassSymbol.canBeOrExtend('object') should be always true (SonarSource#833)
1 parent 937c12e commit 7449e23

5 files changed

Lines changed: 17 additions & 116 deletions

File tree

its/ruling/src/test/resources/expected/python-S5655.json

Lines changed: 0 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -4,116 +4,6 @@
44
1149,
55
1169,
66
],
7-
'project:mypy-0.782/mypy/options.py':[
8-
294,
9-
],
10-
'project:mypy-0.782/mypy/test/testgraph.py':[
11-
27,
12-
37,
13-
72,
14-
82,
15-
85,
16-
],
17-
'project:mypy-0.782/mypy/test/teststubgen.py':[
18-
47,
19-
49,
20-
49,
21-
67,
22-
69,
23-
69,
24-
106,
25-
110,
26-
172,
27-
182,
28-
187,
29-
190,
30-
195,
31-
200,
32-
204,
33-
205,
34-
207,
35-
210,
36-
213,
37-
216,
38-
220,
39-
224,
40-
228,
41-
234,
42-
240,
43-
244,
44-
248,
45-
251,
46-
254,
47-
258,
48-
263,
49-
271,
50-
275,
51-
280,
52-
288,
53-
611,
54-
614,
55-
618,
56-
623,
57-
627,
58-
634,
59-
634,
60-
646,
61-
646,
62-
647,
63-
647,
64-
657,
65-
657,
66-
658,
67-
658,
68-
671,
69-
671,
70-
672,
71-
672,
72-
684,
73-
684,
74-
685,
75-
685,
76-
699,
77-
699,
78-
700,
79-
700,
80-
714,
81-
714,
82-
715,
83-
715,
84-
730,
85-
730,
86-
731,
87-
731,
88-
748,
89-
748,
90-
749,
91-
749,
92-
762,
93-
762,
94-
763,
95-
763,
96-
778,
97-
778,
98-
779,
99-
779,
100-
798,
101-
798,
102-
805,
103-
],
104-
'project:mypy-0.782/mypy/test/testtypes.py':[
105-
407,
106-
407,
107-
420,
108-
423,
109-
446,
110-
446,
111-
449,
112-
449,
113-
],
114-
'project:mypy-0.782/mypy/treetransform.py':[
115-
130,
116-
],
1177
'project:mypy-0.782/test-data/stdlib-samples/3.2/pprint.py':[
1188
67,
1199
71,

its/ruling/src/test/resources/expected/python-S5886.json

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,6 @@
7575
3215,
7676
3217,
7777
],
78-
'project:mypy-0.782/mypy/options.py':[
79-
286,
80-
],
8178
'project:mypy-0.782/mypy/plugins/attrs.py':[
8279
411,
8380
413,
@@ -384,9 +381,6 @@
384381
390,
385382
753,
386383
],
387-
'project:mypy-0.782/test-data/stdlib-samples/3.2/tempfile.py':[
388-
105,
389-
],
390384
'project:mypy-0.782/test-data/stdlib-samples/3.2/test/test_random.py':[
391385
79,
392386
],

python-checks/src/test/resources/checks/argumentType.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ class SomeMock(Mock): ...
172172
my_mock = SomeMock()
173173
hex(my_mock) # OK
174174

175+
def exception_for_object():
176+
def foo(a: object): ...
177+
foo([])
178+
175179
class StaticCallInsideClass:
176180
def my_method(a: int, b: str): ...
177181
my_method(1, "hello") # OK

python-frontend/src/main/java/org/sonar/python/semantic/ClassSymbolImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,18 @@ public boolean isOrExtends(String fullyQualifiedClassName) {
203203

204204
@Override
205205
public boolean isOrExtends(ClassSymbol other) {
206+
if ("object".equals(other.fullyQualifiedName())) {
207+
return true;
208+
}
206209
// TODO there should be only 1 class with a given fullyQualifiedName when analyzing a python file
207210
return allSuperClasses(false).stream().anyMatch(c -> Objects.equals(c.fullyQualifiedName(), other.fullyQualifiedName()));
208211
}
209212

210213
@Override
211214
public boolean canBeOrExtend(String fullyQualifiedClassName) {
215+
if ("object".equals(fullyQualifiedClassName)) {
216+
return true;
217+
}
212218
return allSuperClasses(true).stream().anyMatch(c -> c.fullyQualifiedName() != null && Objects.equals(fullyQualifiedClassName, c.fullyQualifiedName()))
213219
|| hasUnresolvedTypeHierarchy();
214220
}

python-frontend/src/test/java/org/sonar/python/semantic/ClassSymbolImplTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.sonar.plugins.python.api.tree.FileInput;
3030
import org.sonar.plugins.python.api.tree.Tree;
3131
import org.sonar.python.PythonTestUtils;
32+
import org.sonar.python.types.TypeShed;
3233

3334
import static org.assertj.core.api.Assertions.assertThat;
3435
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@@ -156,6 +157,9 @@ public void isOrExtends() {
156157
assertThat(b.isOrExtends(a)).isFalse();
157158
ClassSymbolImpl c = new ClassSymbolImpl("c", "mod2.c");
158159
assertThat(a.isOrExtends(c)).isFalse();
160+
161+
assertThat(new ClassSymbolImpl("foo", "foo").isOrExtends(TypeShed.typeShedClass("object"))).isTrue();
162+
assertThat(a.isOrExtends(TypeShed.typeShedClass("object"))).isTrue();
159163
}
160164

161165
@Test
@@ -201,6 +205,9 @@ public void canBeOrExtend() {
201205
assertThat(d.canBeOrExtend("mod1.a")).isTrue();
202206
assertThat(d.canBeOrExtend("mod2.a")).isTrue();
203207
assertThat(d.canBeOrExtend("mod3.a")).isTrue();
208+
209+
assertThat(new ClassSymbolImpl("foo", "foo").canBeOrExtend("object")).isTrue();
210+
assertThat(a.canBeOrExtend("object")).isTrue();
204211
}
205212

206213
@Test

0 commit comments

Comments
 (0)