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

feat: Add span and diagnostics base classes #548

Merged
merged 8 commits into from
Oct 9, 2024
Merged

Conversation

mark-koch
Copy link
Collaborator

Closes #536.

@mark-koch mark-koch requested a review from croyzor October 7, 2024 10:12
@mark-koch mark-koch requested a review from a team as a code owner October 7, 2024 10:12
@codecov-commenter
Copy link

codecov-commenter commented Oct 7, 2024

Codecov Report

Attention: Patch coverage is 0% with 83 lines in your changes missing coverage. Please review.

Project coverage is 89.90%. Comparing base (37e1633) to head (56dec3e).

Files with missing lines Patch % Lines
guppylang/span.py 0.00% 42 Missing ⚠️
guppylang/diagnostic.py 0.00% 41 Missing ⚠️
Additional details and impacted files
@@                 Coverage Diff                  @@
##           feat/diagnostics     #548      +/-   ##
====================================================
- Coverage             91.09%   89.90%   -1.20%     
====================================================
  Files                    59       61       +2     
  Lines                  6264     6347      +83     
====================================================
  Hits                   5706     5706              
- Misses                  558      641      +83     

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

assert file is not None
assert line_offset is not None
# line_offset starts at 1, so we have to do subtract 1
start = Loc(file, x.lineno + line_offset - 1, x.col_offset)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Loc is meant to be 1-indexed, so it seems good that line_offset starts at 1?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

But x.lineno also starts at 1

Both of these come from the python ast module 😅

span_label: ClassVar[str | None] = None

#: Message that is printed if no span is provided.
message: ClassVar[str | None] = None
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why have this be a ClassVar and not just a field?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Otherwise Python will complain that non-default fields follow default ones when defining a subclass. E.g.

@dataclass(frozen=True)
class TypeMismatchError(Error):
    expected: Type
    actual: Type

    title: str = "Type mismatch"
    span_label: str = "Expected `{expected}`, got `{actual}`"

This is because the field order is dermined by the base classes, so the title and span_label constructor arguments would actually come before the expected and actual args. We would need to do

@dataclass(frozen=True)
class TypeMismatchError(Error):
    expected: Type
    actual: Type

    title: str = field(default="Type mismatch", init=False)
    span_label: str = field(defaut="Expected `{expected}`, got `{actual}`", init=False)

which would be quite cumbersome and looks noisy. Making them ClassVars tells the @dataclass decorator that these fields are static and shouldn't be initialised by the constructor.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Okay, I think the disconnect is that you're intending for these classes to be inherited from rather than used directly, so it makes sense for a the TypeMismatchError subclass to have a class variable.

@mark-koch mark-koch merged commit d671bb5 into feat/diagnostics Oct 9, 2024
2 checks passed
@mark-koch mark-koch deleted the diag/base branch October 9, 2024 08:44
github-merge-queue bot pushed a commit that referenced this pull request Nov 11, 2024
This is the development branch for our new diagnostics infrastructure.
Tracked by #535.

*  #548
* #551
* #552 
* #553
* #586
* #588
* #587
* #589 
* #590
* #600
* #601
* #604 
* #605 
* #606
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.

3 participants