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

refactor(types): Mark properties with NotRequired #2062

Merged
merged 10 commits into from
Sep 21, 2022
Merged

refactor(types): Mark properties with NotRequired #2062

merged 10 commits into from
Sep 21, 2022

Conversation

rchl
Copy link
Member

@rchl rchl commented Sep 20, 2022

Make use of auto-generated types from https://github.com/sublimelsp/lsp-python-types that utilize NotRequired for properties that might not be present.

https://github.com/sublimelsp/lsp-python-types generates two separate files - lsp_types.py and lsp_types_sublime_text_33.py. Former uses new class syntax that is documentation-annotations-friendly but is not compatible with Python 3.3. Latter doesn't use the new class syntax. We have to use the latter which means that documentation for properties won't be picked up by pyright but otherwise there are no downsides.

Even though ST's 3.3 host doesn't support NotRequired (or even typing for that matter), that doesn't matter because for 3.3 the existing LSP code has a dummy type aliases that do nothing anyway. The important part is that type checkers like mypy and pyright type-check using Python version that supports those types.

Some notes about changes:

  • Our WorkspaceFolder class conflicted with same-named LSP type. Moved our class to workspace.py. Since it's a public export, plugins that imported it from LSP.plugin will be unaffected. The few that imported from LSP.core.protocol will be updated and I took care of that.
  • Mypy is not compatible with those generated types and errors with Cannot resolve name "X" (possible cyclic definition). This is seemingly already supported on master (as an opt-in) but not released yet. For now mypy is disabled due to that. I'm not sure but I don't think we can ignore type errors for specific file as if mypy can't process those types then I don't think it would be able to type check anyway.
  • Fixed various type issues with Promise.
  • Enabled type checking using pyright in tox.ini and CI and fixed almost all errors that it reported. The remaining ones are the stupid cases like view = None # type: sublime.View that I think we shouldn't be doing as that looks and feels wrong. Haven't investigated how we could fix those yet. Potentially we can just ignore those.
  • I'm sure that there are places in the existing code that could be updated to use more of the generated types but that's something that can be done later, while making changes in relevant code.

plugin/core/views.py Outdated Show resolved Hide resolved
@rchl rchl merged commit 57ff6fc into main Sep 21, 2022
@rchl rchl deleted the fix/types branch September 21, 2022 17:47
from typing import TypeVar
from typing import Union
from typing_extensions import NotRequired
from typing_extensions import Required
from typing_extensions import TypedDict
Copy link
Member

Choose a reason for hiding this comment

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

typing_extensions is a third-party package, so this won't work on python 3.8.

Copy link
Member Author

Choose a reason for hiding this comment

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

In 3.8 we could keep using real imports for type checking and dummy ones for the plugin host, like we do now.
Then:

  • this won't really matter
  • if we set python version to 3.11 for pyright then we can import those from typing

One thing I'm not sure about (although it doesn't matter for ST) is how both pyright and mypy don't have problems with this typing_extensions import. I guess that they either come with own copies or use system ones?

Copy link
Member Author

Choose a reason for hiding this comment

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

Addressing in #2067

rchl added a commit that referenced this pull request Sep 27, 2022
* main:
  Fix issues with diagnostics panel toggling on save (#2063)
  Request color presentations when clicking on a color box (#2065)
  Import new imports like NotRequired from "typing"
  Ensure "Source Actions..." request includes the "source" kind (#2064)
  refactor(types): Mark properties with NotRequired (#2062)
  Add template variable `$text_document_position` in execute command (#2061)
  Only update content of diagnostics panel when visible (#2054)
  Remove Range class and rename RangeLsp to Range (#2058)
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.

4 participants