Skip to content

simplify _is_bad_TypedDict #521

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 26, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions flake8_pyi/visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand All @@ -442,17 +436,15 @@ 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)
and type(key.value) is str
and key.value.isidentifier()
and not iskeyword(key.value)
):
return False
return True


def _is_assignment_which_must_have_a_value(
Expand Down