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

add concrete visitor methods #45

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

danieleades
Copy link
Contributor

@danieleades danieleades commented May 8, 2021

add concrete visitor methods to the GenericNodeVisitor and SparseNodeVisitor classes

@danieleades danieleades force-pushed the fix/add-visitors branch 4 times, most recently from ff25db2 to b09eb13 Compare May 8, 2021 10:19
@danieleades danieleades force-pushed the fix/add-visitors branch 2 times, most recently from 5073088 to 232129f Compare May 8, 2021 10:23
@danieleades
Copy link
Contributor Author

@tk0miya this is ready for review

@danieleades
Copy link
Contributor Author

ping @tk0miya

Copy link
Owner

@tk0miya tk0miya left a comment

Choose a reason for hiding this comment

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

Sorry for late response

class SparseNodeVisitor(NodeVisitor):
def visit_Text(self, node: Text) -> None: ...
def depart_Text(self, node: Text) -> None: ...
def visit_abbreviation(self, node: abbreviation) -> None: ...
Copy link
Owner

Choose a reason for hiding this comment

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

I feel using a concrete node class for each visit/departure method is too strict. Surely it's very accurate and correct. But it warns if we'll pass a node to another visit/departure method like following:

def visit_mynode(self, node: mynode) -> None:
    self.visit_emphasis(node)  # => incompatible type error!

This is the reason why we use `nodes.Element` for the annotation of writers.

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'm not sure i understand what you mean? the example you give is inherently not type-safe. The caller can't assume that visit_emphasis will even work correctly on their custom Element. In the general case, visit_X might rely on attributes/behaviour that are defined in X and not defined on the Element baseclass. This should be an error.

If a user defines a custom Element for which they wish to use visit_emphasis then surely they should subclass nodes.Emphasis (in which case this won't give an error)? Failing that, they should explicitly suppress type errors for that function/line

Copy link
Owner

Choose a reason for hiding this comment

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

These are examples.

https://repo.or.cz/docutils.git/blob/HEAD:/docutils/docutils/writers/_html_base.py#l1310
https://repo.or.cz/docutils.git/blob/HEAD:/docutils/docutils/writers/latex2e/__init__.py#l1622
https://repo.or.cz/docutils.git/blob/HEAD:/docutils/docutils/writers/manpage.py#l475

In the general case, visit_X might rely on attributes/behaviour that are defined in X and not defined on the Element baseclass.

Generally, it's correct. But, in the docutils case, there are no nodes having additional attributes and behaviors. There is no advantage to use a detailed class for visitor methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thankyou for the examples @tk0miya! I'll try to take a proper look in the next couple of days

Copy link
Contributor Author

Choose a reason for hiding this comment

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

https://repo.or.cz/docutils.git/blob/HEAD:/docutils/docutils/writers/manpage.py#l475

perhaps in this case, this would be better typed as Admonition, than admonition?

Copy link
Owner

Choose a reason for hiding this comment

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

perhaps in this case, this would be better typed as Admonition, than admonition?

The classes starting with capital are not "node" classes. They're mainly used for classifying the semantics of nodes. They don't have any methods and attributes. So typing as Admonition is not useful at all.

Additionally, the caution node is not a child of the admonition node.

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

Successfully merging this pull request may close these issues.

2 participants