Skip to content

fix[ux]: add missing filename to syntax exceptions#4343

Merged
charles-cooper merged 37 commits intovyperlang:masterfrom
sandbubbles:fix/syntax-exception-missing-filename
Dec 13, 2024
Merged

fix[ux]: add missing filename to syntax exceptions#4343
charles-cooper merged 37 commits intovyperlang:masterfrom
sandbubbles:fix/syntax-exception-missing-filename

Conversation

@sandbubbles
Copy link
Copy Markdown
Contributor

@sandbubbles sandbubbles commented Nov 1, 2024

What I did

Print filename for syntax exceptions as in #4285.

How I did it

Rethrow syntax exceptions from a point where path is known (two places - one for natspec, one for the rest).

How to verify it

Commit message

this commit adds a filename to `SyntaxException`s. previously, they did
not include filename information because that is typically added from
the `Module` AST node fields. but, at the time a `SyntaxException`
is thrown, the AST is not yet available, so the normal handler does
not have the filename info. this commit adds the filename info in two
places where the path is known, one in natspec.py and one in parse.py.

Description for the changelog

Cute Animal Picture

Put a link to a cute animal picture inside the parenthesis-->

@codecov
Copy link
Copy Markdown

codecov bot commented Nov 2, 2024

Codecov Report

Attention: Patch coverage is 38.09524% with 13 lines in your changes missing coverage. Please review.

Project coverage is 49.36%. Comparing base (c8691ac) to head (fd8d78d).

Files with missing lines Patch % Lines
vyper/exceptions.py 25.00% 6 Missing ⚠️
vyper/ast/parse.py 42.85% 4 Missing ⚠️
vyper/ast/natspec.py 50.00% 3 Missing ⚠️

❗ There is a different number of reports uploaded between BASE (c8691ac) and HEAD (fd8d78d). Click for more details.

HEAD has 116 uploads less than BASE
Flag BASE (c8691ac) HEAD (fd8d78d)
140 24
Additional details and impacted files
@@             Coverage Diff             @@
##           master    #4343       +/-   ##
===========================================
- Coverage   91.31%   49.36%   -41.95%     
===========================================
  Files         113      113               
  Lines       16065    16083       +18     
  Branches     2705     2706        +1     
===========================================
- Hits        14670     7940     -6730     
- Misses        964     7529     +6565     
- Partials      431      614      +183     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment thread vyper/exceptions.py Outdated
node_msg = f'{node_msg}function "{fn_node.name}", '

if self.path is not None:
node_msg = f'{node_msg}contract "{self.path}", '
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

code smell that this code is similar with line 132 above -- and in fact, line 132 appends lineno but this does not. also the path could be appended twice in case there is both a node and .path set on this exception

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

is it better now?

sandbubbles and others added 5 commits November 7, 2024 11:43
…4337)

Make "venom" an alternative alias to the "experimental_codegen" flag.
Add the alias to cli, pre-parser, and json parser.
the call to `setup()` would fail on windows when there are unicode
characters in `README.md`, because files are apparently opened with
encoding `cp1252` by default on windows. this commit ensures the file
is opened with `utf-8` encoding.
@sandbubbles sandbubbles marked this pull request as ready for review November 9, 2024 10:21
Comment thread vyper/exceptions.py Outdated
return msg
return msg + f"\n (hint: {self.hint})"

def _append_contract(self, msg, path, lineno):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

_append_contract() sounds like it modifies the Exception, but this is a helper function.

Comment thread vyper/compiler/phases.py Outdated
analyze_module(module)
nspec = natspec.parse_natspec(module)
try:
nspec = natspec.parse_natspec(module)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

i just checked natspec.py and don't see that it throws SyntaxException anywhere?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nvm -- NatspecSyntaxException inherits from SyntaxException. however it will be better to have the try/catch in parse_natspec itself (in case it's ever used from somewhere else)

Copy link
Copy Markdown
Member

@charles-cooper charles-cooper left a comment

Choose a reason for hiding this comment

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

left some comments

Comment thread vyper/ast/natspec.py Fixed
Comment thread vyper/ast/natspec.py Fixed
Comment thread vyper/exceptions.py
if module_node.get("path") not in (None, "<unknown>"):
node_msg = f'{node_msg}contract "{module_node.path}:{node.lineno}", '
node_msg = self._format_contract_details(
node_msg, module_node.resolved_path, node.lineno
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

we check .get("path") but we access .resolved_path - is that correct?

@cyberthirst
Copy link
Copy Markdown
Collaborator

when raising the exception:

    except SyntaxError as e:
        # TODO: Ensure 1-to-1 match of source_code:reformatted_code SyntaxErrors
        raise SyntaxException(str(e), vyper_source, e.lineno, e.offset) from None

we use str(e) which will get propagated as the message. during the construction, the path is not provided, thus the unknown is still displayed:

main.vy
def foo():
    uint256 a = pass

yielding:

vyper.exceptions.SyntaxException: invalid syntax (<unknown>, line 2)

  contract "tests/custom/test.vy:2", line 2:13 
       1 def foo():
  ---> 2     uint256 a = pass
  --------------------^

see vyper.exceptions.SyntaxException: invalid syntax (<unknown>, line 2)

Comment thread vyper/exceptions.py
@cyberthirst cyberthirst added the release - must release blocker label Dec 11, 2024
@sandbubbles
Copy link
Copy Markdown
Contributor Author

I believe all the changes have been addressed so it's ready for another review

Comment thread vyper/exceptions.py Outdated
self.lineno = None
self.col_offset = None
self.annotations = None
self.path = None
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

for clarity, let's rename this to self.resolved_path

Comment thread vyper/ast/parse.py Outdated
except SyntaxError as e:
# TODO: Ensure 1-to-1 match of source_code:reformatted_code SyntaxErrors
raise SyntaxException(str(e), vyper_source, e.lineno, e.offset) from None
# SyntaxError offset is 1-based, not 0-based
Copy link
Copy Markdown
Member

@charles-cooper charles-cooper Dec 12, 2024

Choose a reason for hiding this comment

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

please add docs to comment:

Suggested change
# SyntaxError offset is 1-based, not 0-based
# SyntaxError offset is 1-based, not 0-based
# https://docs.python.org/3/library/exceptions.html#SyntaxError.offset

Copy link
Copy Markdown
Member

@charles-cooper charles-cooper left a comment

Choose a reason for hiding this comment

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

couple tiny nits, i think otherwise this is ready

Comment thread vyper/exceptions.py Outdated
@charles-cooper charles-cooper enabled auto-merge (squash) December 13, 2024 16:38
@charles-cooper charles-cooper changed the title fix[ux]: syntax exception missing filename fix[ux]: add missing filename to syntax exceptions Dec 13, 2024
@charles-cooper charles-cooper merged commit c951dea into vyperlang:master Dec 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release - must release blocker

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants