Skip to content
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

Add support for TypedDict #128

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion flake8_builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ def run(self):

def check_assignment(self, statement):
msg = self.assign_msg
if type(statement.__flake8_builtins_parent) is ast.ClassDef:
if isinstance(statement.__flake8_builtins_parent, ast.ClassDef):
if "TypedDict" in {a.id for a in statement.__flake8_builtins_parent.bases if isinstance(a, ast.Name)}:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danjones1618 do we expect other typing classes to be used eventually here? 🤔

Given how long this conditional is, it might be worth refactoring into a function?

def base_classes_ids(self, statement):
    return {a.id for a in statement.__flake8_builtins_parent.bases if isinstance(a, ast.Name)}

return
msg = self.class_attribute_msg

if isinstance(statement, ast.Assign):
Expand Down
74 changes: 74 additions & 0 deletions run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,3 +529,77 @@ def test_module_name_ignore_module():
def test_module_name_not_builtin():
source = ''
check_code(source, filename='log_config')



@pytest.mark.skipif(
sys.version_info < (3, 9),
reason='Typed dicts are introduced in Python 3.9',
)
def test_typed_dicts_dont_shadow_builtins():
source = """
from typing import Any, TypedDict


class PaginatedResponse(TypedDict):
count: int
next: str
prev: str
data: list[Any]
"""
check_code(source)


@pytest.mark.xfail(reason="N-deep inheritence not supported")
@pytest.mark.skipif(
sys.version_info < (3, 9),
reason='Typed dicts are introduced in Python 3.9',
)
def test_inherited_typed_dicts_dont_shadow_builtins():
source = """
from typing import Any, TypedDict


class ApiResponseDict(TypedDict):
status: int
okay: bool


class PaginatedResponse(ApiResponseDict):
count: int
next: str
prev: str
data: list[Any]
"""
check_code(source)


@pytest.mark.xfail(reason="N-deep inheritence not supported")
@pytest.mark.skipif(
sys.version_info < (3, 9),
reason='Typed dicts are introduced in Python 3.9',
)
def test_n_deep_inherited_typed_dicts_dont_shadow_builtins():
source = """
from typing import Any, TypedDict


class ApiResponseDict(TypedDict):
status: int
okay: bool


class Something(ApiResponseDict):
okay: int


class SomethingElse(Something):
is_an_oof: bool

class PaginatedResponse(SomethingElse):
count: int
next: str
prev: str
data: list[Any]
"""
check_code(source)
Loading