Skip to content

Trouble parsing unassigned expressions #54

Closed

Description

@brocla

The Representer fails to parse this dataclass,

from dataclasses import dataclass


@dataclass
class Point:
    x: int
    y: int

Here is the traceback.

But it will parse the dataclass if the variables are initialized like this,

from dataclasses import dataclass


@dataclass
class Point:
    x: int = 0
    y: int = 0

The representer also fails on this bit of code,

try:
    1/0
except:
    pass

but will parse the code if the expression is assigned like this,

try:
    x = 1/0
except:
    pass

Even though the traceback points at ast.unparse(tree), I suspect the visit_Expr() method is the place to look.

Activity

  1. github-actions commented on Mar 21, 2024

    @github-actions
  2. BethanyG commented on Mar 21, 2024

    @BethanyG
    Member

    sigh. That auto-close bot sure is aggressive.

    Even though the traceback points at ast.unparse(tree), I suspect the visit_Expr() method is the place to look.

    Yup. We may have an issue there. But basic math doesn't fail, AFAK, so maybe it's the nesting.

    Parsing and unparsing the code directly using ast.parse() and ast.unparse works just fine. But it is weird that an uninstantiated dataclass (which is a shortcut class) would create any issues at all. Especially since uninstantiated classes don't create issues.

    🤔 .....although, it might be the decorator specifically. That doesn't explain the issues with the try -- except.

    I'll take a look this afternoon. Thanks for reporting this!

  3. BethanyG commented on Mar 21, 2024

    @BethanyG
    Member

    UPDATE:

    Starting at line 314

    This code:

            # Pass through generator code.
            if isinstance(node.value, Yield):
                return node

    Did not account for other possible top-level unassigned expressions. For the most part, this wouldn't get triggered by student code because most things inside functions get assigned names (...right up until they don't get assigned names 🤦🏽‍♀️ ). But a biggie is that it didn't account for hanging lambda expressions. UGH.

    So that we have a record, the ast.Expr types we needed to account for in the above are:

    UnaryOp, BinOp, BoolOp, Lambda, ListComp, DictComp, SetComp, GenExpr, Yield, YieldFrom

    However, if we look explicitly for Constant, we don't have to be explicit with the skips.
    So, I eliminated that code, and adjusted the eliminate registered docstrings code to read:

            if isinstance(node.value, Constant) and not isinstance(node.value, Call):
                # eliminate registered docstrings
                if utils.md5sum(node.value.value) in self._docstring_cache:
                    return None

    But I now have some tests failing, so need to make sure I haven't munged something else. Will keep you posted. 😄

  4. BethanyG commented on Mar 21, 2024

    @BethanyG
    Member

    Alrighty. PR'd a fix for this here. Should you want to play with it using docker, you will need to pull the PR branch and rebuild the container by using docker build -t exercism/python-representer . from your exercism/python-representer directory.

    Let me know if you have questions or issues. Many thanks again for finding this. 😄

    We may need to wait until Erik is back from vacation to merge this. I don't think that's a huge deal, but if you think this migtht be critical, we can ask iHiD to review and merge for us.

  5. brocla commented on Mar 22, 2024

    @brocla
    ContributorAuthor
  6. self-assigned this
    on Mar 26, 2024
  7. BethanyG commented on Mar 27, 2024

    @BethanyG
    Member

    PR #55 has been merged 🎉 . Closing this as fixed (at least until we find the next bug). 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions