Skip to content

Commit 57bad1f

Browse files
authored
Cursor-experiment (#2019)
* Fix poor usage of comprehensions * Relpace any type hint by Any * Add Optional type hint where needed * Add type hints to class attributes * Fix import * Add AI generated tests * Formatting * Change types using any by Any * MOre type hints * Remove trailing white spaces * Fix possible None dereference * Refactoring * reload instead of update sq_json * Reload sq_json instead of update * Imporved to copy only the right credentials in the test/gen directory * Kill TD * Add mor eissues deliberately * Put reference issue sin credentials * Fix is_xxx in MQR mode * Fix set_type to raise unsupported op * Small refactoring * Added type hints for kwargs * Fix test_refresh() * Formatting
1 parent d72b02e commit 57bad1f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+1161
-311
lines changed

cli/audit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def write_json(queue: Queue[list[problem.Problem]], fd: TextIO, settings: types.
109109

110110

111111
def _audit_sq(
112-
sq: platform.Platform, settings: types.ConfigSettings, what_to_audit: Optional[list[str]] = None, key_list: types.KeyList = None
112+
sq: platform.Platform, settings: types.ConfigSettings, what_to_audit: Optional[list[str]] = None, key_list: Optional[types.KeyList] = None
113113
) -> list[problem.Problem]:
114114
"""Audits a SonarQube/Cloud platform"""
115115
everything = what_to_audit is None

cli/options.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import sys
2828
import random
2929
from argparse import ArgumentParser
30+
from typing import Optional
3031

3132
import sonar.logging as log
3233
from sonar import errcodes, version, utilities, exceptions
@@ -186,7 +187,7 @@ def __check_file_writeable(file: str) -> None:
186187
os.remove(file)
187188

188189

189-
def parse_and_check(parser: ArgumentParser, logger_name: str = None, verify_token: bool = True, is_migration: bool = False) -> object:
190+
def parse_and_check(parser: ArgumentParser, logger_name: Optional[str] = None, verify_token: bool = True, is_migration: bool = False) -> object:
190191
"""Parses arguments, applies default settings and perform common environment checks"""
191192
try:
192193
args = parser.parse_args()
@@ -408,7 +409,7 @@ def set_target_sonar_args(parser: ArgumentParser) -> ArgumentParser:
408409
return parser
409410

410411

411-
def set_output_file_args(parser: ArgumentParser, help_str: str = None, allowed_formats: tuple[str, ...] = ("csv",)) -> ArgumentParser:
412+
def set_output_file_args(parser: ArgumentParser, help_str: Optional[str] = None, allowed_formats: tuple[str, ...] = ("csv",)) -> ArgumentParser:
412413
"""Sets the output file CLI options"""
413414
if not help_str:
414415
help_str = "Report file, stdout by default"

cli/projects_cli.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ def __import_projects(endpoint: platform.Platform, **kwargs) -> None:
107107
if proj["key"] in statuses:
108108
proj.update(statuses[proj["key"]])
109109
else:
110-
_ = [proj.pop(k, None) for k in ("importStatus", "importDate", "importProjectUrl")]
110+
for k in ("importStatus", "importDate", "importProjectUrl"):
111+
proj.pop(k, None)
111112
data["importSonarqubeEnvironment"] = {
112113
"url": endpoint.url(),
113114
"version": ".".join([str(n) for n in endpoint.version()[:2]]),

conf/build_tests.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ do
4343
b=$(basename "${f}" .py)
4444
cp "${f}" "${ROOT_DIR}/${GEN_LOC}/${target}/${b}_${target}.py"
4545
done
46+
rm "${ROOT_DIR}/${GEN_LOC}/${target}"/credentials*.py
4647
cp "credentials-${target}.py" "${ROOT_DIR}/${GEN_LOC}/${target}/credentials.py"
4748
mv "${ROOT_DIR}/${GEN_LOC}/${target}/conftest_${target}.py" "${ROOT_DIR}/${GEN_LOC}/${target}/conftest.py"
4849
mv "${ROOT_DIR}/${GEN_LOC}/${target}/utilities_${target}.py" "${ROOT_DIR}/${GEN_LOC}/${target}/utilities.py"

sonar/aggregations.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ class Aggregation(comp.Component):
4040
"""Parent class of applications and portfolios"""
4141

4242
def __init__(self, endpoint: pf.Platform, key: str, data: types.ApiPayload = None) -> None:
43-
self._nbr_projects = None
44-
self._permissions = None
43+
self._nbr_projects: Optional[int] = None
44+
self._permissions: Optional[object] = None
4545
super().__init__(endpoint=endpoint, key=key)
4646

4747
def reload(self, data: dict[str, any]) -> None:

sonar/app_branches.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
from __future__ import annotations
2424
from typing import Optional
25+
from datetime import datetime
2526

2627
import json
2728
from requests.utils import quote
@@ -62,7 +63,7 @@ def __init__(
6263
self.sq_json = branch_data
6364
self._is_main = is_main
6465
self._project_branches = project_branches
65-
self._last_analysis = None
66+
self._last_analysis: Optional[datetime] = None
6667
log.debug("Created object %s with uuid %d id %x", str(self), hash(self), id(self))
6768
ApplicationBranch.CACHE.put(self)
6869

sonar/applications.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ class Application(aggr.Aggregation):
7171
def __init__(self, endpoint: pf.Platform, key: str, name: str) -> None:
7272
"""Don't use this directly, go through the class methods to create Objects"""
7373
super().__init__(endpoint=endpoint, key=key)
74-
self._branches = None
75-
self._projects = None
76-
self._description = None
74+
self._branches: Optional[dict[str, app_branches.ApplicationBranch]] = None
75+
self._projects: Optional[dict[str, str]] = None
76+
self._description: Optional[str] = None
7777
self.name = name
7878
log.debug("Created object %s with uuid %d id %x", str(self), hash(self), id(self))
7979
Application.CACHE.put(self)
@@ -90,7 +90,7 @@ def get_object(cls, endpoint: pf.Platform, key: str) -> Application:
9090
:rtype: Application
9191
"""
9292
check_supported(endpoint)
93-
o = Application.CACHE.get(key, endpoint.local_url)
93+
o: Application = Application.CACHE.get(key, endpoint.local_url)
9494
if o:
9595
return o
9696
data = json.loads(endpoint.get(Application.API[c.GET], params={"application": key}).text)["application"]
@@ -109,7 +109,7 @@ def load(cls, endpoint: pf.Platform, data: types.ApiPayload) -> Application:
109109
:rtype: Application
110110
"""
111111
check_supported(endpoint)
112-
o = Application.CACHE.get(data["key"], endpoint.local_url)
112+
o: Application = Application.CACHE.get(data["key"], endpoint.local_url)
113113
if not o:
114114
o = cls(endpoint, data["key"], data["name"])
115115
o.reload(data)
@@ -259,7 +259,7 @@ def delete(self) -> bool:
259259
branch.delete()
260260
return super().delete()
261261

262-
def get_hotspots(self, filters: dict[str, str] = None) -> dict[str, object]:
262+
def get_hotspots(self, filters: Optional[dict[str, str]] = None) -> dict[str, object]:
263263
new_filters = filters.copy() if filters else {}
264264
pattern = new_filters.pop("branch", None) if new_filters else None
265265
if not pattern:
@@ -270,7 +270,7 @@ def get_hotspots(self, filters: dict[str, str] = None) -> dict[str, object]:
270270
findings_list |= comp.get_hotspots(new_filters)
271271
return findings_list
272272

273-
def get_issues(self, filters: dict[str, str] = None) -> dict[str, object]:
273+
def get_issues(self, filters: Optional[dict[str, str]] = None) -> dict[str, object]:
274274
new_filters = filters.copy() if filters else {}
275275
pattern = new_filters.pop("branch", None) if new_filters else None
276276
if not pattern:
@@ -412,7 +412,8 @@ def update(self, data: types.ObjectJsonRepr) -> None:
412412
main_branch_name = next((k for k, v in data.get("branches", {}).items() if v.get("isMain", False)), None)
413413
main_branch_name is None or self.main_branch().rename(main_branch_name)
414414

415-
_ = [self.set_branches(name, branch_data) for name, branch_data in data.get("branches", {}).items()]
415+
for name, branch_data in data.get("branches", {}).items():
416+
self.set_branches(name, branch_data)
416417

417418
def api_params(self, op: Optional[str] = None) -> types.ApiParams:
418419
ops = {c.READ: {"application": self.key}, c.RECOMPUTE: {"key": self.key}}

sonar/audit/problem.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#
2020

2121
import csv
22+
from typing import Optional
2223

2324
import sonar.logging as log
2425
from sonar import utilities
@@ -60,7 +61,9 @@ def to_json(self, with_url=False):
6061
return d
6162

6263

63-
def dump_report(problems: list[Problem], file: str, server_id: str = None, format: str = "csv", with_url: bool = False, separator: str = ",") -> None:
64+
def dump_report(
65+
problems: list[Problem], file: str, server_id: Optional[str] = None, format: str = "csv", with_url: bool = False, separator: str = ","
66+
) -> None:
6467
"""Dumps to file a report about a list of problems
6568
6669
:param list[Problems] problems: List of problems to dump
@@ -76,7 +79,7 @@ def dump_report(problems: list[Problem], file: str, server_id: str = None, forma
7679
__dump_csv(problems=problems, file=file, server_id=server_id, with_url=with_url, separator=separator)
7780

7881

79-
def __dump_csv(problems: list[Problem], file: str, server_id: str = None, with_url: bool = False, separator: str = ",") -> None:
82+
def __dump_csv(problems: list[Problem], file: str, server_id: Optional[str] = None, with_url: bool = False, separator: str = ",") -> None:
8083
"""Writes a list of problems in CSV format
8184
8285
:param list[Problems] problems: List of problems to dump
@@ -98,7 +101,7 @@ def __dump_csv(problems: list[Problem], file: str, server_id: str = None, with_u
98101
csvwriter.writerow(data)
99102

100103

101-
def __dump_json(problems: list[Problem], file: str, server_id: str = None, with_url: bool = False) -> None:
104+
def __dump_json(problems: list[Problem], file: str, server_id: Optional[str] = None, with_url: bool = False) -> None:
102105
"""Writes a list of problems in JSON format
103106
104107
:param list[Problems] problems: List of problems to dump

sonar/branches.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from __future__ import annotations
2424
from http import HTTPStatus
2525
from typing import Optional
26+
from datetime import datetime
2627
import json
2728
import re
2829
from urllib.parse import unquote
@@ -67,10 +68,10 @@ def __init__(self, project: projects.Project, name: str) -> None:
6768
super().__init__(endpoint=project.endpoint, key=name)
6869
self.name = name
6970
self.concerned_object = project
70-
self._is_main = None
71-
self._new_code = None
72-
self._last_analysis = None
73-
self._keep_when_inactive = None
71+
self._is_main: Optional[bool] = None
72+
self._new_code: Optional[str] = None
73+
self._last_analysis: Optional[datetime] = None
74+
self._keep_when_inactive: Optional[bool] = None
7475
Branch.CACHE.put(self)
7576
log.debug("Created object %s", str(self))
7677

sonar/changelog.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def __init__(self, jsonlog: types.ApiPayload, concerned_object: object) -> None:
3636
"""
3737
self.concerned_object = concerned_object
3838
self.sq_json = jsonlog
39-
self._change_type = None
39+
self._change_type: Optional[str] = None
4040

4141
def __str__(self) -> str:
4242
"""str() implementation"""

0 commit comments

Comments
 (0)