From 9fad8da9373fc55081f86fc52fc205766196bff3 Mon Sep 17 00:00:00 2001 From: Jeroen Dekkers Date: Tue, 3 Dec 2024 11:29:35 +0100 Subject: [PATCH] Update pre-commit and all hooks (#3923) Co-authored-by: Jan Klopper --- .github/workflows/pre_commit_checks.yml | 2 +- .pre-commit-config.yaml | 28 +++++++-------- boefjes/boefjes/plugins/helpers.py | 24 +++++++------ .../check_images/normalize.py | 2 +- .../integration/test_sql_repositories.py | 6 ++-- .../tests/katalogus/test_plugin_service.py | 4 +-- boefjes/tests/plugins/test_bodyimage.py | 4 +-- boefjes/tests/plugins/test_calvin.py | 16 ++++----- boefjes/tests/plugins/test_manual.py | 36 +++++++++---------- cveapi/cveapi.py | 2 +- mula/scheduler/storage/filters/casting.py | 2 +- octopoes/octopoes/core/service.py | 4 +-- .../tests/test_bit_expiring_certificate.py | 1 + pyproject.toml | 3 +- .../reports/report_types/dns_report/report.py | 2 +- rocky/reports/utils.py | 5 ++- .../rocky/views/ooi_detail_related_object.py | 19 +++++----- rocky/tools/forms/ooi_form.py | 6 ++-- rocky/tools/view_helpers.py | 2 +- 19 files changed, 89 insertions(+), 79 deletions(-) diff --git a/.github/workflows/pre_commit_checks.yml b/.github/workflows/pre_commit_checks.yml index fa537d8692c..f2e7082f44d 100644 --- a/.github/workflows/pre_commit_checks.yml +++ b/.github/workflows/pre_commit_checks.yml @@ -24,7 +24,7 @@ jobs: cache: pip - name: Install pre-commit - run: pip install pre-commit==3.8.0 + run: pip install pre-commit==4.0.1 - uses: actions/cache@v4 with: diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2219fd8b2a8..89228a63f0f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.5.0 + rev: v5.0.0 hooks: - id: trailing-whitespace - id: mixed-line-ending @@ -29,13 +29,13 @@ repos: args: ["--autofix", "--no-ensure-ascii", "--no-sort-keys"] - repo: https://github.com/abravalheri/validate-pyproject - rev: v0.16 + rev: v0.23 hooks: - id: validate-pyproject files: pyproject.toml$ - repo: https://github.com/rstcheck/rstcheck - rev: v6.2.1 + rev: v6.2.4 hooks: - id: rstcheck # https://github.com/rstcheck/rstcheck-core/issues/4 @@ -49,37 +49,37 @@ repos: additional_dependencies: ["rstcheck[sphinx]", "autodoc-pydantic==2.1.0"] - repo: https://github.com/MarketSquare/robotframework-tidy - rev: "4.11.0" + rev: "4.14.0" hooks: - id: robotidy - repo: https://github.com/jendrikseipp/vulture - rev: v2.11 + rev: v2.13 hooks: - id: vulture exclude: | /tests/ - repo: https://github.com/astral-sh/ruff-pre-commit - rev: "v0.3.5" + rev: "v0.8.1" hooks: - id: ruff - id: ruff-format - repo: https://github.com/asottile/pyupgrade - rev: v3.15.2 + rev: v3.19.0 hooks: - id: pyupgrade args: [--py310-plus] - repo: https://github.com/adamchainz/django-upgrade - rev: 1.16.0 + rev: 1.22.1 hooks: - id: django-upgrade args: [--target-version, "5.0"] - repo: https://github.com/pre-commit/mirrors-mypy - rev: v1.9.0 + rev: v1.13.0 hooks: - id: mypy additional_dependencies: @@ -106,7 +106,7 @@ repos: ) - repo: https://github.com/codespell-project/codespell - rev: v2.2.6 + rev: v2.3.0 hooks: - id: codespell additional_dependencies: ["tomli"] @@ -136,7 +136,7 @@ repos: ) - repo: https://github.com/Riverside-Healthcare/djLint - rev: v1.34.1 + rev: v1.36.3 hooks: - id: djlint-reformat-django files: | @@ -155,7 +155,7 @@ repos: exclude: '^rocky/rocky/templates/admin/.*\.html$' - repo: https://github.com/thibaudcolas/pre-commit-stylelint - rev: v16.3.1 + rev: v16.10.0 hooks: - id: stylelint args: [--fix] @@ -171,13 +171,13 @@ repos: args: ["-e", "SC1091"] - repo: https://github.com/scop/pre-commit-shfmt - rev: v3.8.0-1 + rev: v3.10.0-1 hooks: - id: shfmt args: ["-w", "-s", "-i", "4", "-sr"] - repo: https://github.com/pre-commit/mirrors-prettier - rev: v3.1.0 + rev: v4.0.0-alpha.8 hooks: - id: prettier additional_dependencies: diff --git a/boefjes/boefjes/plugins/helpers.py b/boefjes/boefjes/plugins/helpers.py index d897a182546..88405c6c672 100644 --- a/boefjes/boefjes/plugins/helpers.py +++ b/boefjes/boefjes/plugins/helpers.py @@ -54,18 +54,22 @@ def get_file_from_container(container: docker.models.containers.Container, path: logging.warning("%s not found in container %s %s", path, container.short_id, container.image.tags) return None - f = tarfile.open(mode="r|", fileobj=TarStream(stream).reader()) - tarobject = f.next() - if not tarobject or tarobject.name != os.path.basename(path): - logging.warning("%s not found in tarfile from container %s %s", path, container.short_id, container.image.tags) - return None + with tarfile.open(mode="r|", fileobj=TarStream(stream).reader()) as f: + tarobject = f.next() + if not tarobject or tarobject.name != os.path.basename(path): + logging.warning( + "%s not found in tarfile from container %s %s", path, container.short_id, container.image.tags + ) + return None - extracted_file = f.extractfile(tarobject) - if not extracted_file: - logging.warning("%s not found in tarfile from container %s %s", path, container.short_id, container.image.tags) - return None + extracted_file = f.extractfile(tarobject) + if not extracted_file: + logging.warning( + "%s not found in tarfile from container %s %s", path, container.short_id, container.image.tags + ) + return None - return extracted_file.read() + return extracted_file.read() def cpe_to_name_version(cpe: str) -> tuple[str | None, str | None]: diff --git a/boefjes/boefjes/plugins/kat_webpage_analysis/check_images/normalize.py b/boefjes/boefjes/plugins/kat_webpage_analysis/check_images/normalize.py index 810467e2398..420c994f1c2 100644 --- a/boefjes/boefjes/plugins/kat_webpage_analysis/check_images/normalize.py +++ b/boefjes/boefjes/plugins/kat_webpage_analysis/check_images/normalize.py @@ -54,5 +54,5 @@ def run(input_ooi: dict, raw: bytes) -> Iterable[NormalizerOutput]: yield Finding( finding_type=kat_ooi.reference, ooi=resource, - description="Image ended up bigger than %d Pixels, possible decompression Bomb" % image.MAX_IMAGE_PIXELS, + description=f"Image ended up bigger than {image.MAX_IMAGE_PIXELS} Pixels, possible decompression Bomb", ) diff --git a/boefjes/tests/integration/test_sql_repositories.py b/boefjes/tests/integration/test_sql_repositories.py index 0bd76c21995..1327e9b1389 100644 --- a/boefjes/tests/integration/test_sql_repositories.py +++ b/boefjes/tests/integration/test_sql_repositories.py @@ -53,7 +53,7 @@ def test_settings_storage(plugin_storage, organisation_storage, config_storage): with pytest.raises(ConfigNotFound): config_storage.delete("no organisation!", plugin_id) - assert {"TEST_SETTING": "123.9", "TEST_SETTING2": 13} == settings_storage.get_all_settings(org.id, plugin_id) + assert settings_storage.get_all_settings(org.id, plugin_id) == {"TEST_SETTING": "123.9", "TEST_SETTING2": 13} assert config_storage.get_all_settings(org.id, "wrong") == {} assert config_storage.get_all_settings("wrong", plugin_id) == {} @@ -91,14 +91,14 @@ def test_settings_storage_values_field_limits(plugin_storage, organisation_stora }, ) - assert { + assert settings_storage.get_all_settings(org.id, plugin_id) == { "TEST_SETTING": 12 * "123.9", "TEST_SETTING2": 12000, "TEST_SETTING3": 30 * "b", "TEST_SETTING4": 30 * "b", "TEST_SETTING5": 10 * "b", "TEST_SETTING6": 123456789, - } == settings_storage.get_all_settings(org.id, plugin_id) + } def test_plugin_enabled_storage(organisation_storage, plugin_storage, config_storage): diff --git a/boefjes/tests/katalogus/test_plugin_service.py b/boefjes/tests/katalogus/test_plugin_service.py index 4f1806f176a..9c28338fbb9 100644 --- a/boefjes/tests/katalogus/test_plugin_service.py +++ b/boefjes/tests/katalogus/test_plugin_service.py @@ -47,12 +47,12 @@ def test_update_by_id_bad_schema(mock_plugin_service, test_organisation): def test_get_schema(mock_plugin_service): schema = mock_plugin_service.schema("kat_test") - assert { + assert schema == { "title": "Arguments", "type": "object", "properties": {"api_key": {"title": "Api Key", "maxLength": 128, "type": "string"}}, "required": ["api_key"], - } == schema + } schema = mock_plugin_service.schema("kat_test_normalize") assert schema is None diff --git a/boefjes/tests/plugins/test_bodyimage.py b/boefjes/tests/plugins/test_bodyimage.py index 241c3cdb927..66ae459c947 100644 --- a/boefjes/tests/plugins/test_bodyimage.py +++ b/boefjes/tests/plugins/test_bodyimage.py @@ -48,7 +48,7 @@ def test_body_image_normalizer(normalizer_runner): output = normalizer_runner.run(meta, get_dummy_data("cat_image")).observations[0].results assert len(output) == 1 - assert { + assert output[0].dict() == { "object_type": "ImageMetadata", "primary_key": "ImageMetadata|internet|134.209.85.72|tcp|443|https|internet" "|mispo.es|https|internet|mispo.es|443|/", @@ -65,7 +65,7 @@ def test_body_image_normalizer(normalizer_runner): "size": (600, 600), "width": 600, }, - } == output[0].dict() + } def test_body_normalizer(normalizer_runner): diff --git a/boefjes/tests/plugins/test_calvin.py b/boefjes/tests/plugins/test_calvin.py index 46d07957702..e8a370a5726 100644 --- a/boefjes/tests/plugins/test_calvin.py +++ b/boefjes/tests/plugins/test_calvin.py @@ -8,7 +8,7 @@ def test_parse_user_changed(normalizer_runner): output = normalizer_runner.run(meta, get_dummy_data("user-changed.json")) assert len(output.declarations) == 8 - assert { + assert output.declarations[1].ooi.dict() == { "application": Application(name="organisation/env/app").reference, "event_id": '{"client_environment_app":"organisation/env/app","log_user_user_id":1234}-1655979300000', "event_title": "UC: User privilege monitoring", @@ -31,9 +31,9 @@ def test_parse_user_changed(normalizer_runner): "scan_profile": None, "user_id": None, "severity": "MEDIUM", - } == output.declarations[1].ooi.dict() + } - assert { + assert output.declarations[-1].ooi.dict() == { "application": Application(name="organisation/env/app").reference, "event_id": '{"client_environment_app":"organisation/env/app","log_user_user_id":1234}-1658825100000', "event_title": "UC: User privilege monitoring", @@ -56,7 +56,7 @@ def test_parse_user_changed(normalizer_runner): "scan_profile": None, "user_id": None, "severity": "MEDIUM", - } == output.declarations[-1].ooi.dict() + } def test_parse_admin_login_failure(normalizer_runner): @@ -64,7 +64,7 @@ def test_parse_admin_login_failure(normalizer_runner): output = normalizer_runner.run(meta, get_dummy_data("user-login-admin-failure.json")) assert len(output.declarations) == 8 - assert { + assert output.declarations[1].ooi.dict() == { "application": Application(name="organisation/env/app").reference, "event_id": '{"client_environment_app":"organisation/env/app","log_user_user_id":1234}-1659618600000', "event_title": "UC: Detect brute force login attempts for an admin account", @@ -89,7 +89,7 @@ def test_parse_admin_login_failure(normalizer_runner): "scan_profile": None, "user_id": None, "severity": "MEDIUM", - } == output.declarations[1].ooi.dict() + } def test_parse_user_login_failure(normalizer_runner): @@ -97,7 +97,7 @@ def test_parse_user_login_failure(normalizer_runner): output = normalizer_runner.run(meta, get_dummy_data("user-login-failure.json")) assert len(output.declarations) == 8 - assert { + assert output.declarations[1].ooi.dict() == { "application": Application(name="organisation/env/app").reference, "event_id": '{"client_environment_app":"organisation/env/app","log_user_user_id":1234}-1658998200000', "event_title": "UC: Detects attempts to guess passwords", @@ -123,4 +123,4 @@ def test_parse_user_login_failure(normalizer_runner): "scan_profile": None, "user_id": None, "severity": "MEDIUM", - } == output.declarations[1].ooi.dict() + } diff --git a/boefjes/tests/plugins/test_manual.py b/boefjes/tests/plugins/test_manual.py index 464bac5d620..14bd3ca7e53 100644 --- a/boefjes/tests/plugins/test_manual.py +++ b/boefjes/tests/plugins/test_manual.py @@ -46,27 +46,27 @@ def test_parse_manual_declarations(normalizer_runner): assert len(output.declarations) == 2 assert len(output.observations) == 0 - assert { + assert output.declarations[0].ooi.dict() == { "name": "net1", "object_type": "Network", "primary_key": "Network|net1", "scan_profile": None, "user_id": None, - } == output.declarations[0].ooi.dict() - assert { + } + assert output.declarations[1].ooi.dict() == { "name": "net2", "object_type": "Network", "primary_key": "Network|net2", "scan_profile": None, "user_id": None, - } == output.declarations[1].ooi.dict() + } def test_parse_manual_hostname_csv(normalizer_runner): meta, output, runner = check_network_created(normalizer_runner, 0) assert len(output.declarations) == 2 - assert { + assert output.declarations[1].ooi.dict() == { "dns_zone": None, "name": "example.com", "network": Reference("Network|internet"), @@ -75,12 +75,12 @@ def test_parse_manual_hostname_csv(normalizer_runner): "registered_domain": None, "scan_profile": None, "user_id": None, - } == output.declarations[1].ooi.dict() + } meta, output, runner = check_network_created(normalizer_runner, 1) assert len(output.declarations) == 2 - assert { + assert output.declarations[1].ooi.dict() == { "dns_zone": None, "name": "example.net", "network": Reference("Network|internet"), @@ -89,13 +89,13 @@ def test_parse_manual_hostname_csv(normalizer_runner): "registered_domain": None, "scan_profile": None, "user_id": None, - } == output.declarations[1].ooi.dict() + } def test_parse_manual_ip_csv(normalizer_runner): meta, output, runner = check_network_created(normalizer_runner, 2) assert len(output.declarations) == 6 - assert { + assert output.declarations[1].ooi.model_dump() == { "address": "1.1.1.1", "netblock": None, "network": Reference("Network|internet"), @@ -103,10 +103,10 @@ def test_parse_manual_ip_csv(normalizer_runner): "primary_key": "IPAddressV4|internet|1.1.1.1", "scan_profile": None, "user_id": None, - } == output.declarations[1].ooi.model_dump() + } meta, output, runner = check_network_created(normalizer_runner, 3) - assert { + assert output.declarations[1].ooi.model_dump() == { "address": "fe80:cd00:0:cde:1257:0:211e:729c", "netblock": None, "network": Reference("Network|internet"), @@ -114,14 +114,14 @@ def test_parse_manual_ip_csv(normalizer_runner): "primary_key": "IPAddressV6|internet|fe80:cd00:0:cde:1257:0:211e:729c", "scan_profile": None, "user_id": None, - } == output.declarations[1].ooi.model_dump() + } def test_parse_url_csv(normalizer_runner): meta, output, runner = check_network_created(normalizer_runner, 4) assert len(output.declarations) == 4 - assert { + assert output.declarations[1].ooi.model_dump() == { "network": Reference("Network|internet"), "object_type": "URL", "primary_key": "URL|internet|https://example.com/", @@ -129,11 +129,11 @@ def test_parse_url_csv(normalizer_runner): "scan_profile": None, "user_id": None, "web_url": None, - } == output.declarations[1].ooi.model_dump() + } meta, output, runner = check_network_created(normalizer_runner, 5) assert len(output.declarations) == 2 - assert { + assert output.declarations[1].ooi.dict() == { "network": Reference("Network|internet"), "object_type": "URL", "primary_key": "URL|internet|https://example.com/", @@ -141,7 +141,7 @@ def test_parse_url_csv(normalizer_runner): "scan_profile": None, "user_id": None, "web_url": None, - } == output.declarations[1].ooi.dict() + } def check_network_created( @@ -153,12 +153,12 @@ def check_network_created( output = normalizer_runner.run(meta, CSV_EXAMPLES[csv_idx]) assert len(output.observations) == 0 - assert { + assert output.declarations[0].ooi.dict() == { "name": "internet", "object_type": "Network", "primary_key": "Network|internet", "scan_profile": None, "user_id": None, - } == output.declarations[0].ooi.dict() + } return meta, output, runner diff --git a/cveapi/cveapi.py b/cveapi/cveapi.py index 92c5722a07f..09fcab2aa27 100644 --- a/cveapi/cveapi.py +++ b/cveapi/cveapi.py @@ -70,7 +70,7 @@ def run(): loglevel = os.getenv("CVEAPI_LOGLEVEL", "INFO") numeric_level = getattr(logging, loglevel.upper(), None) if not isinstance(numeric_level, int): - raise ValueError("Invalid log level: %s" % loglevel) + raise ValueError(f"Invalid log level: {loglevel}") logging.basicConfig(format="%(message)s", level=numeric_level) cveapi_dir = os.getenv("CVEAPI_DIR", "/var/lib/kat-cveapi") diff --git a/mula/scheduler/storage/filters/casting.py b/mula/scheduler/storage/filters/casting.py index cf3b1a98a54..921eb120a21 100644 --- a/mula/scheduler/storage/filters/casting.py +++ b/mula/scheduler/storage/filters/casting.py @@ -28,7 +28,7 @@ def cast_expression(expression: BinaryExpression, filter_: Filter) -> BinaryExpr raise MismatchedTypeError("List values must be of the same type") element_type = type(filter_.value[0]) - if element_type == str: + if element_type is str: expression = expression.astext elif element_type in [int, float]: expression = expression.cast(Numeric) diff --git a/octopoes/octopoes/core/service.py b/octopoes/octopoes/core/service.py index 9349afda974..135ad39f8ba 100644 --- a/octopoes/octopoes/core/service.py +++ b/octopoes/octopoes/core/service.py @@ -295,9 +295,7 @@ def recalculate_scan_profiles(self, valid_time: datetime) -> None: } temp_next_ooi_set = set() - for ooi_type_ in grouped_per_type: - current_ooi_set = grouped_per_type[ooi_type_] - + for ooi_type_, current_ooi_set in grouped_per_type.items(): # find paths to neighbours higher or equal than current processing level paths = get_paths_to_neighours(ooi_type_) paths = { diff --git a/octopoes/tests/test_bit_expiring_certificate.py b/octopoes/tests/test_bit_expiring_certificate.py index 95033aa321b..60c938cb21e 100644 --- a/octopoes/tests/test_bit_expiring_certificate.py +++ b/octopoes/tests/test_bit_expiring_certificate.py @@ -1,4 +1,5 @@ from _datetime import datetime, timedelta + from bits.expiring_certificate.expiring_certificate import run from octopoes.models.ooi.certificate import X509Certificate diff --git a/pyproject.toml b/pyproject.toml index ec710b355e1..021561d838d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -48,6 +48,7 @@ skip-magic-trailing-comma = true select = ["E", "F", "ERA", "W", "TID", "I", "G", "INP", "T20", "UP", "ISC", "PTH", "SIM", "PLC", "A", "S"] ignore = [ "A003", # Built-in shadowing is usually not a problem and some built-ins have very generic names + "SIM103", # Creating long return statements is often less readable "SIM108", # Ternary operator is not always more readable "S101", # Assert use is normal in pytest tests "S104", # Binding to all is normal in containers @@ -87,7 +88,7 @@ task-tags = ["Example", "todo", "TODO", "FIXME"] split-on-trailing-comma = false [tool.codespell] -ignore-words-list = 'edn,juxt' +ignore-words-list = 'edn,juxt,assertIn' [tool.poetry] name = "openkat" diff --git a/rocky/reports/report_types/dns_report/report.py b/rocky/reports/report_types/dns_report/report.py index 0fd6b62eb52..80885a1520b 100644 --- a/rocky/reports/report_types/dns_report/report.py +++ b/rocky/reports/report_types/dns_report/report.py @@ -35,7 +35,7 @@ def generate_data(self, input_ooi: str, valid_time: datetime) -> dict[str, Any]: for ooi_type, ooi in findings_tree.items(): if isinstance(ooi, Finding): for check in ["caa", "dkim", "dmarc", "dnssec", "spf"]: - if "NO-%s" % check.upper() in ooi.finding_type.tokenized.id: + if f"NO-{check.upper()}" in ooi.finding_type.tokenized.id: security[check] = False if ooi.finding_type.tokenized.id == "KAT-INVALID-SPF": security["spf"] = False diff --git a/rocky/reports/utils.py b/rocky/reports/utils.py index a88bed1864c..9b15e1aaade 100644 --- a/rocky/reports/utils.py +++ b/rocky/reports/utils.py @@ -20,7 +20,10 @@ class JSONEncoder(DjangoJSONEncoder): def default(self, o): if isinstance(o, OOI): return str(o) - elif dataclasses.is_dataclass(o): + elif dataclasses.is_dataclass(o) and not isinstance(o, type): + # is_dataclass return True if o is a dataclass or an instance, but + # asdict only accept instances, so we need to add the "not + # isinstance(o, type)" to make sure o is an instance not a class. return dataclasses.asdict(o) else: return super().default(o) diff --git a/rocky/rocky/views/ooi_detail_related_object.py b/rocky/rocky/views/ooi_detail_related_object.py index 66e1d30b3a3..badcbe6ecd2 100644 --- a/rocky/rocky/views/ooi_detail_related_object.py +++ b/rocky/rocky/views/ooi_detail_related_object.py @@ -30,11 +30,6 @@ def get_related_objects(self, observed_at): related.append(rel) return related - def split_ooi_type_choice(self, ooi_type_choice) -> dict[str, str]: - ooi_type = ooi_type_choice.split("|", 1) - - return {"ooi_type": ooi_type[0], "ooi_relation": ooi_type[1] if len(ooi_type) > 1 else None} - def ooi_add_url(self, ooi: OOI, ooi_type: str, ooi_relation: str = "ooi_id") -> str: """ When a user wants to add an OOI TYPE to another OOI TYPE object, it will @@ -128,9 +123,17 @@ def get(self, request, *args, **kwargs): self.ooi_id = self.get_ooi(pk=request.GET["ooi_id"]) if "add_ooi_type" in request.GET: - ooi_type_choice = self.split_ooi_type_choice(request.GET["add_ooi_type"]) - if existing_ooi_type(ooi_type_choice["ooi_type"]): - return redirect(self.ooi_add_url(self.ooi_id, **ooi_type_choice)) + if "|" in request.GET["add_ooi_type"]: + ooi_type, ooi_relation = request.GET["add_ooi_type"].split("|", 1) + else: + ooi_type = request.GET["add_ooi_type"] + ooi_relation = None + + if existing_ooi_type(ooi_type): + if ooi_relation: + return redirect(self.ooi_add_url(self.ooi_id, ooi_type, ooi_relation)) + else: + return redirect(self.ooi_add_url(self.ooi_id, ooi_type)) if "status_code" in kwargs: response = super().get(request, *args, **kwargs) diff --git a/rocky/tools/forms/ooi_form.py b/rocky/tools/forms/ooi_form.py index 3b6a259673e..82349ad9b4a 100644 --- a/rocky/tools/forms/ooi_form.py +++ b/rocky/tools/forms/ooi_form.py @@ -70,9 +70,9 @@ def generate_form_fields(self, hidden_ooi_fields: dict[str, str] | None = None) fields[name] = generate_ip_field(field) elif annotation == AnyUrl: fields[name] = generate_url_field(field) - elif annotation == dict or annotation == list[str] or annotation == dict[str, Any]: + elif annotation is dict or annotation == list[str] or annotation == dict[str, Any]: fields[name] = forms.JSONField(**default_attrs) - elif annotation == int or (hasattr(annotation, "__args__") and int in annotation.__args__): + elif annotation is int or (hasattr(annotation, "__args__") and int in annotation.__args__): fields[name] = forms.IntegerField(**default_attrs) elif isclass(annotation) and issubclass(annotation, Enum): fields[name] = generate_select_ooi_type(name, annotation, field) @@ -112,7 +112,7 @@ def generate_select_ooi_field( ) -> forms.fields.Field: # field is a relation, query all objects, and build select default_attrs = default_field_options(name, field) - is_multiselect = getattr(field.annotation, "__origin__", None) == list + is_multiselect = getattr(field.annotation, "__origin__", None) is list option_label = default_attrs.get("label", _("option")) option_text = "-- " + _("Optionally choose a {option_label}").format(option_label=option_label) + " --" diff --git a/rocky/tools/view_helpers.py b/rocky/tools/view_helpers.py index 64f79ae4d02..263512ca54c 100644 --- a/rocky/tools/view_helpers.py +++ b/rocky/tools/view_helpers.py @@ -67,7 +67,7 @@ def get_ooi_url(routename: str, ooi_id: str, organization_code: str, **kwargs) - return url_with_querystring(reverse(routename, kwargs={"organization_code": organization_code}), **kwargs) -def existing_ooi_type(ooi_type: str): +def existing_ooi_type(ooi_type: str) -> bool: if not ooi_type: return False