Skip to content

Commit

Permalink
chore: update linter to ruff (#558)
Browse files Browse the repository at this point in the history
  • Loading branch information
vncsna authored Feb 4, 2024
1 parent bc627af commit 764c6c9
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 242 deletions.
13 changes: 2 additions & 11 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,8 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up python
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
architecture: ${{ matrix.arch }}
- name: Set up poetry and upgrade pip
run: pip install -U pip poetry
- name: Install this package
run: poetry install
- name: Lint source code
run: poetry run lint
uses: chartboost/ruff-action@v1
- name: Lint pull request title
uses: amannn/action-semantic-pull-request@v5
env:
Expand All @@ -50,8 +41,8 @@ jobs:
- name: Set up python
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
architecture: ${{ matrix.arch }}
python-version: ${{ matrix.python-version }}
- name: Set up poetry and upgrade pip
run: pip install -U pip poetry
- name: Install this package
Expand Down
20 changes: 5 additions & 15 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,9 @@ repos:
rev: v1.29.0
hooks:
- id: yamllint
- repo: https://github.com/psf/black
rev: 22.3.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.2.0
hooks:
- id: black
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
- repo: https://github.com/PyCQA/autoflake
rev: v2.2.0
hooks:
- id: autoflake
- repo: https://github.com/PyCQA/flake8
rev: 4.0.1
hooks:
- id: flake8
- id: ruff
args: [--fix]
- id: ruff-format
26 changes: 18 additions & 8 deletions bd_api/apps/api/v1/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ def get_formset(self, request, obj=None, **kwargs):
def formfield_for_foreignkey(self, db_field, request, **kwargs):
"""Limit the observation level queryset to the parent object"""
if db_field.name == "observation_level":
kwargs["queryset"] = ObservationLevel.objects.filter(table=self.parent_inline_obj)
kwargs["queryset"] = ObservationLevel.objects.filter(
table=self.parent_inline_obj
)
return super().formfield_for_foreignkey(db_field, request, **kwargs)


Expand Down Expand Up @@ -236,7 +238,9 @@ class UpdateInline(admin.StackedInline):
################################################################################


def update_table_metadata(modeladmin: ModelAdmin, request: HttpRequest, queryset: QuerySet):
def update_table_metadata(
modeladmin: ModelAdmin, request: HttpRequest, queryset: QuerySet
):
"""Update the metadata of selected tables in the admin"""
if str(modeladmin) == "v1.TableAdmin":
tables = queryset.all()
Expand Down Expand Up @@ -451,10 +455,12 @@ class DatasetAdmin(OrderedInlineModelAdminMixin, TabbedTranslationAdmin):

def related_objects(self, obj):
return format_html(
"<a class='related-widget-wrapper-link add-related' href='/admin/v1/table/add/?dataset={0}&_to_field=id&_popup=1'>{1} {2}</a>", # noqa pylint: disable=line-too-long
"<a class='related-widget-wrapper-link add-related' href='/admin/v1/table/add/?dataset={0}&_to_field=id&_popup=1'>{1} {2}</a>",
obj.id,
obj.tables.count(),
" ".join(["tables" if obj.tables.count() > 1 else "table", "(click to add)"]),
" ".join(
["tables" if obj.tables.count() > 1 else "table", "(click to add)"]
),
)

related_objects.short_description = "Tables"
Expand Down Expand Up @@ -548,7 +554,9 @@ def changeform_view(self, request, object_id=None, form_url="", extra_context=No
{
"id": observation.id,
"entity": observation.entity.name if observation.entity else "",
"columns": "".join([column.name for column in observation.columns.all()]),
"columns": "".join(
[column.name for column in observation.columns.all()]
),
}
)

Expand All @@ -572,7 +580,9 @@ def related_columns(self, obj):
"<a href='/admin/v1/column/add/?table={0}'>{1} {2}</a>",
obj.id,
obj.columns.count(),
" ".join(["columns" if obj.columns.count() > 1 else "column", "(click to add)"]),
" ".join(
["columns" if obj.columns.count() > 1 else "column", "(click to add)"]
),
)

related_columns.short_description = "Columns"
Expand All @@ -582,11 +592,11 @@ def related_coverages(self, obj):
lines = []
for datetimerange in qs:
lines.append(
'<a href="/admin/api/v1/datetimerange/{0}/change/" target="_blank">Date Time Range</a>', # noqa pylint: disable=line-too-long
'<a href="/admin/api/v1/datetimerange/{0}/change/" target="_blank">Date Time Range</a>',
datetimerange.pk,
)
return format_html(
'<a href="/admin/api/v1/datetimerange/{}/change/" target="_blank">Date Time Range</a>', # noqa pylint: disable=line-too-long
'<a href="/admin/api/v1/datetimerange/{}/change/" target="_blank">Date Time Range</a>',
obj.datetimerange.slug,
)

Expand Down
95 changes: 69 additions & 26 deletions bd_api/apps/api/v1/models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
# -*- coding: utf-8 -*-
# pylint: disable=too-few-public-methods,too-many-lines
"""
Models for API v1
"""
import calendar
import json
import os
Expand Down Expand Up @@ -277,7 +273,9 @@ class Key(BaseModel):
"""

id = models.UUIDField(primary_key=True, default=uuid4)
dictionary = models.ForeignKey("Dictionary", on_delete=models.CASCADE, related_name="keys")
dictionary = models.ForeignKey(
"Dictionary", on_delete=models.CASCADE, related_name="keys"
)
name = models.CharField(max_length=255)
value = models.CharField(max_length=255)

Expand Down Expand Up @@ -436,7 +434,9 @@ class Organization(BaseModel):
slug = models.SlugField(unique=False, max_length=255)
name = models.CharField(max_length=255)
description = models.TextField(blank=True, null=True)
area = models.ForeignKey("Area", on_delete=models.CASCADE, related_name="organizations")
area = models.ForeignKey(
"Area", on_delete=models.CASCADE, related_name="organizations"
)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
website = models.URLField(blank=True, null=True, max_length=255)
Expand Down Expand Up @@ -604,8 +604,12 @@ def coverage(self):
continue
date_time = get_date_time(date_times)

start_year = date_time.start_year if date_time.start_year else start_year
start_month = date_time.start_month if date_time.start_month else start_month
start_year = (
date_time.start_year if date_time.start_year else start_year
)
start_month = (
date_time.start_month if date_time.start_month else start_month
)
start_day = date_time.start_day if date_time.start_day else start_day
end_year = date_time.end_year if date_time.end_year else end_year
end_month = date_time.end_month if date_time.end_month else end_month
Expand All @@ -616,7 +620,9 @@ def coverage(self):
date_time.start_month or 1,
date_time.start_day or 1,
)
start_date = new_start_date if new_start_date < start_date else start_date
start_date = (
new_start_date if new_start_date < start_date else start_date
)
new_end_date = datetime(
date_time.end_year or 1,
date_time.end_month or 1,
Expand All @@ -631,8 +637,12 @@ def coverage(self):
continue
date_time = get_date_time(date_times)

start_year = date_time.start_year if date_time.start_year else start_year
start_month = date_time.start_month if date_time.start_month else start_month
start_year = (
date_time.start_year if date_time.start_year else start_year
)
start_month = (
date_time.start_month if date_time.start_month else start_month
)
start_day = date_time.start_day if date_time.start_day else start_day
end_year = date_time.end_year if date_time.end_year else end_year
end_month = date_time.end_month if date_time.end_month else end_month
Expand All @@ -643,7 +653,9 @@ def coverage(self):
date_time.start_month or 1,
date_time.start_day or 1,
)
start_date = new_start_date if new_start_date < start_date else start_date
start_date = (
new_start_date if new_start_date < start_date else start_date
)
new_end_date = datetime(
date_time.end_year or 1,
date_time.end_month or 1,
Expand All @@ -658,8 +670,12 @@ def coverage(self):
continue
date_time = get_date_time(date_times)

start_year = date_time.start_year if date_time.start_year else start_year
start_month = date_time.start_month if date_time.start_month else start_month
start_year = (
date_time.start_year if date_time.start_year else start_year
)
start_month = (
date_time.start_month if date_time.start_month else start_month
)
start_day = date_time.start_day if date_time.start_day else start_day
end_year = date_time.end_year if date_time.end_year else end_year
end_month = date_time.end_month if date_time.end_month else end_month
Expand All @@ -670,7 +686,9 @@ def coverage(self):
date_time.start_month or 1,
date_time.start_day or 1,
)
start_date = new_start_date if new_start_date < start_date else start_date
start_date = (
new_start_date if new_start_date < start_date else start_date
)
new_end_date = datetime(
date_time.end_year or 1,
date_time.end_month or 1,
Expand Down Expand Up @@ -828,7 +846,9 @@ class Update(BaseModel):
"""

id = models.UUIDField(primary_key=True, default=uuid4)
entity = models.ForeignKey("Entity", on_delete=models.CASCADE, related_name="updates")
entity = models.ForeignKey(
"Entity", on_delete=models.CASCADE, related_name="updates"
)
frequency = models.IntegerField()
lag = models.IntegerField(blank=True, null=True)
latest = models.DateTimeField(blank=True, null=True)
Expand Down Expand Up @@ -882,7 +902,9 @@ def clean(self) -> None:
)

if self.entity.category.slug != "datetime":
raise ValidationError("Entity's category is not in category.slug = `datetime`.")
raise ValidationError(
"Entity's category is not in category.slug = `datetime`."
)

return super().clean()

Expand All @@ -894,7 +916,9 @@ class Table(BaseModel, OrderedModel):
slug = models.SlugField(unique=False, max_length=255)
name = models.CharField(max_length=255)
description = models.TextField(blank=True, null=True)
dataset = models.ForeignKey("Dataset", on_delete=models.CASCADE, related_name="tables")
dataset = models.ForeignKey(
"Dataset", on_delete=models.CASCADE, related_name="tables"
)
version = models.IntegerField(null=True, blank=True)
status = models.ForeignKey(
"Status", on_delete=models.PROTECT, related_name="tables", null=True, blank=True
Expand Down Expand Up @@ -949,7 +973,9 @@ class Table(BaseModel, OrderedModel):
compressed_file_size = models.BigIntegerField(blank=True, null=True)
number_rows = models.BigIntegerField(blank=True, null=True)
number_columns = models.BigIntegerField(blank=True, null=True)
is_closed = models.BooleanField(default=False, help_text="Table is for BD Pro subscribers only")
is_closed = models.BooleanField(
default=False, help_text="Table is for BD Pro subscribers only"
)
page_views = models.BigIntegerField(
default=0,
help_text="Number of page views by Google Analytics",
Expand Down Expand Up @@ -1262,7 +1288,10 @@ def clean(self) -> None:
"observation_level"
] = "Observation level is not in the same table as the column."

if self.directory_primary_key and self.directory_primary_key.table.is_directory is False:
if (
self.directory_primary_key
and self.directory_primary_key.table.is_directory is False
):
errors[
"directory_primary_key"
] = "Column indicated as a directory's primary key is not in a directory."
Expand Down Expand Up @@ -1390,7 +1419,9 @@ class Dictionary(BaseModel):
"""Model definition for Dictionary."""

id = models.UUIDField(primary_key=True, default=uuid4)
column = models.ForeignKey("Column", on_delete=models.CASCADE, related_name="dictionaries")
column = models.ForeignKey(
"Column", on_delete=models.CASCADE, related_name="dictionaries"
)

graphql_nested_filter_fields_whitelist = ["id"]

Expand All @@ -1409,7 +1440,9 @@ class CloudTable(BaseModel):
"""Model definition for CloudTable."""

id = models.UUIDField(primary_key=True, default=uuid4)
table = models.ForeignKey("Table", on_delete=models.CASCADE, related_name="cloud_tables")
table = models.ForeignKey(
"Table", on_delete=models.CASCADE, related_name="cloud_tables"
)
columns = models.ManyToManyField(
"Column",
related_name="cloud_tables",
Expand Down Expand Up @@ -1439,7 +1472,9 @@ def clean(self) -> None:
errors["gcp_table_id"] = "gcp_table_id must be in snake_case."
for column in self.columns.all():
if column.table != self.table:
errors["columns"] = f"Column {column} does not belong to table {self.table}."
errors[
"columns"
] = f"Column {column} does not belong to table {self.table}."
if errors:
raise ValidationError(errors)

Expand Down Expand Up @@ -1509,9 +1544,15 @@ class RawDataSource(BaseModel, OrderedModel):
availability = models.ForeignKey(
"Availability", on_delete=models.CASCADE, related_name="raw_data_sources"
)
languages = models.ManyToManyField("Language", related_name="raw_data_sources", blank=True)
languages = models.ManyToManyField(
"Language", related_name="raw_data_sources", blank=True
)
license = models.ForeignKey(
"License", on_delete=models.CASCADE, related_name="raw_data_sources", blank=True, null=True
"License",
on_delete=models.CASCADE,
related_name="raw_data_sources",
blank=True,
null=True,
)
area_ip_address_required = models.ManyToManyField(
"Area", related_name="raw_data_sources", blank=True
Expand Down Expand Up @@ -1798,7 +1839,9 @@ def clean(self) -> None:
self.end_second or 0,
)
if start_datetime > end_datetime:
errors["start_year"] = ["Start datetime cannot be greater than end datetime"]
errors["start_year"] = [
"Start datetime cannot be greater than end datetime"
]

except TypeError:
errors["start_year"] = ["Start year or end year are invalid"]
Expand Down
10 changes: 7 additions & 3 deletions bd_api/apps/api/v1/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ def test_date_time_range_empty(coverage_tabela_open):


@pytest.mark.django_db
def test_dataset_create(dataset_dados_mestres, tema_saude, tema_educacao, tag_aborto, tag_covid):
def test_dataset_create(
dataset_dados_mestres, tema_saude, tema_educacao, tag_aborto, tag_covid
):
"""Test for Dataset creation"""
dataset_dados_mestres.save()
dataset_dados_mestres.themes.add(tema_saude, tema_educacao)
Expand All @@ -127,7 +129,9 @@ def test_table_create(tabela_bairros):


@pytest.mark.django_db
def test_table_with_empty_coverage(tabela_bairros, coverage_tabela_open, datetime_range_empty):
def test_table_with_empty_coverage(
tabela_bairros, coverage_tabela_open, datetime_range_empty
):
"""
Test for Table with Coverage containing no DateTimeRange.
Coverage must be empty string.
Expand Down Expand Up @@ -200,7 +204,7 @@ def test_information_request_create(


@pytest.mark.django_db
def test_analysis_create( # pylint: disable=too-many-arguments
def test_analysis_create(
analise_bairros,
dataset_dados_mestres,
tema_saude,
Expand Down
Loading

0 comments on commit 764c6c9

Please sign in to comment.