From 4b7e60100a240eb03991333f17ce23ebffc1442e Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 31 Jul 2024 09:49:43 +0000 Subject: [PATCH 1/5] Bump pyright from 1.1.370 to 1.1.374 Bumps [pyright](https://github.com/RobertCraigie/pyright-python) from 1.1.370 to 1.1.374. - [Release notes](https://github.com/RobertCraigie/pyright-python/releases) - [Commits](https://github.com/RobertCraigie/pyright-python/compare/v1.1.370...v1.1.374) --- updated-dependencies: - dependency-name: pyright dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 867f923f..1102ea66 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -90,7 +90,7 @@ dev-dependencies = [ "junit2html >= 30.1,< 32.0", # Pyright >= 1.1.367 breaks the build. # Waiting for new pyright release to fix it. https://github.com/uclahs-cds/BL_Python/issues/79 - "pyright == 1.1.370", + "pyright == 1.1.374", "isort ~= 5.13", "ruff ~= 0.3", "bandit[sarif,toml] ~= 1.7" From 465f0838b58a28ed157f86ce786d52bf755526d2 Mon Sep 17 00:00:00 2001 From: Aaron Holmes Date: Tue, 6 Aug 2024 12:25:35 -0700 Subject: [PATCH 2/5] Resolve pyright error in SSM library. Fixes `ssm.py:132:36 - error: Argument expression after ** must be a mapping with a "str" key type (reportCallIssue)`. This is caused by attempted to unpack `**None`, which is a runtime error. Instead, check for `None` and bail out before the undesired runtime error. In either case, the behavior is the same - either Python will show the mapping error (before this fix) or it will show the "not loaded" error (after) this fix. The behavior of the SSM library itself is unchanged, so this is only to show a less disturbing message. Related #92 --- src/AWS/BL_Python/AWS/ssm.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/AWS/BL_Python/AWS/ssm.py b/src/AWS/BL_Python/AWS/ssm.py index 13c5438c..900e7ceb 100644 --- a/src/AWS/BL_Python/AWS/ssm.py +++ b/src/AWS/BL_Python/AWS/ssm.py @@ -124,7 +124,11 @@ def load_ssm_application_parameters( def load_config(self, config_type: type[TConfig]) -> TConfig | None: config: TConfig | None = None try: - ssm_parameters: AnyDict | None = self.load_ssm_application_parameters() + ssm_parameters = self.load_ssm_application_parameters() + + if ssm_parameters is None: + raise Exception("SSM parameters were not loaded.") + config = config_type(**ssm_parameters) except: if not self._continue_on_ssm_failure: From acafcbd2a8a28003cee688057380ccff49c05168 Mon Sep 17 00:00:00 2001 From: Aaron Holmes Date: Tue, 6 Aug 2024 14:46:12 -0700 Subject: [PATCH 3/5] Remove unnecessary type ignores for pyright. Related #92 --- src/web/BL_Python/web/scaffolding/scaffolder.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/web/BL_Python/web/scaffolding/scaffolder.py b/src/web/BL_Python/web/scaffolding/scaffolder.py index 0afab516..0cbaaf50 100644 --- a/src/web/BL_Python/web/scaffolding/scaffolder.py +++ b/src/web/BL_Python/web/scaffolding/scaffolder.py @@ -196,9 +196,7 @@ def _render_template( template = template_environment.get_template(template_name) if write_rendered_template: - template.stream( # pyright: ignore[reportUnknownMemberType] - **template_config - ).dump(str(template_output_path)) + template.stream(**template_config).dump(str(template_output_path)) else: template_stream = template.stream(**template_config) while next(template_stream, None): @@ -252,14 +250,12 @@ def _execute_module_hooks(self, module_template_directory: str): `on_create(config: dict[str, Any], log: Logger) -> None` """ module_hook_path = Path(module_template_directory, "__hook__.py") - if self._provider.has_resource( # pyright: ignore[reportUnknownMemberType] - str(module_hook_path) - ): + if self._provider.has_resource(str(module_hook_path)): # load the module from its path # and execute it spec = spec_from_file_location( "__hook__", - self._provider.get_resource_filename( # pyright: ignore[reportUnknownArgumentType,reportUnknownMemberType] + self._provider.get_resource_filename( self._manager, str(module_hook_path) ), ) @@ -338,7 +334,7 @@ def _scaffold_modules(self, overwrite_existing_files: bool = True): # all modules contain templates under templates/ # but only modules with __meta__.j2 control how # their own templates are rendered. - if self._provider.has_resource( # pyright: ignore[reportUnknownMemberType] + if self._provider.has_resource( str(Path(module_template_directory, "__meta__.j2")) ): module_env = Environment( From adf62c171d715ac57c636bac15da9b23f2e30d33 Mon Sep 17 00:00:00 2001 From: Aaron Holmes Date: Tue, 6 Aug 2024 14:46:43 -0700 Subject: [PATCH 4/5] Fix consistency issue between pyright and pylance. Related #92 --- .../BL_Python/database/migrations/alembic/bl_alembic.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/database/BL_Python/database/migrations/alembic/bl_alembic.py b/src/database/BL_Python/database/migrations/alembic/bl_alembic.py index 4b3878eb..7bb7ffba 100644 --- a/src/database/BL_Python/database/migrations/alembic/bl_alembic.py +++ b/src/database/BL_Python/database/migrations/alembic/bl_alembic.py @@ -2,7 +2,7 @@ from logging import Logger from pathlib import Path from types import TracebackType -from typing import Callable +from typing import BinaryIO, Callable, cast import alembic.util.messaging @@ -228,7 +228,7 @@ def _copy_files(self, files: list[FileCopy], force_overwrite: bool = False): open(file.source, "r") as source, open(file.destination, write_mode) as destination, ): - destination.writelines(source.buffer) + destination.writelines(cast(BinaryIO, source.buffer)) # pyright: ignore[reportUnnecessaryCast] Disagreement between pyright and pylance. Pyright things this is a different type, and pylance doesn't like the cast. except FileExistsError as e: if e.filename != str(file.destination): raise From 6f3675bd5def3d6700da01869f03d39841fc8049 Mon Sep 17 00:00:00 2001 From: Aaron Holmes Date: Tue, 6 Aug 2024 14:47:26 -0700 Subject: [PATCH 5/5] Remove variable imports causing type alias errors. Related #92 --- src/web/BL_Python/web/middleware/__init__.py | 14 ++++++-------- src/web/BL_Python/web/middleware/flask/__init__.py | 14 ++++++-------- .../BL_Python/web/middleware/openapi/__init__.py | 14 ++++++-------- 3 files changed, 18 insertions(+), 24 deletions(-) diff --git a/src/web/BL_Python/web/middleware/__init__.py b/src/web/BL_Python/web/middleware/__init__.py index 09cde079..828c151f 100644 --- a/src/web/BL_Python/web/middleware/__init__.py +++ b/src/web/BL_Python/web/middleware/__init__.py @@ -1,6 +1,6 @@ import json from logging import Logger -from typing import Awaitable, Callable, TypeVar +from typing import Awaitable, Callable, TypeAlias, TypeVar from BL_Python.web.middleware.flask import ( register_flask_api_request_handlers, @@ -13,11 +13,7 @@ ) from connexion import FlaskApp from flask import Flask, Response -from flask.typing import ( - AfterRequestCallable, - BeforeRequestCallable, - ResponseReturnValue, -) +from flask.typing import ResponseReturnValue from werkzeug.exceptions import HTTPException, Unauthorized # pyright: reportUnusedFunction=false @@ -26,8 +22,10 @@ # Fixes type problems when using @inject with @app.before_request and @app.after_request. # The main difference with these types as opposed to the Flask-defined types is that # these types allow the handler to take any arguments, versus no arguments or just Response. -AfterRequestCallable = Callable[..., Response] | Callable[..., Awaitable[Response]] -BeforeRequestCallable = ( +AfterRequestCallable: TypeAlias = ( + Callable[..., Response] | Callable[..., Awaitable[Response]] +) +BeforeRequestCallable: TypeAlias = ( Callable[..., ResponseReturnValue | None] | Callable[..., Awaitable[ResponseReturnValue | None]] ) diff --git a/src/web/BL_Python/web/middleware/flask/__init__.py b/src/web/BL_Python/web/middleware/flask/__init__.py index d0ba38bd..515ee2af 100644 --- a/src/web/BL_Python/web/middleware/flask/__init__.py +++ b/src/web/BL_Python/web/middleware/flask/__init__.py @@ -1,17 +1,13 @@ import re import uuid from logging import Logger -from typing import Awaitable, Callable, Dict, TypeVar +from typing import Awaitable, Callable, Dict, TypeAlias, TypeVar from uuid import uuid4 import json_logging from connexion import FlaskApp from flask import Flask, Request, Response, request -from flask.typing import ( - AfterRequestCallable, - BeforeRequestCallable, - ResponseReturnValue, -) +from flask.typing import ResponseReturnValue from injector import inject from ...config import Config @@ -35,8 +31,10 @@ # Fixes type problems when using @inject with @app.before_request and @app.after_request. # The main difference with these types as opposed to the Flask-defined types is that # these types allow the handler to take any arguments, versus no arguments or just Response. -AfterRequestCallable = Callable[..., Response] | Callable[..., Awaitable[Response]] -BeforeRequestCallable = ( +AfterRequestCallable: TypeAlias = ( + Callable[..., Response] | Callable[..., Awaitable[Response]] +) +BeforeRequestCallable: TypeAlias = ( Callable[..., ResponseReturnValue | None] | Callable[..., Awaitable[ResponseReturnValue | None]] ) diff --git a/src/web/BL_Python/web/middleware/openapi/__init__.py b/src/web/BL_Python/web/middleware/openapi/__init__.py index c4b8d6b7..c790ef9d 100644 --- a/src/web/BL_Python/web/middleware/openapi/__init__.py +++ b/src/web/BL_Python/web/middleware/openapi/__init__.py @@ -4,7 +4,7 @@ from contextlib import ExitStack from contextvars import Token from logging import Logger -from typing import Any, Awaitable, Callable, Literal, TypeVar, cast +from typing import Any, Awaitable, Callable, Literal, TypeAlias, TypeVar, cast from uuid import uuid4 import json_logging @@ -18,11 +18,7 @@ from flask.ctx import AppContext from flask.globals import _cv_app # pyright: ignore[reportPrivateUsage] from flask.globals import current_app -from flask.typing import ( - AfterRequestCallable, - BeforeRequestCallable, - ResponseReturnValue, -) +from flask.typing import ResponseReturnValue from flask_login import AnonymousUserMixin, current_user from injector import inject from starlette.datastructures import Address @@ -46,8 +42,10 @@ # Fixes type problems when using @inject with @app.before_request and @app.after_request. # The main difference with these types as opposed to the Flask-defined types is that # these types allow the handler to take any arguments, versus no arguments or just Response. -AfterRequestCallable = Callable[..., Response] | Callable[..., Awaitable[Response]] -BeforeRequestCallable = ( +AfterRequestCallable: TypeAlias = ( + Callable[..., Response] | Callable[..., Awaitable[Response]] +) +BeforeRequestCallable: TypeAlias = ( Callable[..., ResponseReturnValue | None] | Callable[..., Awaitable[ResponseReturnValue | None]] )