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

Use issubclass instead of isinstance to compare SourceGenerator in to_source #164

Merged
merged 7 commits into from
Dec 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions astor/code_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"""

import ast
import inspect
import math
import sys

Expand Down Expand Up @@ -53,10 +54,10 @@ class that will be instantiated and used to generate the source code.
"""
if source_generator_class is None:
source_generator_class = SourceGenerator
elif not isinstance(source_generator_class, SourceGenerator):
elif not inspect.isclass(source_generator_class):
raise TypeError('source_generator_class should be a class')
elif not issubclass(source_generator_class, SourceGenerator):
raise TypeError('source_generator_class should be a subclass of SourceGenerator')
elif not callable(source_generator_class):
raise TypeError('source_generator_class should be a callable')
generator = source_generator_class(
indent_with, add_line_information, pretty_string)
generator.visit(node)
Expand Down
8 changes: 8 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ Bug fixes
.. _`Issue 153`: https://github.com/berkerpeksag/astor/issues/153
.. _`PR 155`: https://github.com/berkerpeksag/astor/pull/155

* Fixed :func:`astor.to_source` incorrectly checking whether
*source_generator_class* is a subclass of :class:`astor.code_gen.SourceGenerator`.
(Reported by Yu-Chia "Hank" Liu in `Issue 158`_ and fixed by Will Crichton in `PR 164`_.)

.. _`Issue 158`: https://github.com/berkerpeksag/astor/issues/158
.. _`PR 164`: https://github.com/berkerpeksag/astor/pull/164


0.8.0 - 2019-05-19
------------------

Expand Down
2 changes: 1 addition & 1 deletion tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class InvalidGenerator:
)
self.assertEqual(
str(cm.exception),
'source_generator_class should be a callable',
'source_generator_class should be a class',
)


Expand Down