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

Checking subclass in to_source function #158

Closed
hankliu5 opened this issue Sep 6, 2019 · 4 comments
Closed

Checking subclass in to_source function #158

hankliu5 opened this issue Sep 6, 2019 · 4 comments

Comments

@hankliu5
Copy link

hankliu5 commented Sep 6, 2019

Hello,

I have a question about the function to_source() in code_gen.py. The reference is below:

astor/astor/code_gen.py

Lines 54 to 59 in ef8a655

if source_generator_class is None:
source_generator_class = SourceGenerator
elif not isinstance(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')

Since the message in line 57 mentions that line 56 means to check whether source_generator_class is the subclass of astor.code_gen.SourceGenerator or not, should line 56 uses issubclass(source_generator_class, astor.code_gen.SourceGenerator)?

Thanks for your answer.

@berkerpeksag
Copy link
Owner

Hello, thank you for your report. isinstance(a, b) also does check whether a is a subclass of b. Did you get any non-expected behavior with the current isinstance usage?

@hankliu5
Copy link
Author

hankliu5 commented Sep 6, 2019

Hello,

Thanks for your quick reply. I followed the example from #113 and #114, and here is my source code

import astor
import ast
from astor.string_repr import pretty_string
from astor.source_repr import pretty_source
class SubSourceGenerator(astor.code_gen.SourceGenerator):
    def __init__(self, indent_with, add_line_information=False,
                 pretty_string=pretty_string,
                 # constants
                 len=len, isinstance=isinstance, callable=callable):
        super().__init__(indent_with, add_line_information, pretty_string, len, isinstance, callable)

node = ast.parse('a=1')
astor.to_source(node, source_generator_class=SubSourceGenerator)

Then I got:

Traceback (most recent call last):
  File "<input>", line 15, in <module>
  File "/Users/hankliu/PycharmProjects/py_to_cy/venv/lib/python3.5/site-packages/astor/code_gen.py", line 57, in to_source
    raise TypeError('source_generator_class should be a subclass of SourceGenerator')
TypeError: source_generator_class should be a subclass of SourceGenerator

Then I tried both isinstance and issubclass functions:

issubclass(SubSourceGenerator, astor.code_gen.SourceGenerator)
>>> True

isinstance(SubSourceGenerator, astor.code_gen.SourceGenerator)
>>> False

isinstance(SubSourceGenerator(' '*4), astor.code_gen.SourceGenerator)
>>> True

isinstance(SubSourceGenerator(), astor.code_gen.SourceGenerator)

Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: __init__() missing 1 required positional argument: 'indent_with'

The Python version is 3.5.7. Thanks for your help and your amazing tool :).

@berkerpeksag
Copy link
Owner

Ah, now I got your point! Thank you for the reproducer. That's definitely my mistake, sorry for the trouble. PR #151 should fix that faulty check. I'll try to get it merged this weekend.

@hankliu5
Copy link
Author

hankliu5 commented Sep 6, 2019

Yeah, PR #151 is the one I'd like to propose and fix. Thanks for checking that bug.

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

No branches or pull requests

2 participants