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 some comprehensions and set operations #59

Closed
wants to merge 1 commit into from

Conversation

Takashiidobe
Copy link

@Takashiidobe Takashiidobe commented Oct 21, 2022

As per #56, I tried adding some basic set operations and comprehensions. For comprehensions, I just evaled them, which might not be great (this introduces bugs if each element should be visited instead).

The Ast Nodes for comprehensions have some extra data that can be converted into something like ${f(x) | x \in X}$, but that can be improved in this PR or a later PR.

That being said, it kind of works, so I figured I'd put the changes up here. Feel free to cherry pick the changes or modify in any way.

@google-cla
Copy link

google-cla bot commented Oct 21, 2022

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@odashi
Copy link
Collaborator

odashi commented Oct 21, 2022

@Takashiidobe Thanks for proposing the PR, and sorry I changed some interfaces by the previous PR. Since most of the changes in this PR is implemented in the visitor, I think you can simply implement the same changes into the latexify_visitor.py.

Copy link
Collaborator

@odashi odashi left a comment

Choose a reason for hiding this comment

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

Thanks! this change looks very helpful.

It also contains some issues (as I commented) and we will need some modification.

Since you said "Feel free to cherry pick the changes or modify in any way.", I'm considering to keep this pull request opened and prepare some other requests for the revised versions of this change.

@@ -300,6 +315,25 @@ def visit_If(self, node, action): # pylint: disable=invalid-name
latex += self.visit(node)
return latex + r", & \mathrm{otherwise} \end{array} \right."

def visit_SetComp(self, node, _): # pylint: disable=invalid-name
result = eval(compile(ast.Expression(node), '', 'eval'))
Copy link
Collaborator

@odashi odashi Oct 22, 2022

Choose a reason for hiding this comment

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

eval is not allowed due to its vulnerability. Since latexify accepts almost any code, this invocation may cause an arbitrary code execution. it may be better to adopt ast.literal_eval (it also allows a raw syntax tree).

Same for other parts.

def visit_ListComp(self, node, _): # pylint: disable=invalid-name
result = eval(compile(ast.Expression(node), '', 'eval'))

sorted_result = sorted(result)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Especially for lists, this is not what the users wanted because lists are order-sensitive structure.

Comment on lines +228 to +229
left_type, right_type = type(node.left), type(node.right)
if left_type == right_type == ast.Set:
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it is OK to go into the block if only either lhs or rhs is a Set.

Suggested change
left_type, right_type = type(node.left), type(node.right)
if left_type == right_type == ast.Set:
if isinstance(left_type, ast.Set) or isinstance(right_type, ast.Set):

# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for core."""
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
"""Tests for core."""
"""Tests for latexify.latexify_visitor."""

@@ -0,0 +1,68 @@
# Copyright 2020 Google LLC
Copy link
Collaborator

Choose a reason for hiding this comment

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

This file looks to contain only end-to-end tests. It may be good to either:

  • Move it into src/integration_tests
  • Rewrite tests to directly call LatexifyVisitor.

@Takashiidobe
Copy link
Author

I'll leave this PR to you to cherry pick changes off of. I merged my changes locally to main but it seems to have caused a bunch of errors which I don't feel like fixing again.

@odashi
Copy link
Collaborator

odashi commented Oct 28, 2022

@Takashiidobe Thanks, I'll continue to work on this.

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