Skip to content

Adding Type annotations #1290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 34 commits into from
May 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
266998b
commit
yashgoyal0110 Apr 5, 2025
13c49e4
make changes to config
yashgoyal0110 Apr 6, 2025
d70fc22
Merge branch 'main' into feat/adding-type-annotations
arkid15r Apr 6, 2025
5f26d16
Merge branch 'main' into feat/adding-type-annotations
yashgoyal0110 Apr 7, 2025
25c451e
Merge branch 'OWASP:main' into feat/adding-type-annotations
yashgoyal0110 Apr 9, 2025
d5c105c
fixed exceptions
yashgoyal0110 Apr 9, 2025
c8b2494
fixed exceptions
yashgoyal0110 Apr 9, 2025
b02455d
Merge branch 'main' into feat/adding-type-annotations
yashgoyal0110 Apr 11, 2025
56c6d37
conflicts resolved
yashgoyal0110 Apr 11, 2025
7ce76d0
Merge branch 'main' into feat/adding-type-annotations
yashgoyal0110 Apr 12, 2025
684b650
Address more warnings
arkid15r Apr 11, 2025
8147382
Update code
arkid15r Apr 12, 2025
05b4748
Update MultiSearch.tsx
arkid15r Apr 12, 2025
e5adaad
Update home page e2e tests
arkid15r Apr 12, 2025
f87a392
Update code
arkid15r Apr 13, 2025
51968b3
Merge branch 'main' into feat/adding-type-annotations
yashgoyal0110 Apr 14, 2025
f3b5079
Merge branch 'main' into feat/adding-type-annotations
yashgoyal0110 Apr 16, 2025
8300c3e
Merge branch 'main' into feat/adding-type-annotations
yashgoyal0110 Apr 17, 2025
1bcd7f1
Merge branch 'main' into pr/yashgoyal0110/1290
arkid15r Apr 21, 2025
1406f32
Merge branch 'main' into feat/adding-type-annotations
yashgoyal0110 Apr 22, 2025
38b6a00
conflicts resolved
yashgoyal0110 Apr 22, 2025
7a75ad8
Merge branch 'main' into feat/adding-type-annotations
yashgoyal0110 Apr 23, 2025
0c41d30
Merge branch 'main' into feat/adding-type-annotations
yashgoyal0110 Apr 25, 2025
401487c
Merge branch 'main' into feat/adding-type-annotations
yashgoyal0110 Apr 27, 2025
91f257f
conflicts resolved
yashgoyal0110 Apr 27, 2025
a18eaa3
Merge branch 'main' into feat/adding-type-annotations
yashgoyal0110 Apr 28, 2025
2eb6f6b
Merge branch 'main' into feat/adding-type-annotations
yashgoyal0110 Apr 29, 2025
bf5f204
Merge branch 'main' into feat/adding-type-annotations
yashgoyal0110 Apr 30, 2025
300dca0
Merge branch 'main' into feat/adding-type-annotations
yashgoyal0110 May 2, 2025
e840d3b
Merge branch 'main' into feat/adding-type-annotations
yashgoyal0110 May 2, 2025
7f4573d
commit
yashgoyal0110 May 2, 2025
7affed8
Merge branch 'main' into feat/adding-type-annotations
yashgoyal0110 May 2, 2025
724cb20
Merge branch 'main' into pr/yashgoyal0110/1290
arkid15r May 3, 2025
b1579ef
Update code
arkid15r May 3, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ repos:
- --sequence=4
exclude: (.github|pnpm-lock.yaml)

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.15.0
hooks:
- id: mypy
additional_dependencies:
- types-jsonschema
- types-lxml
- types-python-dateutil
- types-PyYAML
- types-requests
args:
- --config-file
- backend/pyproject.toml

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
Expand Down
3 changes: 2 additions & 1 deletion backend/apps/common/geocoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import time

from geopy.geocoders import Nominatim
from geopy.location import Location

from apps.common.utils import get_nest_user_agent


def get_location_coordinates(query, delay=2):
def get_location_coordinates(query: str, delay: int = 2) -> Location:
"""Get location geo coordinates.

Args:
Expand Down
26 changes: 14 additions & 12 deletions backend/apps/common/index.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Algolia index common classes and helpers."""

from __future__ import annotations

import logging
from functools import lru_cache
from pathlib import Path
Expand All @@ -13,7 +15,7 @@

from apps.common.constants import NL

logger = logging.getLogger(__name__)
logger: logging.Logger = logging.getLogger(__name__)

EXCLUDED_LOCAL_INDEX_NAMES = (
"projects_contributors_count_asc",
Expand All @@ -36,19 +38,19 @@ class IndexRegistry:

_instance = None

def __init__(self):
def __init__(self) -> None:
"""Initialize index registry."""
self.excluded_local_index_names = set()
self.excluded_local_index_names: set = set()
self.load_excluded_local_index_names()

@classmethod
def get_instance(cls):
def get_instance(cls) -> IndexRegistry:
"""Get or create a singleton instance of IndexRegistry."""
if cls._instance is None:
cls._instance = IndexRegistry()
return cls._instance

def is_indexable(self, name: str):
def is_indexable(self, name: str) -> bool:
"""Check if an index is enabled for indexing.

Args:
Expand All @@ -60,7 +62,7 @@ def is_indexable(self, name: str):
"""
return name.lower() not in self.excluded_local_index_names if IS_LOCAL_BUILD else True

def load_excluded_local_index_names(self):
def load_excluded_local_index_names(self) -> IndexRegistry:
"""Load excluded local index names from settings.

Returns
Expand All @@ -81,7 +83,7 @@ def load_excluded_local_index_names(self):
return self


def is_indexable(index_name: str):
def is_indexable(index_name: str) -> bool:
"""Determine if an index should be created based on configuration.

Args:
Expand Down Expand Up @@ -120,7 +122,7 @@ class IndexBase(AlgoliaIndex):
"""Base index class."""

@staticmethod
def get_client(ip_address=None):
def get_client(ip_address=None) -> SearchClientSync:
"""Return an instance of the search client.

Args:
Expand All @@ -140,7 +142,7 @@ def get_client(ip_address=None):
return SearchClientSync(config=config)

@staticmethod
def configure_replicas(index_name: str, replicas: dict):
def configure_replicas(index_name: str, replicas: dict) -> None:
"""Configure replicas for an index.

Args:
Expand Down Expand Up @@ -168,7 +170,7 @@ def configure_replicas(index_name: str, replicas: dict):
client.set_settings(replica_name, {"ranking": replica_ranking})

@staticmethod
def _parse_synonyms_file(file_path):
def _parse_synonyms_file(file_path) -> list | None:
"""Parse a synonyms file and return its content.

Args:
Expand Down Expand Up @@ -214,7 +216,7 @@ def _parse_synonyms_file(file_path):
return synonyms

@staticmethod
def reindex_synonyms(app_name, index_name):
def reindex_synonyms(app_name: str, index_name: str) -> int | None:
"""Reindex synonyms for a specific index.

Args:
Expand Down Expand Up @@ -246,7 +248,7 @@ def reindex_synonyms(app_name, index_name):

@staticmethod
@lru_cache(maxsize=1024)
def get_total_count(index_name, search_filters=None):
def get_total_count(index_name: str, search_filters=None) -> int | None:
"""Get the total count of records in an index.

Args:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,8 @@
class Command(BaseCommand):
help = "Update OWASP Nest index replicas."

def handle(self, *_args, **_options):
"""Update replicas for Algolia indices.

Args:
*_args: Positional arguments (not used).
**_options: Keyword arguments (not used).

"""
def handle(self, *_args, **_options) -> None:
"""Update replicas for Algolia indices."""
print("\n Starting replica configuration...")
ProjectIndex.configure_replicas()
print("\n Replica have been Successfully created.")
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,8 @@
class Command(BaseCommand):
help = "Update OWASP Nest index synonyms."

def handle(self, *_args, **_options):
"""Update synonyms for Algolia indices.

Args:
*_args: Positional arguments (not used).
**_options: Keyword arguments (not used).

"""
def handle(self, *_args, **_options) -> None:
"""Update synonyms for Algolia indices."""
print("\nThe following models synonyms were reindexed:")
for index in (IssueIndex, ProjectIndex):
count = index.update_synonyms()
Expand Down
30 changes: 15 additions & 15 deletions backend/apps/common/management/commands/generate_sitemap.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Management command to generate OWASP Nest sitemap."""

from datetime import datetime, timezone
from datetime import UTC, datetime
from pathlib import Path

from django.conf import settings
Expand Down Expand Up @@ -28,7 +28,7 @@ class Command(BaseCommand):
],
}

def add_arguments(self, parser):
def add_arguments(self, parser) -> None:
"""Add command-line arguments to the parser.

Args:
Expand All @@ -41,7 +41,7 @@ def add_arguments(self, parser):
help="Directory where sitemap files will be saved",
)

def handle(self, *args, **options):
def handle(self, *args, **options) -> None:
"""Generate sitemaps for the OWASP Nest application.

Args:
Expand Down Expand Up @@ -70,7 +70,7 @@ def handle(self, *args, **options):

self.stdout.write(self.style.SUCCESS(f"Successfully generated sitemaps in {output_dir}"))

def generate_project_sitemap(self, output_dir):
def generate_project_sitemap(self, output_dir: Path) -> None:
"""Generate a sitemap for projects.

Args:
Expand All @@ -91,7 +91,7 @@ def generate_project_sitemap(self, output_dir):
content = self.generate_sitemap_content(routes)
self.save_sitemap(content, output_dir / "sitemap-project.xml")

def generate_chapter_sitemap(self, output_dir):
def generate_chapter_sitemap(self, output_dir: Path) -> None:
"""Generate a sitemap for chapters.

Args:
Expand All @@ -112,7 +112,7 @@ def generate_chapter_sitemap(self, output_dir):
content = self.generate_sitemap_content(routes)
self.save_sitemap(content, output_dir / "sitemap-chapters.xml")

def generate_committee_sitemap(self, output_dir):
def generate_committee_sitemap(self, output_dir: Path) -> None:
"""Generate a sitemap for committees.

Args:
Expand All @@ -136,7 +136,7 @@ def generate_committee_sitemap(self, output_dir):
content = self.generate_sitemap_content(routes)
self.save_sitemap(content, output_dir / "sitemap-committees.xml")

def generate_user_sitemap(self, output_dir):
def generate_user_sitemap(self, output_dir: Path) -> None:
"""Generate a sitemap for users.

Args:
Expand Down Expand Up @@ -172,7 +172,7 @@ def generate_sitemap_content(self, routes):

"""
urls = []
lastmod = datetime.now(timezone.utc).strftime("%Y-%m-%d")
lastmod = datetime.now(UTC).strftime("%Y-%m-%d")

for route in routes:
url_entry = {
Expand All @@ -185,7 +185,7 @@ def generate_sitemap_content(self, routes):

return self.create_sitemap(urls)

def generate_index_sitemap(self, sitemap_files):
def generate_index_sitemap(self, sitemap_files: list) -> str:
"""Generate the sitemap index file.

Args:
Expand All @@ -196,15 +196,15 @@ def generate_index_sitemap(self, sitemap_files):

"""
sitemaps = []
lastmod = datetime.now(timezone.utc).strftime("%Y-%m-%d")
lastmod = datetime.now(UTC).strftime("%Y-%m-%d")

for sitemap_file in sitemap_files:
sitemap_entry = {"loc": f"{settings.SITE_URL}/{sitemap_file}", "lastmod": lastmod}
sitemaps.append(self.create_sitemap_index_entry(sitemap_entry))

return self.create_sitemap_index(sitemaps)

def create_url_entry(self, url_data):
def create_url_entry(self, url_data: dict) -> str:
"""Create a URL entry for the sitemap.

Args:
Expand All @@ -223,7 +223,7 @@ def create_url_entry(self, url_data):
" </url>"
).format(**url_data)

def create_sitemap_index_entry(self, sitemap_data):
def create_sitemap_index_entry(self, sitemap_data: dict) -> str:
"""Create a sitemap index entry.

Args:
Expand All @@ -237,7 +237,7 @@ def create_sitemap_index_entry(self, sitemap_data):
" <sitemap>\n <loc>{loc}</loc>\n <lastmod>{lastmod}</lastmod>\n </sitemap>"
).format(**sitemap_data)

def create_sitemap(self, urls):
def create_sitemap(self, urls: list) -> str:
"""Create the complete sitemap XML.

Args:
Expand All @@ -254,7 +254,7 @@ def create_sitemap(self, urls):
"</urlset>"
)

def create_sitemap_index(self, sitemaps):
def create_sitemap_index(self, sitemaps: list) -> str:
"""Create the complete sitemap index XML.

Args:
Expand All @@ -272,7 +272,7 @@ def create_sitemap_index(self, sitemaps):
)

@staticmethod
def save_sitemap(content, filepath):
def save_sitemap(content: str, filepath: Path) -> None:
"""Save the sitemap content to a file.

Args:
Expand Down
10 changes: 2 additions & 8 deletions backend/apps/common/management/commands/load_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,8 @@
class Command(BaseCommand):
help = "Load OWASP Nest data."

def handle(self, *_args, **_options):
"""Load data into the OWASP Nest application.

Args:
*_args: Positional arguments (not used).
**_options: Keyword arguments (not used).

"""
def handle(self, *_args, **_options) -> None:
"""Load data into the OWASP Nest application."""
# Disable indexing
unregister_indexes()

Expand Down
10 changes: 2 additions & 8 deletions backend/apps/common/management/commands/purge_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,8 @@
class Command(BaseCommand):
help = "Purge OWASP Nest data."

def handle(self, *_args, **options):
"""Purge data from specified OWASP Nest applications.

Args:
*_args: Positional arguments (not used).
**options: Keyword arguments (not used).

"""
def handle(self, *_args, **options) -> None:
"""Purge data from specified OWASP Nest applications."""
nest_apps = ("github", "owasp")

with connection.cursor() as cursor:
Expand Down
2 changes: 1 addition & 1 deletion backend/apps/common/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Meta:
abstract = True

@staticmethod
def bulk_save(model, objects, fields=None):
def bulk_save(model, objects, fields=None) -> None:
"""Bulk save objects.

Args:
Expand Down
Loading