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(api): keep proto contents in bytes for longer #14446

Merged
merged 6 commits into from
Feb 9, 2024

Conversation

sfoster1
Copy link
Member

@sfoster1 sfoster1 commented Feb 7, 2024

When we parse python protocols, we were doing it by (1) making it into a string with decode('utf-8') and then (2) passing the string ast.parse(). The problem with this is that decode('utf-8') does not apply "universal newlines", which means that the code object created by compiling the ast will have line numbers that are around twice what they should be under certain circumstances (windows machine, crlf file, mercury in the seventh house, etc). Then, when we go and display a nice error message about a syntax error or whatever, the user says "why is this error message pointing to a place past the end of my protocol".

This should fix that by keeping the protocol contents in bytes form all the way through to passing ast.parse() a bytes that has never been through str.decode('utf-8') which should preserve everything.

Testing

  • Use a protocol like the attached
    flex_mag_mod.py.zip (unzip it first) that has (1) an error and (2) crlf newlines
  • On windows, simulate with opentrons_simulate and in the app
  • check that it says the error is on the right line, and not the line*2

When we parse python protocols, we were doing it by (1) making it into a
string with decode('utf-8') and then (2) passing the string ast.parse().
The problem with this is that decode('utf-8') does not apply "universal
newlines", which means that the code object created by compiling the ast
will have line numbers that are around twice what they should be under
certain circumstances (windows machine, crlf file, mercury in the
seventh house, etc). Then, when we go and display a nice error message
about a syntax error or whatever, the user says "why is this error
message pointing to a place past the end of my protocol".

This should fix that by keeping the protocol contents in bytes form all
the way through to passing ast.parse() a bytes that _has never been
through str.decode('utf-8')_ which should preserve everything.
@sfoster1 sfoster1 requested a review from a team as a code owner February 7, 2024 21:37
Copy link

codecov bot commented Feb 7, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (8398c83) 68.10% compared to head (dbe8417) 68.03%.
Report is 7 commits behind head on chore_release-7.2.0.

Additional details and impacted files

Impacted file tree graph

@@                   Coverage Diff                   @@
##           chore_release-7.2.0   #14446      +/-   ##
=======================================================
- Coverage                68.10%   68.03%   -0.08%     
=======================================================
  Files                     2518     2518              
  Lines                    72031    72027       -4     
  Branches                  9245     9245              
=======================================================
- Hits                     49059    49003      -56     
- Misses                   20770    20822      +52     
  Partials                  2202     2202              
Flag Coverage Δ
g-code-testing 92.43% <ø> (-4.06%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
api/src/opentrons/protocols/parse.py 96.32% <ø> (-0.14%) ⬇️
api/src/opentrons/protocols/types.py 89.74% <ø> (ø)
api/src/opentrons/util/entrypoint_util.py 90.69% <ø> (ø)

... and 5 files with indirect coverage changes

Copy link
Contributor

@SyntaxColoring SyntaxColoring left a comment

Choose a reason for hiding this comment

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

Wow, juicy.

Very happy to have less encoding/decoding. Thank you!

api/src/opentrons/protocols/parse.py Outdated Show resolved Hide resolved
Copy link
Contributor

@SyntaxColoring SyntaxColoring left a comment

Choose a reason for hiding this comment

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

Thank you! CI is failing but it looks like some Python environment thing unrelated to this PR. This LGTM if tests and linting pass locally.

@sfoster1 sfoster1 merged commit 8dc6f79 into chore_release-7.2.0 Feb 9, 2024
18 of 22 checks passed
@sfoster1 sfoster1 deleted the fix-py-error-numbers-crlf branch February 9, 2024 15:56
TamarZanzouri pushed a commit that referenced this pull request Feb 16, 2024
When we parse python protocols, we were doing it by (1) making it into a
string with decode('utf-8') and then (2) passing the string ast.parse().
The problem with this is that decode('utf-8') does not apply "universal
newlines", which means that the code object created by compiling the ast
will have line numbers that are around twice what they should be under
certain circumstances (windows machine, crlf file, mercury in the
seventh house, etc). Then, when we go and display a nice error message
about a syntax error or whatever, the user says "why is this error
message pointing to a place past the end of my protocol".

This should fix that by keeping the protocol contents in bytes form all
the way through to passing ast.parse() a bytes that _has never been
through str.decode('utf-8')_ which should preserve everything.
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