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

feat: Support use of TYPE_CHECKING with simple boolean logic #142

Merged
merged 1 commit into from Nov 20, 2022

Conversation

steverice
Copy link
Contributor

For various reasons, modules may be conditioned on multiple criteria that determine whether they should be imported. For example, if we have an module that should be imported during testing or type checking, we may do something like:

if TYPE_CHECKING or environment == 'test':
    import foobar

Currently, the linter will flag the foobar import as needing to be moved into an if TYPE_CHECKING block in this scenario. This gives us two options, each of which is less than ideal. We could import the module twice, i.e.

if TYPE_CHECKING:
    import foobar
elif environment == 'test':
    import foobar

or use a # noqa comment and lose the safety of the check when the code changes.

This commit adds some logic to handle these types of simple conditionals and determine if they are equivalent to if TYPE_CHECKING: for the purposes of this linter. In our simple example, we should be able to determine that if TYPE_CHECKING or environment == 'test' will always result in the module being imported if TYPE_CHECKING is True. Similarly, we should be able to determine that if TYPE_CHECKING and environment == 'test' is not sufficient, since we have no guarantees about the value of environment.

@steverice
Copy link
Contributor Author

It seems the 3.10.7 test workflow failed because of a network issue. Perhaps it can be retried.

@codecov
Copy link

codecov bot commented Nov 19, 2022

Codecov Report

Merging #142 (acdbe29) into main (d97a8af) will decrease coverage by 0.5%.
The diff coverage is 96.5%.

@@           Coverage Diff           @@
##            main    #142     +/-   ##
=======================================
- Coverage   98.7%   98.1%   -0.6%     
=======================================
  Files          3       3             
  Lines        472     487     +15     
=======================================
+ Hits         466     478     +12     
- Misses         6       9      +3     
Impacted Files Coverage Δ
flake8_type_checking/plugin.py 97.5% <80.0%> (-2.5%) ⬇️
flake8_type_checking/checker.py 98.1% <100.0%> (-0.5%) ⬇️

@sondrelg
Copy link
Member

Sorry @steverice, I think I saw the test failed when you posted this and assumed you'd make edits, then I missed your comment and forgot about it 🙇

This looks good to me. Would you mind rebasing the PR and I'll take one final look at it before merging and releasing 👍

For various reasons, modules may be conditioned on multiple criteria that determine whether they should be imported.
For example, if we have an module that should be imported during testing or type checking, we may do something like:
```python
if TYPE_CHECKING or environment == 'test':
    import foobar
```

Currently, the linter will flag the `foobar` import as needing to be moved into an `if TYPE_CHECKING` block in this scenario.
This gives us two options, each of which is less than ideal.
We could import the module twice, i.e.
```python
if TYPE_CHECKING:
    import foobar
elif environment == 'test':
    import foobar
```
or use a `# noqa` comment and lose the safety of the check when the code changes.

This commit adds some logic to handle these types of simple conditionals and determine if they are equivalent to `if TYPE_CHECKING:` for the purposes of this linter.
In our simple example, we should be able to determine that `if TYPE_CHECKING or environment == 'test'` will always result in the module being imported if `TYPE_CHECKING` is True.
Similarly, we should be able to determine that `if TYPE_CHECKING and environment == 'test'` is _not_ sufficient, since we have no guarantees about the value of `environment`.
@steverice
Copy link
Contributor Author

Thanks @sondrelg , updated! I looked at the coverage report and it looks like the uncovered lines are from #103 and #126. Would you like me to add coverage for those?

@sondrelg
Copy link
Member

If you're up for it, that would be amazing 👏

@steverice
Copy link
Contributor Author

@sondrelg #144 and #145 contain the fixes for the test coverage. Let's merge those first and then I'll rebase this.

Codecov is complaining on all three of these. Hopefully it's clear that this is due to the existing coverage issues, as a commit that only adds new tests can't possibly decrease coverage 😄

@sondrelg
Copy link
Member

Thanks again 👍 Don't think you need to rebase this, I'll merge as is and release a new minor version shortly

@sondrelg sondrelg merged commit 9e986ec into snok:main Nov 20, 2022
@steverice steverice deleted the additional-boolean-logic branch November 20, 2022 21:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants