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 types for protocols to add type safety #1903

Merged
merged 8 commits into from
Dec 13, 2021
Merged

fix types for protocols to add type safety #1903

merged 8 commits into from
Dec 13, 2021

Conversation

rchl
Copy link
Member

@rchl rchl commented Nov 23, 2021

Fixes types for pyright in cases like below where we've blatantly lied about types.

Screenshot 2021-11-23 at 10 41 10

The way it was done before, apart from causing type issues with Pyright, allowed users to just access session_view.session_buffer from Session, for example, and get away with accessing anything on SessionBuffer because the type was Any.

@jwortmann sorry to cause troubles for you again. I don't mean bad. :)

@@ -37,47 +37,63 @@ class SessionView:
_session_buffers = WeakValueDictionary() # type: WeakValueDictionary[Tuple[int, int], SessionBuffer]

def __init__(self, listener: AbstractViewListener, session: Session, uri: DocumentUri) -> None:
self.view = listener.view
Copy link
Member Author

@rchl rchl Nov 23, 2021

Choose a reason for hiding this comment

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

To avoid changing too much code, decided to use the "underscore" property in __init__ and access through the getter elsewhere. This is maybe not super consistent but makes the diff smaller.

Let me know what you prefer. Maybe just assign to _view and use the getter from there on.

@rchl
Copy link
Member Author

rchl commented Nov 23, 2021

Hmm, looks like I've triggered some import loop... Fixed

@rwols
Copy link
Member

rwols commented Nov 27, 2021

The = None syntax is the correct syntax for python < 3.6. Because plugin_host uses python 3.3, we cannot annotate properties of classes so this syntax is used. Mypy understands it.

https://mypy.readthedocs.io/en/stable/class_basics.html#instance-and-class-attributes

Type comments work as well, if you need to support Python versions earlier than 3.6:

class A:
    x = None  # type: List[int]  # Declare attribute 'x' of type List[int]

Do we really need accessor methods? It's starting to look a bit enterprisey...

@rwols
Copy link
Member

rwols commented Nov 27, 2021

I do appreciate that listener and session_buffer don't have type Any anymore, but the accessor methods are a bit too much I think.

@rchl
Copy link
Member Author

rchl commented Nov 27, 2021

Do we really need accessor methods? It's starting to look a bit enterprisey...

I don't know of another way to do it.

Changing:

session_buffer = None  # type: Any

to

session_buffer = None  # type: SessionViewProtocol

confuses mypy in places where SessionView is passed when SessionViewProtocol is expected so that can't be done either.

@rchl
Copy link
Member Author

rchl commented Nov 27, 2021

I would think that type safety is more important over how it "feels"? And to me it doesn't even feel bad from the *Protocol perspective.

I guess it is a bit weird how it feels in the actual "implementations" of those protocols but it can be seen as a positive thing since the distinction between the private properties and the public interface is made clear that way.

rchl added 2 commits November 27, 2021 16:27
* main:
  Send range with textDocument/hover when possible (#1900)
* origin/main:
  fix crash loop on disabling helper package of a running server (#1906)
@rwols rwols merged commit f26536b into main Dec 13, 2021
@rwols rwols deleted the fix/types branch December 13, 2021 20:28
rchl added a commit that referenced this pull request Dec 13, 2021
* main:
  Don't call the callback if the transport was closed
  Handle Code Lens Refresh Request (#1918)
  fix types for protocols to add type safety (#1903)
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