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
Check that collection generics are parametrised #118
Comments
I don't think this can be sufficiently well implemented within Flake8's framework without magic, since we have no ability to trace imports between files. Even if imports were just ignored, this plugin currently has no mechanism for tracking local state, and likely isn't worth the complexity, which is why #98 and #99 use config magic to accomplish their respective tasks. I can sympathize with the motivation, but I think if you're reaching for something like this you're better off with mypy, which has a |
Thanks for the quick response @sco1! I wasn't aware of This is good to close then, thanks for the tip! |
Just out of curiosity – I also saw https://github.com/best-doctor/flake8-annotations-complexity which intuitively should have access to the kind of information we would need here. I suppose that the AST helpers from that project count as the magic you were referring to? Disclaimer: I haven't read carefully the code of neither this nor that plugin. |
Consider the following: import typing as t
SOME_TYPE = t.List[int]
def foo() -> t.List[int]:
...
def bar() -> SOME_TYPE:
... Our returns end up looking something like: # foo
Subscript(value=Attribute(value=Name(id='t', ctx=Load()), attr='List', ctx=Load()), slice=Name(id='int', ctx=Load()), ctx=Load())
# bar
Name(id='SOME_TYPE', ctx=Load()) For
|
Description
Introduce warnings checking that collection generics are parametrised (even just by
typing.Any
).I'm happy to work on it, but I would like to first hear feedback if this check makes sense, if yes what generics should be covered, should it be disabled by default for backwards compatibility and lastly – what are some good commits that I can follow to see how to implement it.
Rationale/Use Case
I often see people annotate their code with really unhelpful types such as
list
,typing.List
,dict
ortyping.Dict
purely to satisfyflake8-annotations
warnings.The idea is to nudge the developer into considering how their generic will be parametrised.
I.e. this would fail:
But that would be fine:
Of course, the idea is that this would make the developer consider declaring the type parameters even more precisely, e.g.:
Or even:
CC: @flaviojdz – we spoke about it a few months ago!
The text was updated successfully, but these errors were encountered: