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

fix: Make filtering children by class in traverse() actually work #157

Merged
merged 1 commit into from
Aug 29, 2022

Conversation

asb
Copy link
Contributor

@asb asb commented Aug 28, 2022

The traverse() utility function was previously written to filter
children by class using issubclass(child, klass):. But the first
argument to issubclass() must be a class so this will always raise an
exception when trying to use the functionality. This patch corrects the
call to isinstance(child, klass) and adds a test.

@pbodnar pbodnar changed the title fix: Correct typo in logic for filtering children by class in traverse() fix: Make filtering children by class in traverse() actually work Aug 28, 2022
Copy link
Collaborator

@pbodnar pbodnar left a comment

Choose a reason for hiding this comment

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

@asb, thank you for the fix. I guess it was a mistake to accept the previous PR without a more complete set of tests.

If you don't mind, I have just slightly changed the PR title - can you please do the same with your commit message, while amending the commit with my little suggestion from the code review? :)

test/test_traverse.py Outdated Show resolved Hide resolved
@pbodnar pbodnar added the bug label Aug 28, 2022
The `traverse()` utility function was previously written to filter
children by class using `issubclass(child, klass):`. But the first
argument to `issubclass()` must be a class so this will always raise an
exception when trying to use the functionality. This patch corrects the
call to `isinstance(child, klass)` and adds a test.
@asb
Copy link
Contributor Author

asb commented Aug 28, 2022

@pbodnar: thanks for such a rapid review, I've made the suggested changes.

It's an unfortunate bug, but the traverse utility does seem pretty handy to have in the codebase. It's nice to have a helper to make it easier to do information extraction or even light modifications at the AST level, as an alternative/companion to customising the renderer.

@pbodnar pbodnar merged commit db9a19e into miyuchina:master Aug 29, 2022
@pbodnar
Copy link
Collaborator

pbodnar commented Aug 29, 2022

@asb, thanks for the quick fix. :)

@asb
Copy link
Contributor Author

asb commented Aug 29, 2022

I'm now wondering what the behaviour should be when specifying a class to filter on and also setting include_source=True. As it is, the 'source' node will be yielded even if it doesn't match the filter. Perhaps it doesn't matter much either way, but this seems a little surprising to me. It also doesn't seem too important one way or another though.

Given filtering by class didn't work previously, there's an opportunity to tweak this without breaking anyone's code.

@pbodnar
Copy link
Collaborator

pbodnar commented Sep 1, 2022

@asb, good point. I can imagine that most people would expect, like you, that filtering by class will always win over include_source=True. In that case, to follow the principle of least surprise, we should probably change the current behavior. Are you willing to do the change yourself (change impl. + doc + unit test)?

@asb
Copy link
Contributor Author

asb commented Sep 2, 2022

Sure thing - see #158

@pbodnar pbodnar mentioned this pull request Jan 7, 2023
asb added a commit to muxup/muxup-site that referenced this pull request Feb 27, 2023
…se is fixed

Now my fixes <miyuchina/mistletoe#157>
<miyuchina/mistletoe#158>
<miyuchina/mistletoe#159> are in a released
version, just use the upstream traverse implementation.
asb added a commit to muxup/muxup-site that referenced this pull request Nov 24, 2024
…se is fixed

Now my fixes <miyuchina/mistletoe#157>
<miyuchina/mistletoe#158>
<miyuchina/mistletoe#159> are in a released
version, just use the upstream traverse implementation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants