Skip to content

Commit

Permalink
Convert to future annotations and assignment expressions. (#918)
Browse files Browse the repository at this point in the history
Simple, no functional changes.
  • Loading branch information
jwag956 authored Feb 10, 2024
1 parent 3b84c92 commit dc6c828
Show file tree
Hide file tree
Showing 26 changed files with 322 additions and 329 deletions.
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@
("py:class", "OAuth"),
("py:class", "OAuthError"),
("py:class", "authlib.integrations.flask_client.OAuth"),
("py:class", "TotpMatch"),
("py:class", "t.Type"),
("py:class", "t.Callable"),
("py:class", "t.Tuple"),
("py:class", "t.Any"),
("py:class", "timedelta"),
]
Expand Down
2 changes: 1 addition & 1 deletion docs/models.rst
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ the User record (since we need to look up the ``User`` based on a WebAuthn ``cre
Add the following to the WebAuthn model:

user = ReferenceField("User")
def get_user_mapping(self) -> t.Dict[str, str]:
def get_user_mapping(self) -> dict[str, str]:
"""Return the mapping from webauthn back to User"""
return dict(id=self.user.id)

Expand Down
8 changes: 5 additions & 3 deletions flask_security/changeable.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
Flask-Security change password module
:copyright: (c) 2012 by Matt Wright.
:copyright: (c) 2019-2022 by J. Christopher Wagner (jwag).
:copyright: (c) 2019-2024 by J. Christopher Wagner (jwag).
:author: Eskil Heyn Olsen
:license: MIT, see LICENSE for more details.
"""

from __future__ import annotations

import typing as t

from flask import current_app, request, session
Expand All @@ -34,7 +36,7 @@ def send_password_changed_notice(user):


def change_user_password(
user: "User", password: t.Optional[str], notify: bool = True, autologin: bool = True
user: User, password: str | None, notify: bool = True, autologin: bool = True
) -> None:
"""Change the specified user's password
Expand Down Expand Up @@ -71,7 +73,7 @@ def change_user_password(
)


def admin_change_password(user: "User", new_passwd: str, notify: bool = True) -> None:
def admin_change_password(user: User, new_passwd: str, notify: bool = True) -> None:
"""
Administratively change a user's password.
Note that this will immediately render the user's existing sessions (and possibly
Expand Down
27 changes: 8 additions & 19 deletions flask_security/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@
csrf_cookie_handler,
default_render_template,
default_want_json,
get_config,
get_identity_attribute,
get_identity_attributes,
get_message,
Expand Down Expand Up @@ -906,7 +905,7 @@ def has_permission(self, permission: str) -> bool:
return True
return False

def get_security_payload(self) -> t.Dict[str, t.Any]:
def get_security_payload(self) -> dict[str, t.Any]:
"""Serialize user object as response payload.
Override this to return any/all of the user object in JSON responses.
Return a dict.
Expand Down Expand Up @@ -1340,7 +1339,6 @@ def init_app(
# Override default forms
# BC - kwarg value here overrides init/constructor time
# BC - we allow forms to be set from config
# Can't wait for assignment expressions.
form_names = [
"login_form",
"verify_form",
Expand Down Expand Up @@ -1369,10 +1367,9 @@ def init_app(
"wan_verify_form",
]
for form_name in form_names:
form_cls = kwargs.get(
form_name, app.config.get(f"SECURITY_{form_name.upper()}", None)
)
if form_cls:
if form_cls := kwargs.get(
form_name, cv(form_name.upper(), app, strict=False)
):
self.forms[form_name].cls = form_cls

# The following will be set as attributes and initialized from either
Expand All @@ -1395,12 +1392,8 @@ def init_app(
"webauthn_util_cls",
]
for attr in attr_names:
if kwargs.get(attr, None):
setattr(self, attr, kwargs.get(attr))

for key, value in get_config(app).items():
if key.lower() in attr_names:
setattr(self, key.lower(), value)
if ov := kwargs.get(attr, cv(attr.upper(), app, strict=False)):
setattr(self, attr, ov)

identity_loaded.connect_via(app)(_on_identity_loaded)

Expand Down Expand Up @@ -1530,13 +1523,9 @@ def init_app(
if hasattr(app, "cli"):
from .cli import users, roles

# Waiting for 3.8 assignment expressions
un = cv("CLI_USERS_NAME", app, strict=True)
rn = cv("CLI_ROLES_NAME", app, strict=True)

if un:
if un := cv("CLI_USERS_NAME", app, strict=True):
app.cli.add_command(users, un)
if rn:
if rn := cv("CLI_ROLES_NAME", app, strict=True):
app.cli.add_command(roles, rn)

# Migrate from TWO_FACTOR config to generic config.
Expand Down
Loading

0 comments on commit dc6c828

Please sign in to comment.