From 0829354ec820ab04a0bb966b607a12494ac8290f Mon Sep 17 00:00:00 2001 From: Sam Dudley Date: Thu, 28 Mar 2024 13:35:48 +0000 Subject: [PATCH] move from flake8 to ruff --- .github/workflows/ci.yml | 4 +- core/import_csv.py | 27 ++++--- core/management/commands/create_stub_data.py | 4 +- core/remove_users.py | 2 +- core/test/factories.py | 1 - data_lake/test/test_actual_split.py | 1 - download_file/test/test_views.py | 25 +++--- end_of_month/test/test_utils.py | 3 +- forecast/import_actuals.py | 6 +- forecast/import_budget_or_forecast.py | 21 +++-- .../management/commands/new_financial_year.py | 2 +- forecast/models.py | 38 ++++++--- forecast/tables.py | 18 ++++- forecast/test/test_download_actual_sum.py | 9 +-- forecast/test/test_download_edit_forecast.py | 10 +-- forecast/test/test_download_forecast.py | 9 +-- forecast/test/test_download_project_detail.py | 9 +-- forecast/test/test_download_reports.py | 18 ++--- .../test/test_edit_future_year_forecast.py | 20 ++--- .../test/test_import_forecast_from_csv.py | 2 +- forecast/test/test_utils.py | 12 +-- forecast/test/test_view_forecast_hierarchy.py | 9 +-- forecast/test/test_view_project_details.py | 9 +-- forecast/test/test_views.py | 45 +++++------ forecast/utils/import_helpers.py | 2 +- forecast/views/edit_forecast.py | 2 +- future_years/test/future_year_utils.py | 1 - .../test_future_year_mi_budget_download.py | 15 ++-- .../test_future_year_mi_forecast_download.py | 10 +-- .../import_nac_cash_income_fields.py | 4 +- importdata/tasks.py | 4 +- makefile | 6 +- .../commands/load_group_to_archive.py | 3 +- .../commands/load_segment_to_archive.py | 3 +- pii-ner-exclude.txt | 1 - poetry.lock | 77 +++++++------------ previous_years/import_previous_year.py | 7 +- .../commands/archive_current_year.py | 1 - .../commands/upload_previous_year.py | 1 - pyproject.toml | 20 ++++- setup.cfg | 19 ----- treasuryCOA/import_csv.py | 1 - treasurySS/import_csv.py | 2 - .../import_project_percentage.py | 6 +- upload_split_file/models.py | 7 +- upload_split_file/split_actuals.py | 2 +- 46 files changed, 244 insertions(+), 254 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 70aeaed57..92a8d118d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -34,8 +34,8 @@ jobs: - name: Collect static run: make collectstatic - - name: Run Flake8 - run: make flake8 + - name: Run ruff + run: make ruff - name: Run isort run: make isort-check diff --git a/core/import_csv.py b/core/import_csv.py index 3687f9b7a..a847ce285 100644 --- a/core/import_csv.py +++ b/core/import_csv.py @@ -60,10 +60,10 @@ def add_position(d, h): dictionary inside""" c = {} for k, v in d.items(): - if type(v) is dict: + if isinstance(v, dict): c[k] = add_position(v, h) else: - if type(v) is str: + if isinstance(v, str): v = v.lower() if v in h: c[k] = h[v] @@ -145,7 +145,7 @@ def read_csv_from_dict(d, row, year): default_list = {} for k, v in d[IMPORT_CSV_FIELDLIST_KEY].items(): - if type(v) is dict: + if isinstance(v, dict): default_list[k], errormsg = read_csv_from_dict(v, row, year) else: default_list[k] = get_value_from_field( @@ -185,8 +185,8 @@ def get_col_from_obj_key(obj_key): if IMPORT_CSV_IS_FK in obj_key: header_list.append(obj_key[IMPORT_CSV_IS_FK]) if IMPORT_CSV_FIELDLIST_KEY in obj_key: - for k, v in obj_key[IMPORT_CSV_FIELDLIST_KEY].items(): - if type(v) is dict: + for _, v in obj_key[IMPORT_CSV_FIELDLIST_KEY].items(): + if isinstance(v, dict): header_list = header_list + get_col_from_obj_key(v) else: header_list.append(v) @@ -254,13 +254,22 @@ class ImportInfo: def __init__( self, - key={}, + key=None, title="", - h_list=[], + h_list=None, special_import_func=None, - filter=[], + filter=None, extra_func=None, ): + if key is None: + key = {} + + if h_list is None: + h_list = [] + + if filter is None: + filter = [] + self.key = key self.special_func = special_import_func if bool(key): @@ -328,7 +337,7 @@ def get_field_name(obj_key, prefix): field_list.append(prefix + model._meta.pk.name) if IMPORT_CSV_FIELDLIST_KEY in obj_key: for k, v in obj_key[IMPORT_CSV_FIELDLIST_KEY].items(): - if type(v) is dict: + if isinstance(v, dict): field_list = field_list + get_field_name(v, prefix + k + "__") else: field_list.append(prefix + k) diff --git a/core/management/commands/create_stub_data.py b/core/management/commands/create_stub_data.py index ec046200e..b4197a267 100644 --- a/core/management/commands/create_stub_data.py +++ b/core/management/commands/create_stub_data.py @@ -29,7 +29,7 @@ def create_departmental_group( ) self.counter += 1 directorate_code_base = int(group_code[0:4]) * 10 - for i in range(howmany_directorate): + for _ in range(howmany_directorate): self.create_directorate( departmental_group, "{}A".format(directorate_code_base), @@ -47,7 +47,7 @@ def create_directorate( group=departmental_group, ) self.counter += 1 - for i in range(howmany_cost_centres): + for _ in range(howmany_cost_centres): CostCentre.objects.create( cost_centre_code=888810 + self.counter, active=True, diff --git a/core/remove_users.py b/core/remove_users.py index a261658ba..cde41a967 100644 --- a/core/remove_users.py +++ b/core/remove_users.py @@ -124,7 +124,7 @@ def bulk_delete_users(file_obj: FileUpload) -> int: else: # needed to avoid processing empty rows at the end of the file break - workbook.close + workbook.close() final_status = FileUpload.PROCESSED if error_found: diff --git a/core/test/factories.py b/core/test/factories.py index e63b83eec..e6a1796d6 100644 --- a/core/test/factories.py +++ b/core/test/factories.py @@ -1,5 +1,4 @@ import factory -import factory from django.contrib.auth import get_user_model from faker import Faker diff --git a/data_lake/test/test_actual_split.py b/data_lake/test/test_actual_split.py index 0fedd60da..ec0aacf65 100644 --- a/data_lake/test/test_actual_split.py +++ b/data_lake/test/test_actual_split.py @@ -51,7 +51,6 @@ def setUp(self): natural_account_code=nac_obj, project_code=project_obj, ) - self.financial_code_obj.save financial_period_queryset = FinancialPeriod.objects.filter( financial_period_code__lt=4 ) diff --git a/download_file/test/test_views.py b/download_file/test/test_views.py index 17851da12..23a6fbdae 100644 --- a/download_file/test/test_views.py +++ b/download_file/test/test_views.py @@ -49,15 +49,15 @@ def setUp(self): natural_account_code=nac_obj, project_code=project_obj, ) - financial_code_obj.save - apr_figure = ForecastMonthlyFigure.objects.create( + # apr figure + ForecastMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get(financial_period_code=1), financial_code=financial_code_obj, financial_year=year_obj, amount=self.amount_apr, ) - apr_figure.save - may_figure = ForecastMonthlyFigure.objects.create( + # may figure + ForecastMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get( financial_period_code=2, ), @@ -65,7 +65,7 @@ def setUp(self): financial_code=financial_code_obj, financial_year=year_obj, ) - may_figure.save + self.year_total = self.amount_apr + self.amount_may def test_download_mi_view(self): @@ -146,16 +146,15 @@ def setUp(self): natural_account_code=nac_obj, project_code=project_obj, ) - financial_code_obj.save - apr_figure = BudgetMonthlyFigure.objects.create( + # apr figure + BudgetMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get(financial_period_code=1), financial_code=financial_code_obj, financial_year=year_obj, amount=self.amount_apr, ) - apr_figure.save - - may_figure = BudgetMonthlyFigure.objects.create( + # may figure + BudgetMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get( financial_period_code=2, ), @@ -163,15 +162,14 @@ def setUp(self): financial_code=financial_code_obj, financial_year=year_obj, ) - may_figure.save financial_code_obj1 = FinancialCode.objects.create( programme=self.programme_obj, cost_centre=cost_centre, natural_account_code=nac_obj, ) - - may_figure1 = BudgetMonthlyFigure.objects.create( + # another may figure + BudgetMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get( financial_period_code=2, ), @@ -179,7 +177,6 @@ def setUp(self): financial_code=financial_code_obj1, financial_year=year_obj, ) - may_figure1.save self.year_total = self.amount_apr + self.amount_may diff --git a/end_of_month/test/test_utils.py b/end_of_month/test/test_utils.py index 0c11d0337..154897058 100644 --- a/end_of_month/test/test_utils.py +++ b/end_of_month/test/test_utils.py @@ -93,7 +93,6 @@ def __init__(self, year=0): natural_account_code=nac_obj, project_code=project_obj, ) - self.financial_code_obj.save if year == 0: year = get_current_financial_year() @@ -166,7 +165,7 @@ def __init__(self, last_archived_period=16, year=0): self.setup_forecast() self.setup_budget() # prepares the lists used to store the totals - for period in range(0, last_archived_period): + for _ in range(0, last_archived_period): self.archived_forecast.append(0) self.archived_budget.append(0) self.set_archive_period(last_archived_period) diff --git a/forecast/import_actuals.py b/forecast/import_actuals.py index ac1aa2a9c..e769cb111 100644 --- a/forecast/import_actuals.py +++ b/forecast/import_actuals.py @@ -212,6 +212,7 @@ def validate_trial_balance_report(file_upload, month_number, year): str(ex), str(ex), ) + workbook.close() raise ex try: @@ -222,8 +223,9 @@ def validate_trial_balance_report(file_upload, month_number, year): str(ex), str(ex), ) - workbook.close + workbook.close() raise ex + return workbook, worksheet @@ -284,7 +286,7 @@ def upload_trial_balance_report(file_upload, month_number, financial_year): else: # needed to avoid processing empty rows at the end of the file break - workbook.close + workbook.close() final_status = FileUpload.PROCESSED if check_financial_code.error_found: diff --git a/forecast/import_budget_or_forecast.py b/forecast/import_budget_or_forecast.py index a4696e67c..9c4fab934 100644 --- a/forecast/import_budget_or_forecast.py +++ b/forecast/import_budget_or_forecast.py @@ -84,7 +84,7 @@ def upload_figures(uploadmodel, data_row, year_obj, financialcode_obj, month_dic if period_figure is None: period_figure = 0 # We import from Excel, and the user may have entered spaces in an empty cell. - if type(period_figure) == str: + if isinstance(period_figure, str): period_figure = period_figure.strip() if period_figure == "-": # we accept the '-' as it is a recognised value in Finance for 0 @@ -99,7 +99,10 @@ def upload_figures(uploadmodel, data_row, year_obj, financialcode_obj, month_dic f"Non-numeric value in {data_row[month_idx].coordinate}:{period_figure}" # noqa ) if period_figure: - (figure_obj, created,) = uploadmodel.objects.get_or_create( + ( + figure_obj, + created, + ) = uploadmodel.objects.get_or_create( financial_year=year_obj, financial_code=financialcode_obj, financial_period=period_obj, @@ -215,6 +218,7 @@ def upload_figure_from_file(file_upload, year): title = "Budgets" else: title = "Forecasts" + try: workbook, worksheet = validate_excel_file(file_upload, title) except UploadFileFormatError as ex: @@ -223,8 +227,11 @@ def upload_figure_from_file(file_upload, year): str(ex), str(ex), ) + workbook.close() raise ex + header_dict = xslx_header_to_dict(worksheet[1]) + try: check_header(header_dict, EXPECTED_FIGURE_HEADERS) except UploadFileFormatError as ex: @@ -233,19 +240,21 @@ def upload_figure_from_file(file_upload, year): str(ex), str(ex), ) - workbook.close + workbook.close() raise ex + try: upload_financial_figures(worksheet, year, header_dict, file_upload) - except (UploadFileDataError) as ex: + except UploadFileDataError as ex: set_file_upload_fatal_error( file_upload, str(ex), str(ex), ) - workbook.close + workbook.close() raise ex - workbook.close + + workbook.close() def upload_budget_from_file(file_upload, year): diff --git a/forecast/management/commands/new_financial_year.py b/forecast/management/commands/new_financial_year.py index 9a3faf3cc..14e31c87e 100644 --- a/forecast/management/commands/new_financial_year.py +++ b/forecast/management/commands/new_financial_year.py @@ -23,7 +23,7 @@ def run_command(self, message, command_name, *arg, **options): ) self.stdout.write(self.style.ERROR(full_error_message)) raise CommandError(full_error_message) - return False + return True def handle_user(self, *args, **options): diff --git a/forecast/models.py b/forecast/models.py index 9084b810d..89180b7b1 100644 --- a/forecast/models.py +++ b/forecast/models.py @@ -273,9 +273,7 @@ def adj_periods(self): ) def reset_actuals(self): - self.get_queryset().filter(actual_loaded=True,).update( - actual_loaded=False, - ) + self.get_queryset().filter(actual_loaded=True).update(actual_loaded=False) def get_max_period(self): return self.get_queryset().order_by("-financial_period_code").first() @@ -574,9 +572,9 @@ def do_output_subtotal(self, current_row): if self.output_subtotal[column]: subtotal_row = self.subtotals[column].copy() level = self.subtotal_columns.index(column) - subtotal_row[ - self.display_total_column - ] = f"Total {self.previous_values[column]}" + subtotal_row[self.display_total_column] = ( + f"Total {self.previous_values[column]}" + ) show_class = TOTAL_CLASS for out_total in self.subtotal_columns[level + 1 :]: subtotal_row[self.display_total_column] = ( @@ -671,7 +669,7 @@ def calculate_subtotal_data( self.output_subtotal[column] = True if subtotal_time: self.do_output_subtotal(current_row) - for k, totals in self.subtotals.items(): + for _, totals in self.subtotals.items(): self.add_row_to_subtotal(current_row, totals) self.output_row_to_table(current_row, "") @@ -701,7 +699,13 @@ def calculate_subtotal_data( class PivotManager(models.Manager): """Managers returning the data in Monthly figures pivoted""" - def pivot_data(self, columns, filter_dict={}, year=0, order_list=[]): + def pivot_data(self, columns, filter_dict=None, year=0, order_list=None): + if filter_dict is None: + filter_dict = {} + + if order_list is None: + order_list = [] + if year == 0: year = get_current_financial_year() @@ -728,11 +732,17 @@ def subtotal_data( display_total_column, subtotal_columns, data_columns, - filter_dict={}, + filter_dict=None, year=0, - order_list=[], + order_list=None, show_grand_total=True, ): + if filter_dict is None: + filter_dict = {} + + if order_list is None: + order_list = [] + # If requesting a subtotal, the # list of columns must be specified if not subtotal_columns: @@ -768,7 +778,13 @@ def subtotal_data( show_grand_total, ) - def raw_data_annotated(self, columns, filter_dict={}, year=0, order_list=[]): + def raw_data_annotated(self, columns, filter_dict=None, year=0, order_list=None): + if filter_dict is None: + filter_dict = {} + + if order_list is None: + order_list = [] + annotations = { budget_field: Sum("budget"), "Apr": Sum("apr"), diff --git a/forecast/tables.py b/forecast/tables.py index 0296055ec..0cfd2c2f7 100644 --- a/forecast/tables.py +++ b/forecast/tables.py @@ -122,7 +122,10 @@ class ForecastSubTotalTable(tables.Table): # Display actual/forecast labels is_forecast = True - def __init__(self, column_dict={}, *args, **kwargs): + def __init__(self, column_dict=None, *args, **kwargs): + if column_dict is None: + column_dict = {} + cols = [(budget_field, tables.Column(budget_header, empty_values=()))] year_period_list = [] @@ -253,9 +256,9 @@ def __init__(self, column_dict={}, *args, **kwargs): self.forecast_cols = (12 + len(extra_column_to_display)) - self.num_actuals super().__init__( + *args, extra_columns=extra_column_to_display, sequence=column_list, - *args, **kwargs, ) # change the stile for columns showing "actuals". @@ -287,8 +290,17 @@ class ForecastWithLinkTable(ForecastSubTotalTable, tables.Table): display_view_details = True def __init__( - self, column_name, viewname, arg_link, code="", column_dict={}, *args, **kwargs + self, + column_name, + viewname, + arg_link, + code="", + column_dict=None, + *args, + **kwargs, ): + if column_dict is None: + column_dict = {} link_args = [] if code: diff --git a/forecast/test/test_download_actual_sum.py b/forecast/test/test_download_actual_sum.py index 6962f53ca..5b2e5c6fe 100644 --- a/forecast/test/test_download_actual_sum.py +++ b/forecast/test/test_download_actual_sum.py @@ -67,17 +67,17 @@ def setUp(self): natural_account_code=nac_obj, project_code=project_obj, ) - financial_code_obj.save self.amount_apr_current_year = 987654300 - apr_figure = ForecastMonthlyFigure.objects.create( + # apr figure + ForecastMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get(financial_period_code=1), financial_code=financial_code_obj, financial_year=year_obj, amount=self.amount_apr_current_year, ) - apr_figure.save self.amount_apr_next_year = 9898989800 - next_year_april_figures = ForecastMonthlyFigure.objects.create( + # next year april figures + ForecastMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get( financial_period_code=1, ), @@ -85,7 +85,6 @@ def setUp(self): financial_code=financial_code_obj, financial_year=next_year_obj, ) - next_year_april_figures.save # Assign forecast view permission can_view_forecasts = Permission.objects.get(codename="can_view_forecasts") self.test_user.user_permissions.add(can_view_forecasts) diff --git a/forecast/test/test_download_edit_forecast.py b/forecast/test/test_download_edit_forecast.py index 49b39b6d5..df680b6d1 100644 --- a/forecast/test/test_download_edit_forecast.py +++ b/forecast/test/test_download_edit_forecast.py @@ -69,16 +69,16 @@ def setUp(self): natural_account_code=self.nac_obj, project_code=self.project_obj, ) - financial_code_obj.save - apr_figure = ForecastMonthlyFigure.objects.create( + # apr figure + ForecastMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get(financial_period_code=1), financial_code=financial_code_obj, financial_year=year_obj, amount=self.amount_apr, ) - apr_figure.save self.amount_may = 1234567 - may_figure = ForecastMonthlyFigure.objects.create( + # may figure + ForecastMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get( financial_period_code=2, ), @@ -86,7 +86,6 @@ def setUp(self): financial_code=financial_code_obj, financial_year=year_obj, ) - may_figure.save # This will create a row with no figures. # Changed to create a record with value 0 @@ -100,7 +99,6 @@ def setUp(self): natural_account_code=self.nac_obj, project_code=project_obj1, ) - financial_code_obj1.save ForecastMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get(financial_period_code=1), financial_code=financial_code_obj1, diff --git a/forecast/test/test_download_forecast.py b/forecast/test/test_download_forecast.py index c1f17820b..641462f4d 100644 --- a/forecast/test/test_download_forecast.py +++ b/forecast/test/test_download_forecast.py @@ -63,16 +63,16 @@ def setUp(self): natural_account_code=nac_obj, project_code=self.project_obj, ) - financial_code_obj.save - apr_figure = ForecastMonthlyFigure.objects.create( + # apr figure + ForecastMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get(financial_period_code=1), financial_code=financial_code_obj, financial_year=year_obj, amount=self.amount_apr, ) - apr_figure.save self.amount_may = 1234567 - may_figure = ForecastMonthlyFigure.objects.create( + # may figure + ForecastMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get( financial_period_code=2, ), @@ -80,7 +80,6 @@ def setUp(self): financial_code=financial_code_obj, financial_year=year_obj, ) - may_figure.save # Assign forecast view permission can_view_forecasts = Permission.objects.get(codename="can_view_forecasts") self.test_user.user_permissions.add(can_view_forecasts) diff --git a/forecast/test/test_download_project_detail.py b/forecast/test/test_download_project_detail.py index 25d50d1b2..935810c52 100644 --- a/forecast/test/test_download_project_detail.py +++ b/forecast/test/test_download_project_detail.py @@ -62,16 +62,16 @@ def setUp(self): natural_account_code=nac_obj, project_code=project_obj, ) - financial_code_obj.save - apr_figure = ForecastMonthlyFigure.objects.create( + # apr figure + ForecastMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get(financial_period_code=1), financial_code=financial_code_obj, financial_year=year_obj, amount=self.amount_apr, ) - apr_figure.save self.amount_may = 1234567 - may_figure = ForecastMonthlyFigure.objects.create( + # may figure + ForecastMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get( financial_period_code=2, ), @@ -79,7 +79,6 @@ def setUp(self): financial_code=financial_code_obj, financial_year=year_obj, ) - may_figure.save # Assign forecast view permission can_view_forecasts = Permission.objects.get(codename="can_view_forecasts") diff --git a/forecast/test/test_download_reports.py b/forecast/test/test_download_reports.py index 6a7d0b3b9..804814e2a 100644 --- a/forecast/test/test_download_reports.py +++ b/forecast/test/test_download_reports.py @@ -48,16 +48,16 @@ def setUp(self): natural_account_code=nac_obj, project_code=project_obj, ) - financial_code_obj.save - apr_figure = ForecastMonthlyFigure.objects.create( + # apr figure + ForecastMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get(financial_period_code=1), financial_code=financial_code_obj, financial_year=year_obj, amount=self.amount_apr, ) - apr_figure.save self.amount_may = 1234567 - may_figure = ForecastMonthlyFigure.objects.create( + # may figure + ForecastMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get( financial_period_code=2, ), @@ -65,7 +65,6 @@ def setUp(self): financial_code=financial_code_obj, financial_year=year_obj, ) - may_figure.save # Assign download permission can_view_forecasts = Permission.objects.get(codename="can_download_mi_reports") self.test_user.user_permissions.add(can_view_forecasts) @@ -130,16 +129,16 @@ def setUp(self): natural_account_code=nac_obj, project_code=project_obj, ) - financial_code_obj.save - apr_figure = ForecastMonthlyFigure.objects.create( + # apr figure + ForecastMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get(financial_period_code=1), financial_code=financial_code_obj, financial_year=year_obj, amount=self.amount_apr, ) - apr_figure.save self.amount_may = 1234567 - may_figure = ForecastMonthlyFigure.objects.create( + # may figure + ForecastMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get( financial_period_code=2, ), @@ -147,7 +146,6 @@ def setUp(self): financial_code=financial_code_obj, financial_year=year_obj, ) - may_figure.save can_download_files = Permission.objects.get( codename="can_download_oscar", diff --git a/forecast/test/test_edit_future_year_forecast.py b/forecast/test/test_edit_future_year_forecast.py index c249a6045..713d0958d 100644 --- a/forecast/test/test_edit_future_year_forecast.py +++ b/forecast/test/test_edit_future_year_forecast.py @@ -59,32 +59,29 @@ def setUp(self): cost_centre=self.cost_centre, natural_account_code=NaturalCodeFactory.create(), ) - financial_code_obj.save self.current_financial_year = get_current_financial_year() - this_year_figure = ForecastMonthlyFigure.objects.create( + # this year figure + ForecastMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get(financial_period_code=1), financial_code=financial_code_obj, financial_year=get_financial_year_obj(self.current_financial_year), amount=self.current_year_amount, ) - this_year_figure.save - - next_year_figure = ForecastMonthlyFigure.objects.create( + # next year figure + ForecastMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get(financial_period_code=1), financial_code=financial_code_obj, financial_year=get_financial_year_obj(self.current_financial_year + 1), amount=self.next_year_amount, ) - next_year_figure.save - - next_next_year_figure = ForecastMonthlyFigure.objects.create( + # next next year figure + ForecastMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get(financial_period_code=1), financial_code=financial_code_obj, financial_year=get_financial_year_obj(self.current_financial_year + 2), amount=self.next_next_year_amount, ) - next_next_year_figure.save def test_correct_current_forecast(self): # Checks the 'Edit-Forecast tab' returns an 'OK' status code @@ -467,16 +464,15 @@ def setUp(self): cost_centre=self.cost_centre, natural_account_code=nac_obj, ) - financial_code_obj.save self.current_financial_year = get_current_financial_year() - this_year_figure = ForecastMonthlyFigure.objects.create( + # this year figure + ForecastMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get(financial_period_code=1), financial_code=financial_code_obj, financial_year=get_financial_year_obj(self.current_financial_year), amount=self.current_year_amount, ) - this_year_figure.save def test_empty_future_forecast(self): # Tests that there are no emtpy rows derived from forecasts data in other years diff --git a/forecast/test/test_import_forecast_from_csv.py b/forecast/test/test_import_forecast_from_csv.py index aca0e3156..1e1d06a02 100644 --- a/forecast/test/test_import_forecast_from_csv.py +++ b/forecast/test/test_import_forecast_from_csv.py @@ -40,7 +40,7 @@ def setUp(self): Analysis2Factory.create(analysis2_code=self.analisys2) self.year_obj = FinancialYear.objects.get(financial_year=2019) self.year_obj.current = True - self.year_obj.save + self.year_obj.save() def get_csv_data(self): header = ( diff --git a/forecast/test/test_utils.py b/forecast/test/test_utils.py index d286971e1..25b9efc99 100644 --- a/forecast/test/test_utils.py +++ b/forecast/test/test_utils.py @@ -17,14 +17,15 @@ def create_budget(financial_code_obj, year_obj): budget_july = 1234567 budget_total = budget_apr + budget_may + budget_july # Save several months, and check that the total is displayed - apr_budget = BudgetMonthlyFigure.objects.create( + # apr figure + BudgetMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get(financial_period_code=1), financial_code=financial_code_obj, financial_year=year_obj, amount=budget_apr, ) - apr_budget.save - may_budget = BudgetMonthlyFigure.objects.create( + # may budget + BudgetMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get( financial_period_code=2, ), @@ -32,8 +33,8 @@ def create_budget(financial_code_obj, year_obj): financial_code=financial_code_obj, financial_year=year_obj, ) - may_budget.save - july_budget = BudgetMonthlyFigure.objects.create( + # july budget + BudgetMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get( financial_period_code=4, ), @@ -41,7 +42,6 @@ def create_budget(financial_code_obj, year_obj): financial_code=financial_code_obj, financial_year=year_obj, ) - july_budget.save return budget_total diff --git a/forecast/test/test_view_forecast_hierarchy.py b/forecast/test/test_view_forecast_hierarchy.py index d5fa4b4a4..6965a98cf 100644 --- a/forecast/test/test_view_forecast_hierarchy.py +++ b/forecast/test/test_view_forecast_hierarchy.py @@ -71,16 +71,16 @@ def setUp(self): natural_account_code=nac_obj, project_code=self.project_obj, ) - financial_code_obj.save - apr_figure = ForecastMonthlyFigure.objects.create( + # apr figure + ForecastMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get(financial_period_code=1), financial_code=financial_code_obj, financial_year=year_obj, amount=self.amount_apr, ) - apr_figure.save self.amount_may = 1234567 - may_figure = ForecastMonthlyFigure.objects.create( + # may figure + ForecastMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get( financial_period_code=2, ), @@ -88,7 +88,6 @@ def setUp(self): financial_code=financial_code_obj, financial_year=year_obj, ) - may_figure.save # Assign forecast view permission can_view_forecasts = Permission.objects.get(codename="can_view_forecasts") self.test_user.user_permissions.add(can_view_forecasts) diff --git a/forecast/test/test_view_project_details.py b/forecast/test/test_view_project_details.py index 9f087682e..4aafb44fc 100644 --- a/forecast/test/test_view_project_details.py +++ b/forecast/test/test_view_project_details.py @@ -76,26 +76,25 @@ def setUp(self): natural_account_code=self.nac_obj, project_code=project_obj, ) - financial_code1_obj.save self.expenditure_type = ( financial_code1_obj.forecast_expenditure_type.forecast_expenditure_type_name ) - apr_figure = ForecastMonthlyFigure.objects.create( + # apr figure + ForecastMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get(financial_period_code=1), financial_code=financial_code1_obj, financial_year=year_obj, amount=self.amount_apr, ) - apr_figure.save self.amount_may = 1234567 - may_figure = ForecastMonthlyFigure.objects.create( + # may figure + ForecastMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get(financial_period_code=4), financial_code=financial_code1_obj, financial_year=year_obj, amount=self.amount_may, ) - may_figure.save # Assign forecast view permission can_view_forecasts = Permission.objects.get(codename="can_view_forecasts") diff --git a/forecast/test/test_views.py b/forecast/test/test_views.py index 9a016c884..9f98b5afd 100644 --- a/forecast/test/test_views.py +++ b/forecast/test/test_views.py @@ -141,37 +141,34 @@ def setUp(self): cost_centre=self.cost_centre, natural_account_code=self.nac1_obj, ) - financial_code1_obj.save financial_code2_obj = FinancialCode.objects.create( programme=programme_obj, cost_centre=self.cost_centre, natural_account_code=self.nac2_obj, ) - financial_code2_obj.save - apr_figure = ForecastMonthlyFigure.objects.create( + # apr figure + ForecastMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get(financial_period_code=1), financial_code=financial_code1_obj, financial_year=year_obj, amount=self.amount1_apr, ) - apr_figure.save - - apr_figure = ForecastMonthlyFigure.objects.create( + # another apr figure + ForecastMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get(financial_period_code=1), financial_code=financial_code2_obj, financial_year=year_obj, amount=self.amount2_apr, ) - apr_figure.save self.amount_may = 1234567 - may_figure = ForecastMonthlyFigure.objects.create( + # may figure + ForecastMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get(financial_period_code=4), financial_code=financial_code1_obj, financial_year=year_obj, amount=self.amount_may, ) - may_figure.save # Assign forecast view permission can_view_forecasts = Permission.objects.get(codename="can_view_forecasts") @@ -343,26 +340,25 @@ def setUp(self): cost_centre=self.cost_centre, natural_account_code=nac_obj, ) - financial_code_obj.save self.forecast_expenditure_type_id = ( financial_code_obj.forecast_expenditure_type.forecast_expenditure_type_name ) - apr_figure = ForecastMonthlyFigure.objects.create( + # apr figure + ForecastMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get(financial_period_code=1), amount=amount_apr, financial_code=financial_code_obj, financial_year=year_obj, ) - apr_figure.save self.amount_may = 1234567 - may_figure = ForecastMonthlyFigure.objects.create( + # may figure + ForecastMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get(financial_period_code=4), financial_code=financial_code_obj, financial_year=year_obj, amount=self.amount_may, ) - may_figure.save # Assign forecast view permission can_view_forecasts = Permission.objects.get(codename="can_view_forecasts") @@ -612,32 +608,31 @@ def setUp(self): natural_account_code=nac_obj, project_code=self.project_obj, ) - financial_code_obj.save + financial_code_obj1 = FinancialCode.objects.create( programme=self.programme_obj, cost_centre=self.cost_centre1, natural_account_code=nac_obj, project_code=self.project_obj, ) - financial_code_obj1.save - apr_figure = ForecastMonthlyFigure.objects.create( + # apr figure + ForecastMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get(financial_period_code=1), financial_code=financial_code_obj, financial_year=year_obj, amount=self.amount_apr, ) - apr_figure.save - - apr_figure = ForecastMonthlyFigure.objects.create( + # another apr figure + ForecastMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get(financial_period_code=1), financial_code=financial_code_obj1, financial_year=year_obj, amount=-self.amount_apr, ) - apr_figure.save self.amount_may = 891000 - may_figure = ForecastMonthlyFigure.objects.create( + # may figure + ForecastMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get( financial_period_code=2, ), @@ -645,9 +640,8 @@ def setUp(self): financial_code=financial_code_obj, financial_year=year_obj, ) - may_figure.save - - may_figure = ForecastMonthlyFigure.objects.create( + # another may figure + ForecastMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get( financial_period_code=2, ), @@ -655,7 +649,6 @@ def setUp(self): financial_code=financial_code_obj1, financial_year=year_obj, ) - may_figure.save # Assign forecast view permission can_view_forecasts = Permission.objects.get(codename="can_view_forecasts") diff --git a/forecast/utils/import_helpers.py b/forecast/utils/import_helpers.py index 52d03d3d8..1d3a4e734 100644 --- a/forecast/utils/import_helpers.py +++ b/forecast/utils/import_helpers.py @@ -395,7 +395,7 @@ def validate_project(self, project): return None def clean_data(self, data): - if type(data) == str: + if isinstance(data, str): return data.strip() else: return data diff --git a/forecast/views/edit_forecast.py b/forecast/views/edit_forecast.py index d7ca43848..96b84e9b6 100644 --- a/forecast/views/edit_forecast.py +++ b/forecast/views/edit_forecast.py @@ -547,4 +547,4 @@ class ErrorView( TemplateView, ): def dispatch(self, request, *args, **kwargs): - 1 / 0 + return 1 / 0 diff --git a/future_years/test/future_year_utils.py b/future_years/test/future_year_utils.py index a1e7cedcf..849a23ea9 100644 --- a/future_years/test/future_year_utils.py +++ b/future_years/test/future_year_utils.py @@ -85,7 +85,6 @@ def __init__(self, test_year): natural_account_code=nac_obj, project_code=project_obj, ) - self.financial_code_obj.save def setup_forecast(self, future: bool): value_dict = {} diff --git a/future_years/test/test_future_year_mi_budget_download.py b/future_years/test/test_future_year_mi_budget_download.py index 828527b93..922d7d002 100644 --- a/future_years/test/test_future_year_mi_budget_download.py +++ b/future_years/test/test_future_year_mi_budget_download.py @@ -40,16 +40,15 @@ def setUp(self): natural_account_code=nac_obj, project_code=project_obj, ) - financial_code_obj.save - apr_figure = BudgetMonthlyFigure.objects.create( + # apr figure + BudgetMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get(financial_period_code=1), financial_code=financial_code_obj, financial_year=year_obj, amount=self.amount_apr, ) - apr_figure.save - - may_figure = BudgetMonthlyFigure.objects.create( + # may figure + BudgetMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get( financial_period_code=2, ), @@ -57,15 +56,14 @@ def setUp(self): financial_code=financial_code_obj, financial_year=year_obj, ) - may_figure.save financial_code_obj1 = FinancialCode.objects.create( programme=self.programme_obj, cost_centre=cost_centre, natural_account_code=nac_obj, ) - - may_figure1 = BudgetMonthlyFigure.objects.create( + # another may figure + BudgetMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get( financial_period_code=2, ), @@ -73,7 +71,6 @@ def setUp(self): financial_code=financial_code_obj1, financial_year=year_obj, ) - may_figure1.save self.year_total = self.amount_apr + self.amount_may diff --git a/future_years/test/test_future_year_mi_forecast_download.py b/future_years/test/test_future_year_mi_forecast_download.py index d5b90f12d..175d56141 100644 --- a/future_years/test/test_future_year_mi_forecast_download.py +++ b/future_years/test/test_future_year_mi_forecast_download.py @@ -50,16 +50,16 @@ def setUp(self): natural_account_code=nac_obj, project_code=project_obj, ) - financial_code_obj.save - apr_figure = ForecastMonthlyFigure.objects.create( + # apr figure + ForecastMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get(financial_period_code=1), financial_code=financial_code_obj, financial_year=year_obj, amount=self.amount_apr, ) - apr_figure.save self.amount_may = 1234567 - may_figure = ForecastMonthlyFigure.objects.create( + # may figure + ForecastMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get( financial_period_code=2, ), @@ -67,7 +67,7 @@ def setUp(self): financial_code=financial_code_obj, financial_year=year_obj, ) - may_figure.save + # Assign download permission can_view_forecasts = Permission.objects.get(codename="can_download_mi_reports") self.test_user.user_permissions.add(can_view_forecasts) diff --git a/import_chart_of_account/import_nac_cash_income_fields.py b/import_chart_of_account/import_nac_cash_income_fields.py index eb6dab462..2341a1ac0 100644 --- a/import_chart_of_account/import_nac_cash_income_fields.py +++ b/import_chart_of_account/import_nac_cash_income_fields.py @@ -164,7 +164,7 @@ def upload_nac_fields(file_obj: FileUpload) -> int: # noqa C901 else: # needed to avoid processing empty rows at the end of the file break - workbook.close + workbook.close() if not error_found: # copy new fields to tables. Use direct sql for performance @@ -184,5 +184,5 @@ def upload_nac_fields(file_obj: FileUpload) -> int: # noqa C901 set_file_upload_feedback( file_obj, f"Processed {rows_to_process} rows.", final_status ) - file_obj.user_error_message + return final_message, success diff --git a/importdata/tasks.py b/importdata/tasks.py index 340068b96..77c934426 100644 --- a/importdata/tasks.py +++ b/importdata/tasks.py @@ -12,7 +12,7 @@ def import_task(requester, type, file, import_func): import_status=AsyncImportLog.STATUS_STARTED, imported_by=requester, ) - log.save + log.save() return True logid = log.id @@ -31,5 +31,5 @@ def import_task(requester, type, file, import_func): import_message=message, import_end=datetime.now(), ) - log.save + log.save() return result diff --git a/makefile b/makefile index 7f51ec3d4..607347055 100644 --- a/makefile +++ b/makefile @@ -92,11 +92,11 @@ isort-check: # Run isort-check isort: # Run isort $(web) isort . -flake8: # Run flake8 - $(web) flake8 $(file) +ruff: # Run ruff + $(web) ruff check check: # Run formatters to see if there are any errors - make flake8 + make ruff make black-check make isort-check diff --git a/oscar_return/management/commands/load_group_to_archive.py b/oscar_return/management/commands/load_group_to_archive.py index 8cee16de0..ac04c02cf 100644 --- a/oscar_return/management/commands/load_group_to_archive.py +++ b/oscar_return/management/commands/load_group_to_archive.py @@ -29,10 +29,9 @@ def handle(self, *args, **options): import_segment_group(csvfile, year) except (ArchiveYearError, WrongHeaderException) as ex: raise CommandError(f"Failure uploading forecast period: {str(ex)}") + finally: csvfile.close() - return - csvfile.close() if self.upload_s3: os.remove(file_name) self.stdout.write( diff --git a/oscar_return/management/commands/load_segment_to_archive.py b/oscar_return/management/commands/load_segment_to_archive.py index 182dbc727..3cd8b2c57 100644 --- a/oscar_return/management/commands/load_segment_to_archive.py +++ b/oscar_return/management/commands/load_segment_to_archive.py @@ -33,10 +33,9 @@ def handle(self, *args, **options): raise CommandError( f"Failure uploading historical Treasury segment data: {ex}" ) + finally: csvfile.close() - return - csvfile.close() if self.upload_s3: os.remove(file_name) self.stdout.write( diff --git a/pii-ner-exclude.txt b/pii-ner-exclude.txt index 21814a110..0f57ca037 100644 --- a/pii-ner-exclude.txt +++ b/pii-ner-exclude.txt @@ -40,7 +40,6 @@ Control Budget Detail Code Row Sort Order Row Long Name PESA Regional Code -Flake8 Segment Grand Parent Code Segment Grand Parent Long Name Segment Long Name diff --git a/poetry.lock b/poetry.lock index 734e00a6d..34e428ba5 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1057,22 +1057,6 @@ files = [ [package.dependencies] python-dateutil = ">=2.4" -[[package]] -name = "flake8" -version = "7.0.0" -description = "the modular source code checker: pep8 pyflakes and co" -optional = false -python-versions = ">=3.8.1" -files = [ - {file = "flake8-7.0.0-py2.py3-none-any.whl", hash = "sha256:a6dfbb75e03252917f2473ea9653f7cd799c3064e54d4c8140044c5c065f53c3"}, - {file = "flake8-7.0.0.tar.gz", hash = "sha256:33f96621059e65eec474169085dc92bf26e7b2d47366b70be2f67ab80dc25132"}, -] - -[package.dependencies] -mccabe = ">=0.7.0,<0.8.0" -pycodestyle = ">=2.11.0,<2.12.0" -pyflakes = ">=3.2.0,<3.3.0" - [[package]] name = "freezegun" version = "1.4.0" @@ -1378,17 +1362,6 @@ docs = ["alabaster (==0.7.12)", "autodocsumm (==0.2.9)", "sphinx (==5.3.0)", "sp lint = ["flake8 (==5.0.4)", "flake8-bugbear (==22.10.25)", "mypy (==0.990)", "pre-commit (>=2.4,<3.0)"] tests = ["pytest", "pytz", "simplejson"] -[[package]] -name = "mccabe" -version = "0.7.0" -description = "McCabe checker, plugin for flake8" -optional = false -python-versions = ">=3.6" -files = [ - {file = "mccabe-0.7.0-py2.py3-none-any.whl", hash = "sha256:6c2d30ab6be0e4a46919781807b4f0d834ebdd6c6e3dca0bda5a15f863427b6e"}, - {file = "mccabe-0.7.0.tar.gz", hash = "sha256:348e0240c33b60bbdf4e523192ef919f28cb2c3d7d5c7794f74009290f236325"}, -] - [[package]] name = "mohawk" version = "1.1.0" @@ -1600,17 +1573,6 @@ files = [ {file = "psycopg2-2.9.9.tar.gz", hash = "sha256:d1454bde93fb1e224166811694d600e746430c006fbb031ea06ecc2ea41bf156"}, ] -[[package]] -name = "pycodestyle" -version = "2.11.1" -description = "Python style guide checker" -optional = false -python-versions = ">=3.8" -files = [ - {file = "pycodestyle-2.11.1-py2.py3-none-any.whl", hash = "sha256:44fe31000b2d866f2e41841b18528a505fbd7fef9017b04eff4e2648a0fadc67"}, - {file = "pycodestyle-2.11.1.tar.gz", hash = "sha256:41ba0e7afc9752dfb53ced5489e89f8186be00e599e712660695b7a75ff2663f"}, -] - [[package]] name = "pycparser" version = "2.21" @@ -1622,17 +1584,6 @@ files = [ {file = "pycparser-2.21.tar.gz", hash = "sha256:e644fdec12f7872f86c58ff790da456218b10f863970249516d60a5eaca77206"}, ] -[[package]] -name = "pyflakes" -version = "3.2.0" -description = "passive checker of Python programs" -optional = false -python-versions = ">=3.8" -files = [ - {file = "pyflakes-3.2.0-py2.py3-none-any.whl", hash = "sha256:84b5be138a2dfbb40689ca07e2152deb896a65c3a3e24c251c5c62489568074a"}, - {file = "pyflakes-3.2.0.tar.gz", hash = "sha256:1c61603ff154621fb2a9172037d84dca3500def8c8b630657d1701f026f8af3f"}, -] - [[package]] name = "pyperclip" version = "1.8.2" @@ -1877,6 +1828,32 @@ requests = ">=2.0.0" [package.extras] rsa = ["oauthlib[signedtoken] (>=3.0.0)"] +[[package]] +name = "ruff" +version = "0.3.4" +description = "An extremely fast Python linter and code formatter, written in Rust." +optional = false +python-versions = ">=3.7" +files = [ + {file = "ruff-0.3.4-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl", hash = "sha256:60c870a7d46efcbc8385d27ec07fe534ac32f3b251e4fc44b3cbfd9e09609ef4"}, + {file = "ruff-0.3.4-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:6fc14fa742e1d8f24910e1fff0bd5e26d395b0e0e04cc1b15c7c5e5fe5b4af91"}, + {file = "ruff-0.3.4-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:d3ee7880f653cc03749a3bfea720cf2a192e4f884925b0cf7eecce82f0ce5854"}, + {file = "ruff-0.3.4-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:cf133dd744f2470b347f602452a88e70dadfbe0fcfb5fd46e093d55da65f82f7"}, + {file = "ruff-0.3.4-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:3f3860057590e810c7ffea75669bdc6927bfd91e29b4baa9258fd48b540a4365"}, + {file = "ruff-0.3.4-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:986f2377f7cf12efac1f515fc1a5b753c000ed1e0a6de96747cdf2da20a1b369"}, + {file = "ruff-0.3.4-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:c4fd98e85869603e65f554fdc5cddf0712e352fe6e61d29d5a6fe087ec82b76c"}, + {file = "ruff-0.3.4-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:64abeed785dad51801b423fa51840b1764b35d6c461ea8caef9cf9e5e5ab34d9"}, + {file = "ruff-0.3.4-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:df52972138318bc7546d92348a1ee58449bc3f9eaf0db278906eb511889c4b50"}, + {file = "ruff-0.3.4-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:98e98300056445ba2cc27d0b325fd044dc17fcc38e4e4d2c7711585bd0a958ed"}, + {file = "ruff-0.3.4-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:519cf6a0ebed244dce1dc8aecd3dc99add7a2ee15bb68cf19588bb5bf58e0488"}, + {file = "ruff-0.3.4-py3-none-musllinux_1_2_i686.whl", hash = "sha256:bb0acfb921030d00070539c038cd24bb1df73a2981e9f55942514af8b17be94e"}, + {file = "ruff-0.3.4-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:cf187a7e7098233d0d0c71175375c5162f880126c4c716fa28a8ac418dcf3378"}, + {file = "ruff-0.3.4-py3-none-win32.whl", hash = "sha256:af27ac187c0a331e8ef91d84bf1c3c6a5dea97e912a7560ac0cef25c526a4102"}, + {file = "ruff-0.3.4-py3-none-win_amd64.whl", hash = "sha256:de0d5069b165e5a32b3c6ffbb81c350b1e3d3483347196ffdf86dc0ef9e37dd6"}, + {file = "ruff-0.3.4-py3-none-win_arm64.whl", hash = "sha256:6810563cc08ad0096b57c717bd78aeac888a1bfd38654d9113cb3dc4d3f74232"}, + {file = "ruff-0.3.4.tar.gz", hash = "sha256:f0f4484c6541a99862b693e13a151435a279b271cff20e37101116a21e2a1ad1"}, +] + [[package]] name = "s3transfer" version = "0.10.1" @@ -2387,4 +2364,4 @@ testing = ["coverage (>=5.0.3)", "zope.event", "zope.testing"] [metadata] lock-version = "2.0" python-versions = "^3.10" -content-hash = "6322ca1c51018b9ce3770add2d8c95d59bfb76ae723b41d1f3239bc01d5ef32f" +content-hash = "52107abf19e1997eec32c3efb7d1d91c1d4f13d90937fedfa1622c62a916199a" diff --git a/previous_years/import_previous_year.py b/previous_years/import_previous_year.py index d81bdf805..97e869ca2 100644 --- a/previous_years/import_previous_year.py +++ b/previous_years/import_previous_year.py @@ -313,7 +313,9 @@ def upload_previous_year_from_file(file_upload, year): str(ex), str(ex), ) + workbook.close() raise ex + try: upload_previous_year(worksheet, year, file_upload) except (UploadFileDataError, ArchiveYearError) as ex: @@ -322,6 +324,7 @@ def upload_previous_year_from_file(file_upload, year): str(ex), str(ex), ) - workbook.close + workbook.close() raise ex - workbook.close + + workbook.close() diff --git a/previous_years/management/commands/archive_current_year.py b/previous_years/management/commands/archive_current_year.py index 9f99d4b53..12e1e64e2 100644 --- a/previous_years/management/commands/archive_current_year.py +++ b/previous_years/management/commands/archive_current_year.py @@ -21,6 +21,5 @@ def handle(self, *args, **options): ArchiveYearError, ) as ex: raise CommandError(f"Failure archiving forecast/actual figures: {str(ex)}") - return self.stdout.write(self.style.SUCCESS("Current financial year archived. ")) diff --git a/previous_years/management/commands/upload_previous_year.py b/previous_years/management/commands/upload_previous_year.py index 158648a39..95d1c7326 100644 --- a/previous_years/management/commands/upload_previous_year.py +++ b/previous_years/management/commands/upload_previous_year.py @@ -40,7 +40,6 @@ def handle(self, *args, **options): ArchiveYearError, ) as ex: raise CommandError(f"Failure uploading historical actuals: {str(ex)}") - return if self.upload_s3: os.remove(file_name) diff --git a/pyproject.toml b/pyproject.toml index 41c011af4..14ed2b7eb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -70,7 +70,7 @@ pytest-random-order = "^1.1.1" pyperclip = "^1.8.0" freezegun = "^1.0.0" isort = "^5.10.1" -flake8 = "^7.0.0" +ruff = "^0.3.4" [build-system] requires = ["poetry-core"] @@ -84,8 +84,7 @@ line_length = 88 lines_after_imports = 2 [tool.black] -# Exclude files with # noqa directive for flake8. They have long lines, -# and black will move the flake8 directive to a new line, and flake8 will complain. +target-version = ["py310", "py311", "py312"] exclude = ''' ( \.direnv @@ -104,3 +103,18 @@ exclude = ''' | upload_split_file/downloads.py ) ''' + +[tool.ruff] +# Same as Black. +line-length = 88 +indent-width = 4 +# Target python 3.10. +target-version = "py310" +# In addition to the standard set of exclusions, omit all tests, plus a specific file. +extend-exclude = ["config/*", "features/*", "migrations", "manage.py"] + +[tool.ruff.lint] +# Enable flake8-bugbear (`B`) rules. +select = ["E", "F", "B"] +# Never enforce `E501` (line length violations). +ignore = ["E501", "B904"] diff --git a/setup.cfg b/setup.cfg index 40c10d5f9..a17d5d650 100644 --- a/setup.cfg +++ b/setup.cfg @@ -6,22 +6,3 @@ DJANGO_SETTINGS_MODULE = config.settings.dev exclude = */migrations/*,__pycache__,manage.py,config/*, ignore = W503 max-line-length = 99 - - -[flake8] -# D100: Missing docstring in public module -# D104: Missing docstring in public package -# D106: Missing docstring in public nested class -# D200: One-line docstring should fit on one line with quotes -# D203: 1 blank line required before class docstring -# D205: 1 blank line required between summary line and description -# D400: First line should end with a period -# D401: First line should be in imperative mood -# W503: line break occurred before a binary operator (not recommended in PEP 8) -# E203: E203 whitespace before ':' (not recommended in PEP 8) -exclude = */migrations/*,__pycache__,manage.py,config/*,env/*,lib/*,bin/*,include/*,.direnv/*,venv/*,./front_end,./node_modules,./src,./features,./fidovenv -ignore = D100,D104,D106,D200,D203,D205,D400,D401,W503 -max-line-length = 88 -max-complexity = 10 -extend-ignore = E203 -application-import-names = chartofaccountDIT,config,core,costcentre,end_of_month,forecast,gifthospitality,importdata,oscar_return,previous_years,treasuryCOA,treasurySS,upload_file diff --git a/treasuryCOA/import_csv.py b/treasuryCOA/import_csv.py index 648ebb8c0..64121b767 100644 --- a/treasuryCOA/import_csv.py +++ b/treasuryCOA/import_csv.py @@ -52,7 +52,6 @@ # 'Cash Indicator Code': 38 # } -# noqa: E501 tells Flake8 to ignore lines that are to long L1_KEY = { IMPORT_CSV_MODEL_KEY: L1Account, IMPORT_CSV_PK_KEY: "Account L1 Code", diff --git a/treasurySS/import_csv.py b/treasurySS/import_csv.py index 7815de2b2..04a2d2327 100644 --- a/treasurySS/import_csv.py +++ b/treasurySS/import_csv.py @@ -48,8 +48,6 @@ # PESA Services Code # PESA Regional Code - -# noqa: E501 tells Flake8 to ignore lines that are to long SEGMENT_GP_KEY = { IMPORT_CSV_MODEL_KEY: SegmentGrandParent, IMPORT_CSV_PK_KEY: "Segment Grand Parent Code", diff --git a/upload_split_file/import_project_percentage.py b/upload_split_file/import_project_percentage.py index 1fdbe3580..88c799755 100644 --- a/upload_split_file/import_project_percentage.py +++ b/upload_split_file/import_project_percentage.py @@ -139,7 +139,7 @@ def copy_uploaded_percentage(self): def get_valid_percentage_value(self, period_percentage): # We import from Excel, and the user # may have entered spaces in an empty cell. - if type(period_percentage) == str: + if isinstance(period_percentage, str): period_percentage = period_percentage.strip() if period_percentage == "-": # we accept the '-' as it is a recognised value in Finance for 0 @@ -306,7 +306,7 @@ def upload_project_percentage_from_file(worksheet, file_upload, include_archived upload.copy_uploaded_percentage() upload.apply_percentages() - except (UploadFileDataError) as ex: + except UploadFileDataError as ex: set_file_upload_fatal_error( file_upload, str(ex), @@ -326,4 +326,4 @@ def upload_project_percentage(file_upload, include_archived=False): ) return upload_project_percentage_from_file(worksheet, file_upload, include_archived) - workbook.close + workbook.close() diff --git a/upload_split_file/models.py b/upload_split_file/models.py index a2c44ff8d..2242c17d2 100644 --- a/upload_split_file/models.py +++ b/upload_split_file/models.py @@ -58,7 +58,12 @@ class Meta: class PivotManager(models.Manager): """Managers returning the data in Monthly figures pivoted""" - def pivot_data(self, columns, filter_dict={}, order_list=[]): + def pivot_data(self, columns, filter_dict=None, order_list=None): + if filter_dict is None: + filter_dict = {} + + if order_list is None: + order_list = [] q1 = self.get_queryset().filter(**filter_dict).order_by(*order_list) pivot_data = pivot( diff --git a/upload_split_file/split_actuals.py b/upload_split_file/split_actuals.py index 7a8d99a4b..e1542174c 100644 --- a/upload_split_file/split_actuals.py +++ b/upload_split_file/split_actuals.py @@ -157,7 +157,7 @@ def handle_split_project_by_directorate( if rounding: # allocate to the last processed row the difference transferred_to_obj.calculated_amount += rounding - transferred_to_obj.save + transferred_to_obj.save() copy_values(financial_period_id, directorate_code, expenditure_code_list)