Skip to content

Commit

Permalink
Fixed SyntaxError with import hook, future imports and module docstring
Browse files Browse the repository at this point in the history
Fixes #87.
  • Loading branch information
agronholm committed Nov 8, 2019
1 parent 3ec45b9 commit a96a01b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ Version history

This library adheres to `Semantic Versioning 2.0 <https://semver.org/#semantic-versioning-200>`_.

**UNRELEASED**

- Fixed import errors when using the import hook and trying to import a module that has both a
module docstring and ``__future__`` imports in it

**2.6.0** (2019-11-06)

- Added a :pep:`302` import hook for annotating functions and classes with ``@typechecked``
Expand Down
1 change: 1 addition & 0 deletions tests/dummymodule.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Module docstring."""
from __future__ import absolute_import
from __future__ import division

Expand Down
2 changes: 2 additions & 0 deletions typeguard/importhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ def visit_Module(self, node: ast.Module):
for i, child in enumerate(node.body):
if isinstance(child, ast.ImportFrom) and child.module == '__future__':
continue
elif isinstance(child, ast.Expr) and isinstance(child.value, ast.Str):
continue # module docstring
else:
node.body.insert(i, ast.Import(names=[ast.alias('typeguard', None)]))
break
Expand Down

0 comments on commit a96a01b

Please sign in to comment.