diff --git a/Makefile b/Makefile index e5fdbae9..0e416510 100644 --- a/Makefile +++ b/Makefile @@ -62,15 +62,15 @@ doc8: valid: @echo "-> Run Ruff format" - @${ACTIVATE} ruff format -exclude purldb-toolkit/ --exclude purl2vcs + @${ACTIVATE} ruff format --exclude etc/scripts/ --exclude purldb-toolkit/ --exclude purl2vcs/ @echo "-> Run Ruff linter" - @${ACTIVATE} ruff check --fix --exclude purldb-toolkit/ --exclude purl2vcs + @${ACTIVATE} ruff check --fix --exclude etc/scripts/ --exclude purldb-toolkit/ --exclude purl2vcs/ check: @echo "-> Run Ruff linter validation (pycodestyle, bandit, isort, and more)" - @${ACTIVATE} ruff check --exclude purldb-toolkit/ --exclude purl2vcs + @${ACTIVATE} ruff check --exclude etc/scripts/ --exclude purldb-toolkit/ --exclude purl2vcs/ @echo "-> Run Ruff format validation" - @${ACTIVATE} ruff format --check --exclude purldb-toolkit/ --exclude purl2vcs + @${ACTIVATE} ruff format --check --exclude etc/scripts/ --exclude purldb-toolkit/ --exclude purl2vcs/ @$(MAKE) doc8 clean: diff --git a/clearcode/store_scans.py b/clearcode/store_scans.py index 0304f193..f2dbef2a 100644 --- a/clearcode/store_scans.py +++ b/clearcode/store_scans.py @@ -33,18 +33,18 @@ from clearcode.models import CDitem """ -The input is a bunch of scans from ClearlyDefined and -the output is a bunch of git repositories with commited and -pushed scans such that we balance the scans roughly evenly accross +The input is a bunch of scans from ClearlyDefined and +the output is a bunch of git repositories with commited and +pushed scans such that we balance the scans roughly evenly accross different repositories. -The primary reason for multiple repositories is size of a single -repo. There is a size limit of 5 GB at GitHub and it's difficult +The primary reason for multiple repositories is size of a single +repo. There is a size limit of 5 GB at GitHub and it's difficult to work with repositories with million files. -Therefore the approach is to use hashing as a way to name git -repositories and directories. We compute hash on the purl of the scanned -package and use the first few layers of this hash for the repo and +Therefore the approach is to use hashing as a way to name git +repositories and directories. We compute hash on the purl of the scanned +package and use the first few layers of this hash for the repo and directory names. Initial processing steps are: @@ -54,15 +54,15 @@ - Then we store the scan using the purl hash and purl as path. - Finally commit and push! : ) -Because it's not practical to process many repos at once, we organize the -processing one repo a time. For this, we iterate over a bunch of records get or compute +Because it's not practical to process many repos at once, we organize the +processing one repo a time. For this, we iterate over a bunch of records get or compute the purl hash and process the records that share the same hash. -We are using a short hash that is three characters long using hexadecimal encoding. -Therefore we can have 16*16*16 = 4096 repositories where each repo would contain about +We are using a short hash that is three characters long using hexadecimal encoding. +Therefore we can have 16*16*16 = 4096 repositories where each repo would contain about 25k scan files, if we were to store 100 million scans (which is a high mark). -For reference one scan should use less than a 100k on average when compressed -with gzip or git based on looking at 15 million scans. Each repo should be roughly +For reference one scan should use less than a 100k on average when compressed +with gzip or git based on looking at 15 million scans. Each repo should be roughly couple hundred mega bytes big, based on 15 million scans. """ diff --git a/clearcode/sync.py b/clearcode/sync.py index 4b1d8098..9d3dd34f 100644 --- a/clearcode/sync.py +++ b/clearcode/sync.py @@ -350,7 +350,7 @@ def is_unchanged_remotely(self, url, session=session): remote_etag = response.headers.get("etag") if remote_etag and self.etags_cache.get(url) == remote_etag: return True - except: + except Exception: return False def is_fetched(self, checksum, url): diff --git a/matchcode/tests/test_match.py b/matchcode/tests/test_match.py index 07faada7..a09fab4d 100644 --- a/matchcode/tests/test_match.py +++ b/matchcode/tests/test_match.py @@ -34,7 +34,7 @@ class MatchPackagesTestCase(MatchcodeTestCase): def setUp(self): # Execute the superclass' setUp method before creating our own # DB objects - super(MatchPackagesTestCase, self).setUp() + super().setUp() self.test_package1, _ = Package.objects.get_or_create( filename="abbot-0.12.3.jar", @@ -158,7 +158,7 @@ class MatchNestedPackagesTestCase(MatchcodeTestCase): def setUp(self): # Execute the superclass' setUp method before creating our own # DB objects - super(MatchNestedPackagesTestCase, self).setUp() + super().setUp() self.test_package1, _ = Package.objects.get_or_create( filename="plugin-request-2.4.1.tgz", @@ -219,7 +219,7 @@ class DirectoryMatchingTestCase(MatchcodeTestCase): maxDiff = None def setUp(self): - super(DirectoryMatchingTestCase, self).setUp() + super().setUp() self.test_package1, _ = Package.objects.get_or_create( filename="abbrev-1.0.3.tgz", diff --git a/matchcode/tests/test_models.py b/matchcode/tests/test_models.py index a74d6aa2..d0eef2e9 100644 --- a/matchcode/tests/test_models.py +++ b/matchcode/tests/test_models.py @@ -41,7 +41,7 @@ class BaseModelTest(MatchcodeTestCase): maxDiff = None def setUp(self): - super(BaseModelTest, self).setUp() + super().setUp() self.test_package1, _ = Package.objects.get_or_create( filename="abbot-0.12.3.jar", diff --git a/minecode/filter.py b/minecode/filter.py index 371e9415..018c6bc9 100644 --- a/minecode/filter.py +++ b/minecode/filter.py @@ -39,28 +39,28 @@ def sf_net(input_file, output): writer = csv.writer(fo, quoting=csv.QUOTE_ALL) with open(input_file) as fi: reader = csv.reader(fi) - for i, l in enumerate(reader): + for i, row in enumerate(reader): if i == 0: # add headers on first row - l.extend(new_headers) - if not l: + row.extend(new_headers) + if not row: continue - project_id = l[0] - name = l[1] - version_column = l[2] + project_id = row[0] + name = row[1] + version_column = row[2] sep = ": released on " if sep not in version_column: # write as is if we do not have a file release date # separator - writer.writerow(l) + writer.writerow(row) continue filename, release_date_ts = version_column.split(sep, 1) found_version = version.version_hint(filename) - l.append(found_version or "") - l.append(release_date_ts or "") - l.append(download_url_template % locals()) - l.append("") # reviewed - l.append("") # curated name + row.append(found_version or "") + row.append(release_date_ts or "") + row.append(download_url_template % locals()) + row.append("") # reviewed + row.append("") # curated name excluded_reason = "" if "." in project_id: excluded_reason = "mirror or special project" @@ -70,10 +70,10 @@ def sf_net(input_file, output): excluded_reason = "special chars in name" elif not good_filename(project_id, filename, name): excluded_reason = "multi component possible" - l.append(excluded_reason) - l.append("") # curated_owner - l.append("") # owner_type - writer.writerow(l) + row.append(excluded_reason) + row.append("") # curated_owner + row.append("") # owner_type + writer.writerow(row) def good_name(s): diff --git a/minecode/indexing.py b/minecode/indexing.py index 797f6d15..5e6016a6 100644 --- a/minecode/indexing.py +++ b/minecode/indexing.py @@ -100,7 +100,7 @@ def index_package( declared_license_expression = summary_data.get("declared_license_expression") other_license_expressions = summary_data.get("other_license_expressions", []) other_license_expressions = [ - l["value"] for l in other_license_expressions if l["value"] + license_expression["value"] for license_expression in other_license_expressions if license_expression["value"] ] other_license_expression = combine_expressions(other_license_expressions) diff --git a/minecode/management/commands/check_licenses.py b/minecode/management/commands/check_licenses.py index 62eb32cc..fbab6dcc 100644 --- a/minecode/management/commands/check_licenses.py +++ b/minecode/management/commands/check_licenses.py @@ -99,8 +99,7 @@ def find_ambiguous_packages( ) license_filter = reduce(operator.or_, filter_expression) - for package in Package.objects.filter(type__in=types).filter(license_filter): - yield package + yield from Package.objects.filter(type__in=types).filter(license_filter) def dump(packages, json_location): diff --git a/minecode/mappers/bitbucket.py b/minecode/mappers/bitbucket.py index fcf80989..0d199d76 100644 --- a/minecode/mappers/bitbucket.py +++ b/minecode/mappers/bitbucket.py @@ -33,10 +33,7 @@ def get_packages(self, uri, resource_uri): """Yield Package built from resource_uri record for a single package version.""" downloads_data = json.loads(resource_uri.data, object_pairs_hook=OrderedDict) for download_data in downloads_data.get("values", []): - for package in build_bitbucket_download_packages( - download_data, resource_uri.package_url - ): - yield package + yield from build_bitbucket_download_packages(download_data, resource_uri.package_url) def build_bitbucket_download_packages(download_data, purl): diff --git a/minecode/mappers/cpan.py b/minecode/mappers/cpan.py index 627f4ed0..5633bff1 100644 --- a/minecode/mappers/cpan.py +++ b/minecode/mappers/cpan.py @@ -49,7 +49,7 @@ def build_packages_from_release_json(metadata, uri=None): continue extracted_license_statement = [ - l for l in release.get("license", []) if l and l.strip() + lic for lic in release.get("license", []) if lic and lic.strip() ] common_data = dict( @@ -87,7 +87,7 @@ def build_packages_from_release_json(metadata, uri=None): # like perl_5. The license here under resources section is the # url of license for example: http://dev.perl.org/licenses/ So # it's useful to collect both information... - license_url = [l for l in resources.get("license", []) if l and l.strip()] + license_url = [lic for lic in resources.get("license", []) if lic and lic.strip()] if license_url: common_data["extracted_license_statement"].extend(license_url) @@ -164,7 +164,7 @@ def build_packages_from_metafile(metadata, uri=None, purl=None): licenses_content = content.get("license") extracted_license_statement = [] if licenses_content: - if isinstance(licenses_content, (list,)): + if isinstance(licenses_content, list): for lic in licenses_content: extracted_license_statement.append(lic) else: diff --git a/minecode/mappers/cran.py b/minecode/mappers/cran.py index 0938e8b6..ced03d0e 100644 --- a/minecode/mappers/cran.py +++ b/minecode/mappers/cran.py @@ -95,7 +95,7 @@ def build_packages_from_html(metadata, uri=None, purl=None): if key == "Version:": common_data["version"] = value elif key == "URL:": - if type(value) == list and len(value) > 0: + if type(value) is list and len(value) > 0: homepages = [] for home_page in value: homepages.append(home_page) @@ -129,7 +129,7 @@ def build_packages_from_html(metadata, uri=None, purl=None): ) common_data["parties"].append(party.to_dict()) elif "source" in key or "binaries" in key: - if type(value) == list: + if type(value) is list: for url in value: download_urls.append(get_download_url(url)) elif key == "Published:": diff --git a/minecode/mappers/eclipse.py b/minecode/mappers/eclipse.py index d53a5764..4cecbc66 100644 --- a/minecode/mappers/eclipse.py +++ b/minecode/mappers/eclipse.py @@ -63,7 +63,7 @@ def build_packages_with_json(metadata, purl=None, uri=None): if project_metadata.get("licenses"): common_data["extracted_license_statement"] = [ - l.get("name") for l in project_metadata.get("licenses", []) + lic.get("name") for lic in project_metadata.get("licenses", []) ] common_data["license_detections"] = [] diff --git a/minecode/mappers/sourceforge.py b/minecode/mappers/sourceforge.py index a0025098..85dba8bc 100644 --- a/minecode/mappers/sourceforge.py +++ b/minecode/mappers/sourceforge.py @@ -79,11 +79,11 @@ def build_packages_from_metafile(metadata, purl=None, uri=None): extracted_license_statement = [] licenses = categories.get("license") or [] - for l in licenses: - license_name = l.get("fullname") + for lic in licenses: + license_name = lic.get("fullname") # full name is first priority than shortname since shortname is like gpl, it doesn't show detailed gpl version etc. if license_name: - extracted_license_statement.append(l.get("shortname")) + extracted_license_statement.append(lic.get("shortname")) if license_name: extracted_license_statement.append(license_name) if extracted_license_statement: diff --git a/minecode/models.py b/minecode/models.py index be6cf92e..009c5bb6 100644 --- a/minecode/models.py +++ b/minecode/models.py @@ -492,7 +492,7 @@ def save(self, *args, **kwargs): self.normalize_fields() self.has_map_error = True if self.map_error else False self.has_visit_error = True if self.visit_error else False - super(ResourceURI, self).save(*args, **kwargs) + super().save(*args, **kwargs) class ScannableURIManager(models.Manager): @@ -790,7 +790,7 @@ def save(self, *args, **kwargs): if not self.canonical: self.canonical = get_canonical(self.uri) self.normalize_fields() - super(ScannableURI, self).save(*args, **kwargs) + super().save(*args, **kwargs) def process_scan_results( self, scan_results_location, scan_summary_location, project_extra_data @@ -971,7 +971,7 @@ class Meta: def save(self, *args, **kwargs): """Save, adding defaults for computed fields and validating fields.""" self.normalize_fields() - super(PriorityResourceURI, self).save(*args, **kwargs) + super().save(*args, **kwargs) # TODO: Use the QuerySet.as_manager() for more flexibility and chaining. @@ -1087,7 +1087,7 @@ class Meta: def save(self, *args, **kwargs): """Save, adding defaults for computed fields and validating fields.""" self.normalize_fields() - super(ImportableURI, self).save(*args, **kwargs) + super().save(*args, **kwargs) class ProcessingError(BaseURI): diff --git a/minecode/saneyaml.py b/minecode/saneyaml.py index e55fc638..ea7c13be 100644 --- a/minecode/saneyaml.py +++ b/minecode/saneyaml.py @@ -121,7 +121,7 @@ def ordered_loader(loader, node): class SaneDumper(SafeDumper): def increase_indent(self, flow=False, indentless=False): """Ensure that lists items are always indented.""" - return super(SaneDumper, self).increase_indent(flow, indentless=False) + return super().increase_indent(flow, indentless=False) def ignore_aliases(self, data): """Avoid having aliases created from re-used Python objects.""" diff --git a/minecode/seed.py b/minecode/seed.py index 9a805960..abba367a 100644 --- a/minecode/seed.py +++ b/minecode/seed.py @@ -45,7 +45,7 @@ def get_active_seeders(seeders=()): if not seeders: seeders = get_configured_seeders() for seeder in seeders: - if isinstance(seeder, (bytes, unicode)): + if isinstance(seeder, bytes | unicode): module_name, _, class_name = seeder.rpartition(".") module = importlib.import_module(module_name) yield getattr(module, class_name)() diff --git a/minecode/tests/test_api.py b/minecode/tests/test_api.py index 9eab2af7..39683e98 100644 --- a/minecode/tests/test_api.py +++ b/minecode/tests/test_api.py @@ -30,7 +30,7 @@ class ScannableURIAPITestCase(JsonBasedTesting, TestCase): def setUp(self): self.scan_queue_worker_user = User.objects.create_user( - username="username", email="e@mail.com", password="secret" + username="username", email="e@mail.com", password="secret" # NOQA ) scan_queue_workers_group, _ = Group.objects.get_or_create( name="scan_queue_workers" @@ -49,7 +49,7 @@ def setUp(self): self.staff_user = User.objects.create_user( username="staff_username", email="staff_e@mail.com", - password="secret", + password="secret", # NOQA is_staff=True, ) self.staff_auth = f"Token {self.staff_user.auth_token.key}" @@ -60,7 +60,7 @@ def setUp(self): self.regular_user = User.objects.create_user( username="regular_username", email="regular_e@mail.com", - password="secret", + password="secret", # NOQA ) self.regular_auth = f"Token {self.regular_user.auth_token.key}" self.regular_client = APIClient(enforce_csrf_checks=True) diff --git a/minecode/tests/test_conan.py b/minecode/tests/test_conan.py index a7ed5b22..213f983e 100644 --- a/minecode/tests/test_conan.py +++ b/minecode/tests/test_conan.py @@ -25,7 +25,7 @@ class ConanPriorityQueueTests(JsonBasedTesting, TestCase): test_data_dir = os.path.join(os.path.dirname(__file__), "testfiles") def setUp(self): - super(ConanPriorityQueueTests, self).setUp() + super().setUp() self.package_url1 = PackageURL.from_string("pkg:conan/zlib@1.3.1") zlib_conanfile_loc = self.get_test_loc("conan/zlib/manifest/conanfile.py") zlib_conandata_loc = self.get_test_loc("conan/zlib/manifest/conandata.yml") diff --git a/minecode/tests/test_gnu.py b/minecode/tests/test_gnu.py index 49231b39..d87c2ff1 100644 --- a/minecode/tests/test_gnu.py +++ b/minecode/tests/test_gnu.py @@ -22,7 +22,7 @@ class GnuPriorityQueueTests(JsonBasedTesting, TestCase): test_data_dir = os.path.join(os.path.dirname(__file__), "testfiles") def setUp(self): - super(GnuPriorityQueueTests, self).setUp() + super().setUp() glibc_data_loc = self.get_test_loc("gnu/glibc/index.html") with open(glibc_data_loc) as f: diff --git a/minecode/tests/test_maven.py b/minecode/tests/test_maven.py index 319af56b..6335ec56 100644 --- a/minecode/tests/test_maven.py +++ b/minecode/tests/test_maven.py @@ -903,7 +903,7 @@ class MavenPriorityQueueTests(JsonBasedTesting, DjangoTestCase): test_data_dir = os.path.join(os.path.dirname(__file__), "testfiles") def setUp(self): - super(MavenPriorityQueueTests, self).setUp() + super().setUp() self.expected_pom_loc = self.get_test_loc("maven/pom/classworlds-1.1.pom") with open(self.expected_pom_loc) as f: diff --git a/minecode/tests/test_migrations.py b/minecode/tests/test_migrations.py index 83ec1311..45dd1243 100644 --- a/minecode/tests/test_migrations.py +++ b/minecode/tests/test_migrations.py @@ -133,51 +133,6 @@ def test_set_is_visitable_for_maven_index_uris(self): self.assertEqual(results, expected) -class TestSetIsVisitableForMavenIndexURIs(TestMigrations): - app_name = "minecode" - migrate_from = "0025_populate_has_error_fields" - migrate_to = "0026_set_is_visitable_for_maven_index_uris" - - def setUpBeforeMigration(self, apps): - # using get_model to avoid circular import - ResourceURI = apps.get_model("minecode", "ResourceURI") - - self.resource_uris = [ - ResourceURI.objects.create( - uri="maven-index://repo1.maven.org/zone/src/sheaf/logback-sheaf/1.1.7/logback-sheaf-1.1.7.jar", - is_visitable=True, - ), - ResourceURI.objects.create( - uri="maven-index://repo1.maven.org/zone/src/sheaf/logback-sheaf/1.1.7/logback-sheaf-1.1.8.jar", - is_visitable=False, - ), - ] - - for resource_uri in self.resource_uris: - resource_uri.save() - - def test_set_is_visitable_for_maven_index_uris(self): - # using get_model to avoid circular import - ResourceURI = apps.get_model("minecode", "ResourceURI") - results = list( - ResourceURI.objects.values( - "uri", - "is_visitable", - ).all() - ) - expected = [ - { - "is_visitable": False, - "uri": "maven-index://repo1.maven.org/zone/src/sheaf/logback-sheaf/1.1.7/logback-sheaf-1.1.8.jar", - }, - { - "is_visitable": False, - "uri": "maven-index://repo1.maven.org/zone/src/sheaf/logback-sheaf/1.1.7/logback-sheaf-1.1.7.jar", - }, - ] - self.assertEqual(results, expected) - - class TestReplaceHttpWithHttpsInMavenURIs(TestMigrations): app_name = "minecode" migrate_from = "0026_set_is_visitable_for_maven_index_uris" diff --git a/minecode/tests/test_npm.py b/minecode/tests/test_npm.py index 53e7ac5f..6bd1d4df 100644 --- a/minecode/tests/test_npm.py +++ b/minecode/tests/test_npm.py @@ -177,7 +177,7 @@ class NpmPriorityQueueTests(JsonBasedTesting, DjangoTestCase): test_data_dir = os.path.join(os.path.dirname(__file__), "testfiles") def setUp(self): - super(NpmPriorityQueueTests, self).setUp() + super().setUp() self.expected_json_loc = self.get_test_loc("npm/lodash_package-expected.json") with open(self.expected_json_loc) as f: self.expected_json_contents = json.load(f) diff --git a/minecode/utils.py b/minecode/utils.py index 846ebe6b..34bdbaa1 100644 --- a/minecode/utils.py +++ b/minecode/utils.py @@ -390,8 +390,7 @@ def _setup(self): i : i + self.max_obj_num ] logger.debug("Grabbing next %s objects from DB" % self.max_obj_num) - for obj in smaller_queryset.iterator(): - yield obj + yield from smaller_queryset.iterator() def __iter__(self): return self._generator diff --git a/minecode/visitors/__init__.py b/minecode/visitors/__init__.py index e75b1ac8..a1b29a0f 100644 --- a/minecode/visitors/__init__.py +++ b/minecode/visitors/__init__.py @@ -8,11 +8,8 @@ # -import gzip import json -import os import pkgutil -import tempfile from functools import total_ordering from minecode.utils import fetch_http @@ -231,7 +228,7 @@ def fetch(self, uri, timeout=10): `timeout` is a default timeout. """ - content = super(NonPersistentHttpVisitor, self).fetch(uri, timeout=timeout) + content = super().fetch(uri, timeout=timeout) temp_file = get_temp_file("NonPersistentHttpVisitor") with open(temp_file, "wb") as tmp: tmp.write(content) diff --git a/minecode/visitors/cpan.py b/minecode/visitors/cpan.py index 95452ad7..253412fb 100644 --- a/minecode/visitors/cpan.py +++ b/minecode/visitors/cpan.py @@ -183,7 +183,7 @@ class CpanReadmeVisitors(HttpVisitor): def dumps(self, content): """Return the json by parsing the readme content""" # Handle bytes properly in python3 - if type(content) == bytes: + if type(content) is bytes: content = content.decode("utf-8") lines = content.splitlines() diff --git a/minecode/visitors/github.py b/minecode/visitors/github.py index 1e6f24b6..89785536 100644 --- a/minecode/visitors/github.py +++ b/minecode/visitors/github.py @@ -182,7 +182,7 @@ def fetch(self, uri, timeout=None): def json_serial_date_obj(obj): """JSON serializer for date object""" - if obj and isinstance(obj, (datetime, date)): + if obj and isinstance(obj, datetime | date): return obj.isoformat() diff --git a/minecode/visitors/maven.py b/minecode/visitors/maven.py index 2473ba2f..baf5019d 100644 --- a/minecode/visitors/maven.py +++ b/minecode/visitors/maven.py @@ -469,17 +469,17 @@ def process_request(purl_str, **kwargs): def check_if_file_name_is_linked_on_page(file_name, links, **kwargs): """Return True if `file_name` is in `links`""" - return any(l.endswith(file_name) for l in links) + return any(link.endswith(file_name) for link in links) def check_if_page_has_pom_files(links, **kwargs): """Return True of any entry in `links` ends with .pom.""" - return any(l.endswith(".pom") for l in links) + return any(link.endswith(".pom") for link in links) def check_if_page_has_directories(links, **kwargs): """Return True if any entry, excluding "../", ends with /.""" - return any(l.endswith("/") for l in links if l != "../") + return any(link.endswith("/") for link in links if link != "../") def check_if_package_version_page(links, **kwargs): diff --git a/packagedb/api.py b/packagedb/api.py index bf72d74a..f9d8624b 100644 --- a/packagedb/api.py +++ b/packagedb/api.py @@ -550,7 +550,7 @@ def create(self, request): if package_set: package_set = package_set - except: + except Exception: message = {"update_status": f"No Package Set found for {uuid}"} return Response(message, status=status.HTTP_400_BAD_REQUEST) diff --git a/packagedb/management/commands/run_scheduler.py b/packagedb/management/commands/run_scheduler.py index e09fc561..e8f065f1 100644 --- a/packagedb/management/commands/run_scheduler.py +++ b/packagedb/management/commands/run_scheduler.py @@ -29,4 +29,4 @@ class Command(rqscheduler.Command): def handle(self, *args, **kwargs): clear_zombie_watch_schedules() init_watch_scheduled() - super(Command, self).handle(*args, **kwargs) + super().handle(*args, **kwargs) diff --git a/packagedb/models.py b/packagedb/models.py index 0a1fcb09..bed7181f 100644 --- a/packagedb/models.py +++ b/packagedb/models.py @@ -79,8 +79,7 @@ def paginated(self, per_page=5000): paginator = Paginator(self, per_page=per_page) for page_number in paginator.page_range: page = paginator.page(page_number) - for object in page.object_list: - yield object + yield from page.object_list VCS_CHOICES = [ @@ -94,7 +93,7 @@ def paginated(self, per_page=5000): class LowerCaseField(models.CharField): def __init__(self, *args, **kwargs): - super(LowerCaseField, self).__init__(*args, **kwargs) + super().__init__(*args, **kwargs) def to_python(self, value): return str(value).lower() @@ -218,7 +217,7 @@ class ExtraDataFieldMixin(models.Model): def update_extra_data(self, data): """Updates the `extra_data` field with the provided `data` dict.""" - if type(data) != dict: + if type(data) is not dict: raise ValueError("Argument `data` value must be a dict()") self.extra_data.update(data) @@ -670,7 +669,7 @@ def update_fields(self, save=False, **values_by_fields): unsaved_models = [] if field == "dependencies": for dep_data in value: - if isinstance(dep_data, (dict, OrderedDict)): + if isinstance(dep_data, dict | OrderedDict): dep = DependentPackage( package=self, purl=dep_data.get("purl"), @@ -692,7 +691,7 @@ def update_fields(self, save=False, **values_by_fields): if field == "parties": for party_data in value: - if isinstance(party_data, (dict, OrderedDict)): + if isinstance(party_data, dict | OrderedDict): party = Party( package=self, type=party_data.get("type"), @@ -711,7 +710,7 @@ def update_fields(self, save=False, **values_by_fields): if field == "resources": for resource_data in value: - if isinstance(resource_data, (dict, OrderedDict)): + if isinstance(resource_data, dict | OrderedDict): resource = Resource( package=self, path=resource_data.get("path"), @@ -1377,7 +1376,7 @@ def save(self, *args, **kwargs): if schedule: self.schedule_work_id = self.create_new_job() - super(PackageWatch, self).save(*args, **kwargs) + super().save(*args, **kwargs) def delete(self, *args, **kwargs): """Clear associated watch schedule.""" diff --git a/packagedb/package_managers.py b/packagedb/package_managers.py index b25e101d..898dc4da 100644 --- a/packagedb/package_managers.py +++ b/packagedb/package_managers.py @@ -58,7 +58,7 @@ def get_response(url, content_type="json", headers=None): try: resp = requests.get(url=url, headers=headers) - except: + except Exception: logger.error(traceback.format_exc()) return if not resp.status_code == 200: diff --git a/packagedb/tests/test_api.py b/packagedb/tests/test_api.py index 9076e905..29e9dc80 100644 --- a/packagedb/tests/test_api.py +++ b/packagedb/tests/test_api.py @@ -172,11 +172,6 @@ def test_api_resource_list_endpoint_returns_none_when_filtering_by_wrong_purl(se self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(0, response.data.get("count")) - def test_api_resource_list_endpoint_returns_none_when_filtering_by_blank_uuid(self): - response = self.client.get("/api/resources/?purl={}".format("")) - self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertEqual(2, response.data.get("count")) - def test_api_resource_list_endpoint_filters_by_package1_purl(self): response = self.client.get( f"/api/resources/?purl={self.package1.package_url}" diff --git a/packagedb/tests/test_throttling.py b/packagedb/tests/test_throttling.py index d8dcc5eb..620efd3c 100644 --- a/packagedb/tests/test_throttling.py +++ b/packagedb/tests/test_throttling.py @@ -21,7 +21,7 @@ class ThrottleApiTests(APITestCase): def setUp(self): # create a basic user self.user = User.objects.create_user( - username="username", email="e@mail.com", password="secret" + username="username", email="e@mail.com", password="secret" # NOQA ) self.auth = f"Token {self.user.auth_token.key}" self.csrf_client = APIClient(enforce_csrf_checks=True) @@ -31,7 +31,7 @@ def setUp(self): self.staff_user = User.objects.create_user( username="staff_username", email="staff_e@mail.com", - password="secret", + password="secret", # NOQA is_staff=True, ) self.staff_auth = f"Token {self.staff_user.auth_token.key}" diff --git a/packagedb/to_purl.py b/packagedb/to_purl.py index 9eac48b1..5066b23a 100644 --- a/packagedb/to_purl.py +++ b/packagedb/to_purl.py @@ -58,7 +58,7 @@ def list(self, request): go_import = validated_data.get("go_package") try: purl = get_golang_purl(go_import) - except: + except Exception: return Response( {"errors": "`@` is not supported either in import or go.mod string"}, status=status.HTTP_400_BAD_REQUEST, diff --git a/pyproject.toml b/pyproject.toml index b8a79ce8..b8f8f699 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -68,7 +68,7 @@ select = [ "I", # isort "C9", # McCabe complexity ] -ignore = ["D1", "D203", "D205", "D212", "D400", "D415", "E501", "S101", "S113"] +ignore = ["D1", "D203", "D205", "D212", "D400", "D415", "E501", "S101", "S113", "S324"] [tool.ruff.lint.isort] force-single-line = true