Add type hints to src/palace/manager/api/admin/ code.#3046
Add type hints to src/palace/manager/api/admin/ code.#3046dbernstein wants to merge 2 commits intomainfrom
Conversation
cd03642 to
168c3c1
Compare
168c3c1 to
d4e43d6
Compare
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #3046 +/- ##
==========================================
- Coverage 93.04% 92.96% -0.09%
==========================================
Files 480 480
Lines 43716 43798 +82
Branches 6027 6034 +7
==========================================
+ Hits 40677 40715 +38
- Misses 1968 2005 +37
- Partials 1071 1078 +7 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
jonathangreen
left a comment
There was a problem hiding this comment.
This looks good! Need to address some of the Any type issues here though before it gets merged.
| @staticmethod | ||
| def forgot_password_template(redirect): | ||
| def forgot_password_template( | ||
| redirect: str | None | Response | WerkzeugResponse, |
There was a problem hiding this comment.
I think this should be str | None, we don't want to pass a Response into a jinja template do we?
| def reset_password_template( | ||
| reset_password_token: str, | ||
| admin_id: int, | ||
| redirect: str | None | Response | WerkzeugResponse, |
There was a problem hiding this comment.
| @@ -1,7 +1,9 @@ | |||
| # Decorators from palace.manager.api.routes and core.app_server are untyped. | |||
| # mypy: disallow_untyped_decorators=false | |||
There was a problem hiding this comment.
Instead of this, lets add types to these decorators
| ) -> Callable[..., Any]: | ||
| @wraps(f) | ||
| def decorated(*args, **kwargs): | ||
| def decorated(*args: Any, **kwargs: Any) -> Any: |
There was a problem hiding this comment.
For all of these decorators we should add actual types instead of defaulting to 'Any'.
circulation/src/palace/manager/api/admin/routes.py
Lines 47 to 65 in d4e43d6
| @app.route("/admin/sign_in_with_password", methods=["POST"]) | ||
| @returns_problem_detail | ||
| def password_auth(): | ||
| def password_auth() -> Any: |
There was a problem hiding this comment.
For all these routes, we should add types instead of defaulting to Any.
| @@ -1,4 +1,10 @@ | |||
| from __future__ import annotations | |||
|
|
|||
| # mypy: warn_unreachable=false | |||
There was a problem hiding this comment.
We shouldn't add this at the file level. If we need this ignore, lets add it line by line, so that we get warnings when its unnecessary. Ideally though, we wouldn't need this at all. usually this is a sign that something is incorrectly typed.
| self, | ||
| settings: list[dict[str, Any]], | ||
| value: str, | ||
| form: dict[str, Any] | None, |
There was a problem hiding this comment.
Minor: The form = form or {} on line 46 handles None, but the callers in validate_email (line 72) already do content.get("form") or {}, so None can never actually reach here. The | None is harmless but misleading.
| @@ -1,4 +1,4 @@ | |||
| body_style = """ | |||
| body_style: str = """ | |||
There was a problem hiding this comment.
Minor: Adding : str to every module-level string constant adds noise. Mypy already infers these as str. This isn't wrong, just unnecessary.
| return [_f for _f in result if _f] | ||
|
|
||
| def _value(self, field, form): | ||
| def _value(self, field: dict[str, Any], form: Any) -> Any: |
There was a problem hiding this comment.
Typing form as Any defeats the purpose. Any means mypy won't catch misuse.
|
|
||
| def _list_of_values(self, fields, form): | ||
| result = [] | ||
| def _list_of_values(self, fields: list[dict[str, Any]], form: Any) -> list[Any]: |
There was a problem hiding this comment.
Typing form as Any defeats the purpose. Any means mypy won't catch misuse.
Description
As part of the ongoing effort to add type hints everywhere. This PR adds type hints to everything below src/palace/manager/api/admin/ and tightens up an implementations that require changes due to type hint additions.
Motivation and Context
Long term team-wide effort.
How Has This Been Tested?
Tests pass. Mypy passes.
Checklist