mirror of
https://github.com/ansible/awx.git
synced 2026-05-12 11:57:37 -02:30
Fix type hints in gateway_mapping.py process_sso_user_list function (#7060)
- Added missing Pattern and Any imports from typing - Fixed users parameter type hint to include Pattern[str] - Simplified overly complex return type annotation to use Any - Added proper type narrowing with isinstance() and cast() - Resolved mypy errors about incompatible list item types Co-authored-by: Claude (Anthropic AI Assistant) <claude@anthropic.com>
This commit is contained in:
committed by
thedoubl3j
parent
5a89d7bc29
commit
df1c453c37
@@ -6,12 +6,12 @@ This module contains functions to convert AWX authentication mappings
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import re
|
import re
|
||||||
from typing import Union
|
from typing import Union, Pattern, Any, cast
|
||||||
|
|
||||||
email_regex = re.compile(r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$")
|
email_regex = re.compile(r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$")
|
||||||
|
|
||||||
|
|
||||||
def pattern_to_slash_format(pattern):
|
def pattern_to_slash_format(pattern: Any) -> str:
|
||||||
"""Convert a re.Pattern object to /pattern/flags format."""
|
"""Convert a re.Pattern object to /pattern/flags format."""
|
||||||
if not isinstance(pattern, re.Pattern):
|
if not isinstance(pattern, re.Pattern):
|
||||||
return str(pattern)
|
return str(pattern)
|
||||||
@@ -30,23 +30,26 @@ def pattern_to_slash_format(pattern):
|
|||||||
|
|
||||||
|
|
||||||
def process_sso_user_list(
|
def process_sso_user_list(
|
||||||
users: Union[str, list[str], bool], email_attr: str = 'email', username_attr: str = 'username'
|
users: Union[str, bool, Pattern[str], list[Union[str, bool, Pattern[str]]]], email_attr: str = 'email', username_attr: str = 'username'
|
||||||
) -> list[dict[str : Union[str, dict[str : dict[str:str]]]]]:
|
) -> list[dict[str, Any]]:
|
||||||
if type(users) is str:
|
if not isinstance(users, list):
|
||||||
users = [users]
|
users = [users]
|
||||||
|
|
||||||
|
# Type cast to help mypy understand the type after conversion
|
||||||
|
user_list: list[Union[str, bool, Pattern[str]]] = cast(list[Union[str, bool, Pattern[str]]], users)
|
||||||
|
|
||||||
triggers = []
|
triggers = []
|
||||||
if users in [False, ["false"]]:
|
if user_list == ["false"] or user_list == [False]:
|
||||||
triggers.append(
|
triggers.append(
|
||||||
{
|
{
|
||||||
"name": "Never Allow",
|
"name": "Never Allow",
|
||||||
"trigger": {"never": {}},
|
"trigger": {"never": {}},
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
elif users in [True, ["true"]]:
|
elif user_list == ["true"] or user_list == [True]:
|
||||||
triggers.append({"name": "Always Allow", "trigger": {"always": {}}})
|
triggers.append({"name": "Always Allow", "trigger": {"always": {}}})
|
||||||
else:
|
else:
|
||||||
for user_or_email in users:
|
for user_or_email in user_list:
|
||||||
if isinstance(user_or_email, re.Pattern):
|
if isinstance(user_or_email, re.Pattern):
|
||||||
user_or_email = pattern_to_slash_format(user_or_email)
|
user_or_email = pattern_to_slash_format(user_or_email)
|
||||||
# If we got a regex it could be either a username or an email object
|
# If we got a regex it could be either a username or an email object
|
||||||
|
|||||||
Reference in New Issue
Block a user