Skip to content

Type annotation improvments#543

Merged
alcarney merged 4 commits into
openlawlibrary:mainfrom
alcarney:type-annotations
Jun 4, 2025
Merged

Type annotation improvments#543
alcarney merged 4 commits into
openlawlibrary:mainfrom
alcarney:type-annotations

Conversation

@alcarney

@alcarney alcarney commented Jun 3, 2025

Copy link
Copy Markdown
Collaborator

Description (e.g. "Related to ...", etc.)

This PR tries to improve the type annotations in various parts of the codebase, they're far from perfect but this is hopefully a step in the right direction. Highlights include

  • Server methods that return a future now specify the type of the result

  • Client methods that return a future now specify the type of the result

  • The get_capability function now has a long list of @typing.overload declarations so that your editor can help you with both the name of the capability

    client_capability

    and the shape of the result

    return_type
    Both types.ServerCapabilities and types.ClientCapabilities are supported

I've also included 1475823 so that pygls better handles responses with unknown ids (see #541)

Code review checklist (for code reviewer to complete)

  • Pull request represents a single change (i.e. not fixing disparate/unrelated things in a single PR)
  • Title summarizes what is changing
  • Commit messages are meaningful (see this for details)
  • Tests have been included and/or updated, as appropriate
  • Docstrings have been included and/or updated, as appropriate
  • Standalone docs have been updated accordingly

Automated linters

You can run the lints that are run on CI locally with:

poetry install --all-extras --with dev
poetry run poe lint

@alcarney alcarney force-pushed the type-annotations branch from 25d2078 to 22db68b Compare June 3, 2025 12:34
alcarney added 4 commits June 3, 2025 13:43
This commit updates some of the type annotations in the `JsonRPCProtocol` class to be
more accurate, reduce the number of false errors when running pyright lsp and hopefully,
improve the quality of completion suggestions.

The easiest way to "fix" the `handle_message` appeared to be to create a bunch of
`Protocols` that describe the shape of incoming messages and use `isinstance` to
pick between them. This does mean duplicating all of our fallback message types but
I don't think that can be avoided as you cannot create an instance of a protocol.

There are still more cases to fix, but this should be a step in the right direction.
@alcarney alcarney force-pushed the type-annotations branch from 22db68b to 16480b8 Compare June 3, 2025 12:43
@alcarney alcarney requested a review from tombh June 3, 2025 12:53
@alcarney

alcarney commented Jun 3, 2025

Copy link
Copy Markdown
Collaborator Author

I'll look into fixing pyodide in a separate PR

@tombh tombh left a comment

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.

Wow 🥹 I love types!

@alcarney

alcarney commented Jun 4, 2025

Copy link
Copy Markdown
Collaborator Author

Thanks!

@alcarney alcarney merged commit 330265d into openlawlibrary:main Jun 4, 2025
17 of 18 checks passed
@alcarney alcarney deleted the type-annotations branch June 4, 2025 10:03
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