From feb231d5e1dbf6f83beb9cfe96daf0944077d38b Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Mon, 26 May 2025 12:18:39 -0700 Subject: [PATCH 1/2] simplify _is_bad_TypedDict --- flake8_pyi/visitor.py | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/flake8_pyi/visitor.py b/flake8_pyi/visitor.py index a4835f1..8c70e89 100644 --- a/flake8_pyi/visitor.py +++ b/flake8_pyi/visitor.py @@ -416,12 +416,6 @@ def _unparse_func_node(node: ast.FunctionDef | ast.AsyncFunctionDef) -> str: return re.sub(r"\s+", " ", ast.unparse(node)) -def _is_list_of_str_nodes(seq: list[ast.expr | None]) -> TypeGuard[list[ast.Constant]]: - return all( - isinstance(item, ast.Constant) and type(item.value) is str for item in seq - ) - - def _is_bad_TypedDict(node: ast.Call) -> bool: """Should the assignment-based TypedDict `node` be rewritten using class syntax? @@ -442,17 +436,12 @@ def _is_bad_TypedDict(node: ast.Call) -> bool: if not isinstance(typed_dict_annotations, ast.Dict): return False - typed_dict_fields = typed_dict_annotations.keys - - if not _is_list_of_str_nodes(typed_dict_fields): - return False - - fieldnames = [field.value for field in typed_dict_fields] - - return all( - fieldname.isidentifier() and not iskeyword(fieldname) - for fieldname in fieldnames - ) + for key in typed_dict_annotations.keys: + if not isinstance(key, ast.Constant) or type(key.value) is not str: + return False + if not key.value.isidentifier() or iskeyword(key.value): + return False + return True def _is_assignment_which_must_have_a_value( From a7701bf9b0a072a9b8634000fdf3ba58bf86bb82 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Mon, 26 May 2025 12:28:50 -0700 Subject: [PATCH 2/2] Update flake8_pyi/visitor.py Co-authored-by: Alex Waygood --- flake8_pyi/visitor.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/flake8_pyi/visitor.py b/flake8_pyi/visitor.py index 8c70e89..e2fd4b2 100644 --- a/flake8_pyi/visitor.py +++ b/flake8_pyi/visitor.py @@ -437,9 +437,12 @@ def _is_bad_TypedDict(node: ast.Call) -> bool: return False for key in typed_dict_annotations.keys: - if not isinstance(key, ast.Constant) or type(key.value) is not str: - return False - if not key.value.isidentifier() or iskeyword(key.value): + if not ( + isinstance(key, ast.Constant) + and type(key.value) is str + and key.value.isidentifier() + and not iskeyword(key.value) + ): return False return True