From 1821e00955cf379c8e39c4f6e0e0fb61d2158ee8 Mon Sep 17 00:00:00 2001 From: Sigurd Borge Date: Tue, 3 Dec 2024 14:21:22 +0100 Subject: [PATCH] ci(lint): forbid wrong typing inside the CI (#24) --- .github/workflows/ci.yml | 4 +- .../service/api_services/renewable_api.py | 9 +-- src/antares/service/base_services.py | 4 +- .../service/local_services/area_local.py | 4 +- .../service/local_services/renewable_local.py | 22 ++--- src/antares/tools/matrix_tool.py | 10 ++- .../services/local_services/test_area.py | 80 ++++++++++--------- tox.ini | 10 ++- 8 files changed, 77 insertions(+), 66 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 855f10ab..81f5cf2c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -29,7 +29,9 @@ jobs: - name: Performs Ubuntu tests if: matrix.os != 'windows-latest' - run: tox -p + run: | + tox -e lint-ci + tox -p - name: Performs Windows tests if: matrix.os == 'windows-latest' diff --git a/src/antares/service/api_services/renewable_api.py b/src/antares/service/api_services/renewable_api.py index 5ad85fba..234a823a 100644 --- a/src/antares/service/api_services/renewable_api.py +++ b/src/antares/service/api_services/renewable_api.py @@ -53,14 +53,7 @@ def update_renewable_properties( def get_renewable_matrix(self, cluster_id: str, area_id: str) -> pd.DataFrame: try: - path = ( - PurePosixPath("input") - / "renewables" - / "series" - / f"{area_id}" - / f"{cluster_id}" - / "series" - ) + path = PurePosixPath("input") / "renewables" / "series" / f"{area_id}" / f"{cluster_id}" / "series" return get_matrix(f"{self._base_url}/studies/{self.study_id}/raw?path={path}", self._wrapper) except APIError as e: raise RenewableMatrixDownloadError(area_id, cluster_id, e.message) from e diff --git a/src/antares/service/base_services.py b/src/antares/service/base_services.py index c552828c..da89201c 100644 --- a/src/antares/service/base_services.py +++ b/src/antares/service/base_services.py @@ -521,9 +521,7 @@ def update_renewable_properties( pass @abstractmethod - def get_renewable_matrix( - self, cluster_id: str, area_id: str - ) -> pd.DataFrame: + def get_renewable_matrix(self, cluster_id: str, area_id: str) -> pd.DataFrame: """ Args: cluster_id: renewable cluster id to retrieve matrix diff --git a/src/antares/service/local_services/area_local.py b/src/antares/service/local_services/area_local.py index 22484f05..3ea986d1 100644 --- a/src/antares/service/local_services/area_local.py +++ b/src/antares/service/local_services/area_local.py @@ -320,7 +320,7 @@ def get_load_matrix(self, area: Area) -> pd.DataFrame: return read_timeseries(TimeSeriesFileType.LOAD, self.config.study_path, area_id=area.id) def get_solar_matrix(self, area: Area) -> pd.DataFrame: - return read_timeseries(TimeSeriesFileType.SOLAR, self.config.study_path, area_id=area.id) + return read_timeseries(TimeSeriesFileType.SOLAR, self.config.study_path, area_id=area.id) def get_wind_matrix(self, area: Area) -> pd.DataFrame: return read_timeseries(TimeSeriesFileType.WIND, self.config.study_path, area_id=area.id) @@ -330,7 +330,7 @@ def get_reserves_matrix(self, area: Area) -> pd.DataFrame: def get_misc_gen_matrix(self, area: Area) -> pd.DataFrame: return read_timeseries(TimeSeriesFileType.MISC_GEN, self.config.study_path, area_id=area.id) - + def read_areas(self) -> List[Area]: local_path = self.config.local_path areas_path = local_path / self.study_name / "input" / "areas" diff --git a/src/antares/service/local_services/renewable_local.py b/src/antares/service/local_services/renewable_local.py index 9e8b3001..2b92a678 100644 --- a/src/antares/service/local_services/renewable_local.py +++ b/src/antares/service/local_services/renewable_local.py @@ -33,15 +33,11 @@ def update_renewable_properties( self, renewable_cluster: RenewableCluster, properties: RenewableClusterProperties ) -> RenewableClusterProperties: raise NotImplementedError - - - def get_renewable_matrix( - self, - cluster_id: str, - area_id: str - ) -> pd.DataFrame: - return read_timeseries(TimeSeriesFileType.RENEWABLE_DATA_SERIES, self.config.study_path, area_id=area_id, cluster_id=cluster_id) + def get_renewable_matrix(self, cluster_id: str, area_id: str) -> pd.DataFrame: + return read_timeseries( + TimeSeriesFileType.RENEWABLE_DATA_SERIES, self.config.study_path, area_id=area_id, cluster_id=cluster_id + ) def read_renewables(self, area_id: str) -> List[RenewableCluster]: renewable_dict = IniFile(self.config.study_path, IniFileTypes.RENEWABLES_LIST_INI, area_name=area_id).ini_dict @@ -56,6 +52,12 @@ def read_renewables(self, area_id: str) -> List[RenewableCluster]: nominal_capacity=renewable_dict[renewable_cluster]["nominalcapacity"], ts_interpretation=renewable_dict[renewable_cluster]["ts-interpretation"], ) - renewables_clusters.append(RenewableCluster(renewable_service=self, area_id=area_id, name=renewable_dict[renewable_cluster]["name"], properties=renewable_properties.yield_renewable_cluster_properties())) + renewables_clusters.append( + RenewableCluster( + renewable_service=self, + area_id=area_id, + name=renewable_dict[renewable_cluster]["name"], + properties=renewable_properties.yield_renewable_cluster_properties(), + ) + ) return renewables_clusters - \ No newline at end of file diff --git a/src/antares/tools/matrix_tool.py b/src/antares/tools/matrix_tool.py index 634a4504..abc864a4 100644 --- a/src/antares/tools/matrix_tool.py +++ b/src/antares/tools/matrix_tool.py @@ -42,7 +42,13 @@ def df_read(path: Path) -> pd.DataFrame: return pd.read_csv(path, sep="\t", header=None) -def read_timeseries(ts_file_type: TimeSeriesFileType, study_path: Path, area_id: Optional[str] = None, constraint_id: Optional[str] = None, cluster_id: Optional[str] = None,) -> pd.DataFrame: +def read_timeseries( + ts_file_type: TimeSeriesFileType, + study_path: Path, + area_id: Optional[str] = None, + constraint_id: Optional[str] = None, + cluster_id: Optional[str] = None, +) -> pd.DataFrame: file_path = study_path / ( ts_file_type.value if not (area_id or constraint_id or cluster_id) @@ -53,4 +59,4 @@ def read_timeseries(ts_file_type: TimeSeriesFileType, study_path: Path, area_id: else: _time_series = pd.DataFrame() - return _time_series \ No newline at end of file + return _time_series diff --git a/tests/antares/services/local_services/test_area.py b/tests/antares/services/local_services/test_area.py index ea321bee..af32cf9e 100644 --- a/tests/antares/services/local_services/test_area.py +++ b/tests/antares/services/local_services/test_area.py @@ -1122,13 +1122,13 @@ def _write_file(_file_path, _time_series) -> None: _file_path.parent.mkdir(parents=True, exist_ok=True) _time_series.to_csv(_file_path, sep="\t", header=False, index=False, encoding="utf-8") + class TestReadLoad: def test_read_load_local(self, local_study_w_areas): study_path = local_study_w_areas.service.config.study_path local_study_object = read_study_local(study_path) areas = local_study_object.read_areas() - for area in areas: expected_time_serie = pd.DataFrame( [ @@ -1158,15 +1158,14 @@ def test_read_renewable_local(self, local_study_with_renewable): local_study_object = read_study_local(study_path) areas = local_study_object.read_areas() - - for area in areas: expected_time_serie = pd.DataFrame( - [ - [-9999999980506447872, 0, 9999999980506447872], - [0, area.id, 0], - ] - , dtype="object") + [ + [-9999999980506447872, 0, 9999999980506447872], + [0, area.id, 0], + ], + dtype="object", + ) renewable_list = area.read_renewables(area.id) if renewable_list: @@ -1183,9 +1182,9 @@ def test_read_renewable_local(self, local_study_with_renewable): assert renewable.properties.group.value == "Other RES 1" # Create folder and file for timeserie. - cluster_path = study_path / "input" / "renewables" / "series"/ Path(area.id) / Path(renewable.id) + cluster_path = study_path / "input" / "renewables" / "series" / Path(area.id) / Path(renewable.id) os.makedirs(cluster_path, exist_ok=True) - series_path = cluster_path / 'series.txt' + series_path = cluster_path / "series.txt" _write_file(series_path, expected_time_serie) # Check matrix @@ -1199,14 +1198,14 @@ def test_read_solar_local(self, local_study_w_areas): local_study_object = read_study_local(study_path) areas = local_study_object.read_areas() - for area in areas: expected_time_serie = pd.DataFrame( - [ - [-9999999980506447872, 0, 9999999980506447872], - [0, area.id, 0], - ] - , dtype="object") + [ + [-9999999980506447872, 0, 9999999980506447872], + [0, area.id, 0], + ], + dtype="object", + ) file_path = study_path / "input" / "solar" / "series" / f"solar_{area.id}.txt" _write_file(file_path, expected_time_serie) @@ -1221,22 +1220,23 @@ def test_read_solar_local(self, local_study_w_areas): matrix = area.get_solar_matrix() pd.testing.assert_frame_equal(matrix, expected_time_serie) + class TestReadReserves: def test_read_reserve_local(self, local_study_w_areas): study_path = local_study_w_areas.service.config.study_path local_study_object = read_study_local(study_path) areas = local_study_object.read_areas() - for area in areas: expected_time_serie = pd.DataFrame( - [ - [-9999999980506447872, 0, 9999999980506447872], - [0, area.id, 0], - ] - , dtype="object") + [ + [-9999999980506447872, 0, 9999999980506447872], + [0, area.id, 0], + ], + dtype="object", + ) - file_path = study_path / "input" / "reserves" / f"{area.id}.txt" + file_path = study_path / "input" / "reserves" / f"{area.id}.txt" _write_file(file_path, expected_time_serie) matrix = area.get_reserves_matrix() @@ -1244,26 +1244,27 @@ def test_read_reserve_local(self, local_study_w_areas): expected_time_serie = pd.DataFrame([]) for area in areas: - file_path = study_path / "input" / "reserves" / f"{area.id}.txt" + file_path = study_path / "input" / "reserves" / f"{area.id}.txt" _write_file(file_path, expected_time_serie) matrix = area.get_reserves_matrix() pd.testing.assert_frame_equal(matrix, expected_time_serie) + class TestReadWind: def test_read_wind_local(self, local_study_w_areas): study_path = local_study_w_areas.service.config.study_path local_study_object = read_study_local(study_path) areas = local_study_object.read_areas() - for area in areas: expected_time_serie = pd.DataFrame( - [ - [-9999999980506447872, 0, 9999999980506447872], - [0, area.id, 0], - ] - , dtype="object") - + [ + [-9999999980506447872, 0, 9999999980506447872], + [0, area.id, 0], + ], + dtype="object", + ) + file_path = study_path / "input" / "wind" / "series" / f"wind_{area.id}.txt" _write_file(file_path, expected_time_serie) @@ -1277,21 +1278,22 @@ def test_read_wind_local(self, local_study_w_areas): matrix = area.get_wind_matrix() pd.testing.assert_frame_equal(matrix, expected_time_serie) + class TestReadmisc_gen: def test_read_misc_gen_local(self, local_study_w_areas): study_path = local_study_w_areas.service.config.study_path local_study_object = read_study_local(study_path) areas = local_study_object.read_areas() - for area in areas: expected_time_serie = pd.DataFrame( - [ - [-9999999980506447872, 0, 9999999980506447872], - [0, area.id, 0], - ] - , dtype="object") - + [ + [-9999999980506447872, 0, 9999999980506447872], + [0, area.id, 0], + ], + dtype="object", + ) + file_path = study_path / "input" / "misc-gen" / f"miscgen-{area.id}.txt" _write_file(file_path, expected_time_serie) @@ -1303,4 +1305,4 @@ def test_read_misc_gen_local(self, local_study_w_areas): file_path = study_path / "input" / "misc-gen" / f"miscgen-{area.id}.txt" _write_file(file_path, expected_time_serie) matrix = area.get_misc_gen_matrix() - pd.testing.assert_frame_equal(matrix, expected_time_serie) \ No newline at end of file + pd.testing.assert_frame_equal(matrix, expected_time_serie) diff --git a/tox.ini b/tox.ini index dd5616bb..6ee5c7ed 100644 --- a/tox.ini +++ b/tox.ini @@ -1,7 +1,6 @@ [tox] env_list = py3.{9,10,12}-test - lint [testenv] deps = @@ -27,3 +26,12 @@ commands = ruff format src/ tests/ {posargs} mypy {posargs} +[testenv:lint-ci] +description = Linting and formatting with ruff, typing with mypy +skip_install = True +commands = + python scripts/license_checker_and_adder.py --path=src --action=check-strict + python scripts/license_checker_and_adder.py --path=tests --action=check-strict + ruff check src/ tests/ {posargs} + ruff format --check src/ tests/ {posargs} + mypy {posargs}