You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# coding: utf-8"""Example script - workable case."""fromtypingimportCallable, Dict, Tupledefsome_closure() ->Tuple[Callable[[str], int], Callable[[str, int], None]]:
"""Toy closure example."""mydict= {} # type: Dict[str, int]defread_value(key: str) ->int:
"""Read a value from the state dict."""nonlocalmydictreturnmydict[key]
defwrite_value(key: str, value: int) ->None:
"""Set a value in the state dict."""nonlocalmydictmydict[key] =valuereturnread_value, write_value
Lint this modified script. Witness W0611: Unused numpy imported as np (unused-import)
# coding: utf-8"""Example script - warning-triggering case."""fromtypingimportCallable, Dict, Tuple, Unionimportnumpyasnp# type: ignoredefsome_closure() ->Tuple[Callable[[str], int], Callable[[str, int], None]]:
"""Toy closure example."""mydict= {} # type: Dict[str, Union[int, np.ndarray]]defread_value(key: str) ->int:
"""Read a value from the state dict."""nonlocalmydictreturnmydict[key]
defwrite_value(key: str, value: int) ->None:
"""Set a value in the state dict."""nonlocalmydictmydict[key] =valuereturnread_value, write_value
Current behavior
In the first example, pylint analyzes the comment type annotation correctly, and does not complain about typing.Dict being imported (it would if the comment was removed, but then mypy would complain about the lack of type annotation for the empty-initialized dict).
In the second example, pylint still analyzes the comment type annotation and does not complain about Dict and Union being imported from typing, however it does complain about the numpy import, whereas the latter is also done so as to specify the type annotation.
This toy example may seem irrelevant (and indeed, the type annotation is here incorrect), but it emerges in my actual code when using imports within a package (e.g. the function used to add values in a dict attribute is imported from another code file, and returns a custom type ; I therefore import said type to type-annotate the dict which is bound to be assigned values at some point, and get pylint warnings).
Expected behavior
Ideally, pylint would detect third-party types imports used in type annotations, in addition to the basic typing types. Of course, warnings can be disabled by adding pylint comments in code files, which might be the right solution if implementing a fix proves too hard, but I still thought it was worth signalling.
@DaniGuardiola It seems that the issue was fixed on master branch by @PCManticore (thanks!), however this was done after the 2.4.4 release - hence it should be included in the next version.
Steps to reproduce
W0611: Unused numpy imported as np (unused-import)
Current behavior
In the first example, pylint analyzes the comment type annotation correctly, and does not complain about
typing.Dict
being imported (it would if the comment was removed, but then mypy would complain about the lack of type annotation for the empty-initialized dict).In the second example, pylint still analyzes the comment type annotation and does not complain about
Dict
andUnion
being imported fromtyping
, however it does complain about thenumpy
import, whereas the latter is also done so as to specify the type annotation.This toy example may seem irrelevant (and indeed, the type annotation is here incorrect), but it emerges in my actual code when using imports within a package (e.g. the function used to add values in a dict attribute is imported from another code file, and returns a custom type ; I therefore import said type to type-annotate the dict which is bound to be assigned values at some point, and get pylint warnings).
Expected behavior
Ideally, pylint would detect third-party types imports used in type annotations, in addition to the basic
typing
types. Of course, warnings can be disabled by adding pylint comments in code files, which might be the right solution if implementing a fix proves too hard, but I still thought it was worth signalling.pylint --version output
The text was updated successfully, but these errors were encountered: