Replace relative imports with absolute when TYPE_CHECKING to fix mypy #1766
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #1765
Feature Summary and Justification
The problem
Fixes the ability to statically type check PRAW's code that was apparently broken by #1676 and never caught, since there is apparently no continuous type checking. In summary, 37 files contain
if TYPE_CHECKINGblocks with imports ofprawthat only are considered statically by the type checker, which are relative imports that traverse outside the package. This causes mypy, stubgen, etc. runs to fail with a critical error, as traversal outside a Python package is not valid for relative imports.The solution
Instead, the exact same intended effect (exposing the top level
prawpackage as a valid name to the type checker) can be achieved by using a shorter absolute import (import praw), which has the added benefit of not breaking if the file is ever moved (as one apparently was). This has no effect at runtime or in any context but static type checking, givenTYPE_CHECKINGis always false otherwise and thus the blocks will not execute.Additional tests performed
In addition to the automated tests with this PR, I also tested this locally by multiple means:
pip install -e .worked finepy.typedactive in the root of the PRAW package worked with no errors or messagesFuture work
I was originally going to add a
py.typedfile in this PR per PEP 561 to enable type checking PRAW-using applications against the library, but I figure its best to leave that to a separate PR to keep this one's scope as narrow as possible. I also hope to clean up the remaining type errors in PRAW, add type hints to prawcore and (if desired) add type checking to the CIs and/or locally to catch errors and avoid future regressions like this one.