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: handle if TYPE_CHECKING block. #65

Merged

Conversation

peterschutt
Copy link
Contributor

We are experiencing an issue where the __all__ declaration gets positioned between runtime and type checking imports. E.g.,

image

So I've slapped this together as a PoC.

The PR updates visitor so that the last_import is updated for import lines that occur within an if TYPE_CHECKING block.

I've not done any ast stuff before, so don't really know if what I've added here is a good way to go about it, also how far I should go with tests. Any advice appreciated.

@@ -162,7 +162,7 @@ def handle_import(self, node: Union[ast.Import, ast.ImportFrom]) -> None:
if not node.col_offset and node.end_lineno > self.last_import: # type: ignore[union-attr]
self.last_import = node.end_lineno # type: ignore[union-attr]
else:
if not node.col_offset and node.lineno > self.last_import:
if node.lineno > self.last_import:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the offset condition b/c the type check imports are offset - I expected this might break a test or two but it didn't (at least not in my local runs - ci still running so I might have gone the early crow!).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really sure why the check was there. Perhaps I was being overly cautious.

peterschutt and others added 2 commits March 28, 2024 17:30
Updates the `last_import` line num for import lines that occur within an `if TYPE_CHECKING` block.
@domdfcoding domdfcoding force-pushed the handle-if-type-checking-block branch from e8193a8 to 16e715f Compare March 28, 2024 17:33
@domdfcoding domdfcoding force-pushed the handle-if-type-checking-block branch from 16e715f to 73e4c49 Compare March 28, 2024 17:37
@repo-helper repo-helper bot removed the failure: flake8 The Flake8 check is failing. label Mar 28, 2024
@domdfcoding domdfcoding enabled auto-merge (squash) March 28, 2024 17:44
@repo-helper repo-helper bot added the 🤖 automerge Auto merge is enabled for this pull request. label Mar 28, 2024
@domdfcoding domdfcoding merged commit 6af2a0a into python-formate:master Mar 28, 2024
44 checks passed
@peterschutt
Copy link
Contributor Author

Thanks @domdfcoding!

@domdfcoding domdfcoding added this to the v0.4.0 milestone Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖 automerge Auto merge is enabled for this pull request.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants