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 option to ignore code in if TYPE_CHECKING #3285

Closed
GoldsteinE opened this issue Dec 3, 2019 · 6 comments · Fixed by #4702
Closed

Add option to ignore code in if TYPE_CHECKING #3285

GoldsteinE opened this issue Dec 3, 2019 · 6 comments · Fixed by #4702
Labels
Enhancement ✨ Improvement to a component Help wanted 🙏 Outside help would be appreciated, good for new contributors High priority Issue with more than 10 reactions

Comments

@GoldsteinE
Copy link

Is your feature request related to a problem? Please describe

In typed code, this is common pattern:

from typing import TYPE_CHECKING
if TYPE_CHECKING:
    from some_module import some_type

If some_type is defined in stub, it doesn't have to exist in runtime (e. g. wsgiref.types is only available in .pyi files). Pylint issues an error for this code, while it's perfectly valid.
Pylint isn't a type checker, so it should ignore all code in if TYPE_CHECKING:.

Describe the solution you'd like

Add option like to ignore all errors or, probably, only import-related errors in if TYPE_CHECKING: blocks.

@PCManticore
Copy link
Contributor

Thanks, agree we should take more hints for TYPE_CHECKING.

@dpinol
Copy link

dpinol commented Jan 12, 2021

I'm trying to preprocess the source code commenting our the imports guarded with TYPE_CHECKING, and then removing types with https://github.com/abarker/strip-hints. Otherwise, pylint would complain of the guarded types used in type hints.
unfortunately strip-hints also removes the types of dataclass fields, and hence I'm writing my own preprocessor :-|

@Pierre-Sassoulas Pierre-Sassoulas added the High priority Issue with more than 10 reactions label May 30, 2021
@ssbarnea
Copy link
Contributor

That bug allone makes it very inconvenient to keep using pylint on types projects, to a point where I am even considering removing it. Any chance we can have someone a patch to address it?

@Pierre-Sassoulas Pierre-Sassoulas changed the title Add option to ignore code in if TYPE_CHECKING Add option to ignore code in if TYPE_CHECKING Jun 30, 2021
@Pierre-Sassoulas
Copy link
Member

This is a high priority issue but no one is working on it right now, if you're willing to contribute it could go faster 😄

@Pierre-Sassoulas Pierre-Sassoulas pinned this issue Jul 1, 2021
@Pierre-Sassoulas Pierre-Sassoulas added the Help wanted 🙏 Outside help would be appreciated, good for new contributors label Jul 1, 2021
@matusvalo
Copy link
Collaborator

Just a note: As a workaround, disable all checks for a block should help.

from typing import TYPE_CHECKING
if TYPE_CHECKING:
    # pylint: disable=all
    from some_module import some_type

@Pierre-Sassoulas
Copy link
Member

Thanks for the clever trick @matusvalo ! For readers who also are would be implementer, this workaround would not work for circular imports for example, so we still need this feature very badly :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement ✨ Improvement to a component Help wanted 🙏 Outside help would be appreciated, good for new contributors High priority Issue with more than 10 reactions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants