-
Notifications
You must be signed in to change notification settings - Fork 182
Handle code guarded by 'if TYPE_CHECKING' correctly #530
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
Closed
Closed
Changes from 7 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
44aab70
Omit code guarded by 'if TYPE_CHECKING'
PetterS d66aeb8
Make the Binding parameter a constructor and use it for all bindings.
PetterS 3b60143
Remove constructor arg.
PetterS a6f0dd2
Another solution.
PetterS b938b2b
Revert "Another solution."
PetterS 3474adf
Add during_type_checking to __init__ and set it during construction.
PetterS b8e687a
Fix Python 2.7
PetterS a30cfcb
Snake case.
PetterS 7306c2b
Rename to for_annotations
PetterS 12fd45a
Flake8
PetterS aeaf0b6
Add a test for type comments as well.
PetterS f5657ba
Merge branch 'master' into omit_type_checking
PetterS a993d91
Fix: elif and else branches should not be affected.
PetterS 7eb8484
Merge branch 'master' into omit_type_checking
PetterS a9d4a7f
Merge branch 'master' into omit_type_checking
PetterS 558914b
Review
PetterS cbe214a
Move the check for _in_annotation
PetterS File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,78 +14,86 @@ | |
class TestImportationObject(TestCase): | ||
|
||
def test_import_basic(self): | ||
binding = Importation('a', None, 'a') | ||
binding = Importation('a', None, during_type_checking=False, full_name='a') | ||
assert binding.source_statement == 'import a' | ||
assert str(binding) == 'a' | ||
|
||
def test_import_as(self): | ||
binding = Importation('c', None, 'a') | ||
binding = Importation('c', None, during_type_checking=False, full_name='a') | ||
assert binding.source_statement == 'import a as c' | ||
assert str(binding) == 'a as c' | ||
|
||
def test_import_submodule(self): | ||
binding = SubmoduleImportation('a.b', None) | ||
binding = SubmoduleImportation('a.b', None, during_type_checking=False) | ||
assert binding.source_statement == 'import a.b' | ||
assert str(binding) == 'a.b' | ||
|
||
def test_import_submodule_as(self): | ||
# A submodule import with an as clause is not a SubmoduleImportation | ||
binding = Importation('c', None, 'a.b') | ||
binding = Importation('c', None, during_type_checking=False, full_name='a.b') | ||
assert binding.source_statement == 'import a.b as c' | ||
assert str(binding) == 'a.b as c' | ||
|
||
def test_import_submodule_as_source_name(self): | ||
binding = Importation('a', None, 'a.b') | ||
binding = Importation('a', None, during_type_checking=False, full_name='a.b') | ||
assert binding.source_statement == 'import a.b as a' | ||
assert str(binding) == 'a.b as a' | ||
|
||
def test_importfrom_relative(self): | ||
binding = ImportationFrom('a', None, '.', 'a') | ||
binding = ImportationFrom('a', None, '.', | ||
during_type_checking=False, real_name='a') | ||
assert binding.source_statement == 'from . import a' | ||
assert str(binding) == '.a' | ||
|
||
def test_importfrom_relative_parent(self): | ||
binding = ImportationFrom('a', None, '..', 'a') | ||
binding = ImportationFrom('a', None, '..', | ||
during_type_checking=False, real_name='a') | ||
assert binding.source_statement == 'from .. import a' | ||
assert str(binding) == '..a' | ||
|
||
def test_importfrom_relative_with_module(self): | ||
binding = ImportationFrom('b', None, '..a', 'b') | ||
binding = ImportationFrom('b', None, '..a', | ||
during_type_checking=False, real_name='b') | ||
assert binding.source_statement == 'from ..a import b' | ||
assert str(binding) == '..a.b' | ||
|
||
def test_importfrom_relative_with_module_as(self): | ||
binding = ImportationFrom('c', None, '..a', 'b') | ||
binding = ImportationFrom('c', None, '..a', | ||
during_type_checking=False, real_name='b') | ||
assert binding.source_statement == 'from ..a import b as c' | ||
assert str(binding) == '..a.b as c' | ||
|
||
def test_importfrom_member(self): | ||
binding = ImportationFrom('b', None, 'a', 'b') | ||
binding = ImportationFrom('b', None, 'a', | ||
during_type_checking=False, real_name='b') | ||
assert binding.source_statement == 'from a import b' | ||
assert str(binding) == 'a.b' | ||
|
||
def test_importfrom_submodule_member(self): | ||
binding = ImportationFrom('c', None, 'a.b', 'c') | ||
binding = ImportationFrom('c', None, 'a.b', | ||
during_type_checking=False, real_name='c') | ||
assert binding.source_statement == 'from a.b import c' | ||
assert str(binding) == 'a.b.c' | ||
|
||
def test_importfrom_member_as(self): | ||
binding = ImportationFrom('c', None, 'a', 'b') | ||
binding = ImportationFrom('c', None, 'a', | ||
during_type_checking=False, real_name='b') | ||
assert binding.source_statement == 'from a import b as c' | ||
assert str(binding) == 'a.b as c' | ||
|
||
def test_importfrom_submodule_member_as(self): | ||
binding = ImportationFrom('d', None, 'a.b', 'c') | ||
binding = ImportationFrom('d', None, 'a.b', | ||
during_type_checking=False, real_name='c') | ||
assert binding.source_statement == 'from a.b import c as d' | ||
assert str(binding) == 'a.b.c as d' | ||
|
||
def test_importfrom_star(self): | ||
binding = StarImportation('a.b', None) | ||
binding = StarImportation('a.b', None, during_type_checking=False) | ||
assert binding.source_statement == 'from a.b import *' | ||
assert str(binding) == 'a.b.*' | ||
|
||
def test_importfrom_star_relative(self): | ||
binding = StarImportation('.b', None) | ||
binding = StarImportation('.b', None, during_type_checking=False) | ||
assert binding.source_statement == 'from .b import *' | ||
assert str(binding) == '.b.*' | ||
|
||
|
@@ -1031,6 +1039,36 @@ def test_futureImportStar(self): | |
from __future__ import * | ||
''', m.FutureFeatureNotDefined) | ||
|
||
def test_ignoresTypingImports(self): | ||
"""Ignores imports within 'if TYPE_CHECKING' checking normal code.""" | ||
self.flakes(''' | ||
from typing import TYPE_CHECKING | ||
if TYPE_CHECKING: | ||
from a import b | ||
b() | ||
''', m.UndefinedName) | ||
|
||
def test_ignoresTypingClassDefinition(self): | ||
"""Ignores definitions within 'if TYPE_CHECKING' checking normal code.""" | ||
self.flakes(''' | ||
from typing import TYPE_CHECKING | ||
if TYPE_CHECKING: | ||
class T: | ||
pass | ||
t = T() | ||
''', m.UndefinedName) | ||
|
||
@skipIf(version_info < (3,), 'has type annotations') | ||
PetterS marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def test_usesTypingImportsForAnnotations(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fwiw, the new tests are using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
"""Uses imports within 'if TYPE_CHECKING' checking annotations.""" | ||
self.flakes(''' | ||
from typing import TYPE_CHECKING | ||
if TYPE_CHECKING: | ||
from a import b | ||
def f() -> "b": | ||
pass | ||
''') | ||
|
||
|
||
class TestSpecialAll(TestCase): | ||
""" | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe
for_annotations
is a more descriptive name here? (idk, I'm bikeshedding on this, feel free to ignore this suggestion, it just feels a little like the meaning of the name gets lost a bit)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's fine; takes a couple of seconds to change.