Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Fix handling of generics and type declarations - #321

Merged
Mikhail Arkhipov (MikhailArkhipov) merged 10 commits into
microsoft:masterfrom
MikhailArkhipov:260-2
Nov 6, 2018
Merged

Mikhail Arkhipov (MikhailArkhipov) merged 10 commits into
microsoft:masterfrom
MikhailArkhipov:260-2

Conversation

@MikhailArkhipov

Copy link
Copy Markdown

Fixes #254
Fixes #260

#254 - Declare protocol that delegates to actual type fully.
#260 - Declare ListProtocol and DictProtocol delegating member fetching to actual underlying types but leaving actual work to Iterable and Mapping protocols respectively as it used to be.

  • Added tests for analysis and completion for the cases

Will split Protocols files into separate classes before merging

@jakebailey

Copy link
Copy Markdown
Member

You might want to add some tests for TypeVar, since you mention it in the comments. I think that'll be required for dealing with some of the typeshed generic types. Maybe something like this (modified from https://docs.python.org/3/library/typing.html#generics):

from typing import Sequence, TypeVar

T = TypeVar('T')      # Declare type variable

def first(l: Sequence[T]) -> T:   # Generic function
    return l[0]

arr = [1, 2, 3]
x = first(arr)  # should be int

Comment thread src/Analysis/Engine/Test/AnalysisTest.cs Outdated
@MikhailArkhipov

Copy link
Copy Markdown
Author

Jake Bailey (@jakebailey) - test added.

@jakebailey

Copy link
Copy Markdown
Member

(Forgive me, I started writing this comment and it blew up to be larger than expected...)

That example I gave does seem to work, but when I actually run it in the editor, I see more than just int:

image

I think the fact that "The most base type" is getting added means that object's docs are getting added which I find to be a little awkward.

Also, I can add the following:

arr2 = ["a", "b", "c"]
y = first(arr2)

And hovering over first will show:

image

Which I find to be a little goofy because:

  • first isn't returning int in this particular location, it's returning str, and y seems to have been given the correct-ish typeset of {str, T}.
  • list isn't technically Sequence (but I suppose we also do this when it's List).

I guess this is due to the fact that we're combining sets of types, and T is just another on top of what's known to be possible. Maybe a future change can improve this.

Comparing this behavior to what's already in master, it seems like this PR doesn't change how TypeVar behaves, but is certainly an improvement for NewType in any case.

(I'd also hoped that the hover would show something like first(l: list[int]) -> int or first(l: list[str]) -> str depending on the invocation, but I think that's probably something that hover will have to deal with some other time.)

@MikhailArkhipov

Copy link
Copy Markdown
Author

It is by design as all types are currently stored, so yet, it is int and T. Same as in def func(a: str) a is str and type - the latter is function argument since it is not type checked and actually does can be anything.

Changing type merging for visual display would be a different task and large amount of work.

@jakebailey

Copy link
Copy Markdown
Member

I understand that's the current implementation, and I'm not saying this PR should be fixing anything of that sort, but what about the inconsistency when it comes to how first is having its return types displayed? For example, I can swap the order of the invocations and get a different result:

image

Comment thread src/Analysis/Engine/Impl/Values/IterableInfo.cs Outdated
Comment thread src/Analysis/Engine/Impl/Values/Protocols.cs Outdated
@MikhailArkhipov
Mikhail Arkhipov (MikhailArkhipov) merged commit 9da1321 into microsoft:master Nov 6, 2018
Jake Bailey (jakebailey) pushed a commit to jakebailey/python-language-server that referenced this pull request Nov 1, 2019
Fix handling of generics and type declarations
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants