gforcada / flake8-builtins

Check for python builtins being used as variables or parameters
https://pypi.org/project/flake8-builtins
GNU General Public License v2.0
115 stars 24 forks source link

False positive with `TypedDict` #127

Open danjones1618 opened 5 months ago

danjones1618 commented 5 months ago

When using with a TypeDict like:

class PaginationDict(TypedDict):
     prev: Optional[str]
     next: Optional[str]
     count: int
     data: any

This will incorrectly flag: A003 class attribute "next" is shadowing a Python builtin

These checks should be disabled when a class inherits from TypedDict

gforcada commented 5 months ago

Hi @danjones1618 thanks for using flake8-builtins and even more to take the time to report issues about it! 👍🏾

Oh, I see, indeed, that's a problematic corner case.

I myself do not use the typing module or typing in general unfortunately.

You have, at least 3 options:

PaginationDict = TypeDict('PaginationDict', {'prev': Optional[str], 'next': Optional[str]})

Unfortunately I myself don't have much time free as of late...

danjones1618 commented 5 months ago

I've drafted a PR to start to fix this in #128 .

There is an edge case I foresee which will always raise a false positive. This is when the developer inhertis from a TypedDict that is defined in another file as we won't be able to derrive the inheritence tree. I think this is a fair edge case to not handle.

The implementation I drafted does not resolve inheritence at the moment. There is a seperate issue that comes to mind: it should validate that the TypedDict you inherit from does in fact come from the typing module.