microsoft / pylance-release

Documentation and issues for Pylance
Creative Commons Attribution 4.0 International
1.72k stars 766 forks source link

Using Type[T] doesnt return object of type T #2003

Closed rayzchen closed 3 years ago

rayzchen commented 3 years ago

Environment data

Expected behaviour

Return value is of the type I specified

Actual behaviour

Return value is of type T@AddComponent

Logs

[Info  - 20:26:45] Searching for source files
[Info  - 20:26:45] Found 11 source files
Background analysis message: analyze
Background analysis message: setConfigOptions
Background analysis message: setImportResolver
Background analysis message: ensurePartialStubPackages
Background analysis message: setTrackedFiles
Background analysis message: markAllFilesDirty
[Info  - 20:26:45] No pyproject.toml file found.
[Info  - 20:26:45] Setting pythonPath for service "<default>": "C:\Users\raych\AppData\Local\Programs\Python\Python39-32\python.exe"
[Warn  - 20:26:45] stubPath typings is not a valid directory.
[Info  - 20:26:46] Assuming Python version 3.9
[Info  - 20:26:46] Assuming Python platform Windows
Search paths for <default>
  c:\Users\raych\.vscode\extensions\ms-python.vscode-pylance-2021.10.3\dist\typeshed-fallback\stdlib
  typings
  c:\Users\raych\.vscode\extensions\ms-python.vscode-pylance-2021.10.3\dist\typeshed-fallback\stubs\...
  c:\Users\raych\.vscode\extensions\ms-python.vscode-pylance-2021.10.3\dist\bundled\stubs
  c:\Users\raych\AppData\Local\Programs\Python\Python39-32\DLLs
  c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib
  c:\Users\raych\AppData\Local\Programs\Python\Python39-32
  c:\Users\raych\AppData\Roaming\Python\Python39\site-packages
  c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages
  c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\win32
  c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\win32\lib
  c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pythonwin
[Info  - 20:26:49] Searching for source files
[Info  - 20:26:49] No source files found.
Background analysis message: analyze
Background analysis message: setConfigOptions
Background analysis message: setImportResolver
Background analysis message: ensurePartialStubPackages
Background analysis message: setTrackedFiles
Background analysis message: markAllFilesDirty
Background analysis message: analyze
[BG(3)] analyzing: c:\Ray-Data\python\pyunity-tools\pyunity_tools\apps\color\color_source.py ...
[BG(3)]   parsing: c:\Ray-Data\python\pyunity-tools\pyunity_tools\apps\color\color_source.py (906ms)
[BG(3)]   parsing: c:\Users\raych\.vscode\extensions\ms-python.vscode-pylance-2021.10.3\dist\typeshed-fallback\stdlib\builtins.pyi [fs read 5ms] (717ms)
[BG(3)]   binding: c:\Users\raych\.vscode\extensions\ms-python.vscode-pylance-2021.10.3\dist\typeshed-fallback\stdlib\builtins.pyi (268ms)
[BG(3)]   binding: c:\Ray-Data\python\pyunity-tools\pyunity_tools\apps\color\color_source.py ...
[BG(3)]     parsing: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\__init__.pyi [fs read 3ms] (261ms)
[BG(3)]     binding: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\__init__.pyi ...
[BG(3)]       parsing: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\audio.pyi [fs read 15ms] (159ms)
[BG(3)]       binding: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\audio.pyi (30ms)
[BG(3)]       parsing: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\core.pyi [fs read 2ms] (28ms)
[BG(3)]       binding: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\core.pyi (4ms)
[BG(3)]       parsing: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\gui.pyi [fs read 5ms] (164ms)
[BG(3)]       binding: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\gui.pyi (8ms)
[BG(3)]       parsing: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\input.pyi [fs read 9ms] (73ms)
[BG(3)]       binding: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\input.pyi (8ms)
[BG(3)]       parsing: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\physics\__init__.pyi [fs read 1ms] (8ms)
[BG(3)]       binding: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\physics\__init__.pyi ...
[BG(3)]         parsing: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\physics\core.pyi [fs read 6ms] (80ms)
[BG(3)]         binding: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\physics\core.pyi (7ms)
[BG(3)]       binding: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\physics\__init__.pyi (88ms)
[BG(3)]       parsing: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\errors.pyi [fs read 3ms] (3ms)
[BG(3)]       binding: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\errors.pyi (1ms)
[BG(3)]       parsing: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\files.pyi [fs read 4ms] (76ms)
[BG(3)]       binding: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\files.pyi (7ms)
[BG(3)]       parsing: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\values\__init__.pyi [fs read 1ms] (11ms)
[BG(3)]       binding: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\values\__init__.pyi ...
[BG(3)]         parsing: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\values\vector.pyi [fs read 1ms] (38ms)
[BG(3)]         binding: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\values\vector.pyi (9ms)
[BG(3)]         parsing: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\values\texture.pyi [fs read 2ms] (6ms)
[BG(3)]         binding: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\values\texture.pyi (3ms)
[BG(3)]       binding: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\values\__init__.pyi (58ms)
[BG(3)]     binding: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\__init__.pyi (731ms)
[BG(3)]   binding: c:\Ray-Data\python\pyunity-tools\pyunity_tools\apps\color\color_source.py (996ms)
[BG(3)]   checking: c:\Ray-Data\python\pyunity-tools\pyunity_tools\apps\color\color_source.py ...
[BG(3)]     parsing: c:\Users\raych\.vscode\extensions\ms-python.vscode-pylance-2021.10.3\dist\typeshed-fallback\stdlib\typing.pyi [fs read 59ms] (212ms)
[BG(3)]     binding: c:\Users\raych\.vscode\extensions\ms-python.vscode-pylance-2021.10.3\dist\typeshed-fallback\stdlib\typing.pyi (69ms)
[BG(3)]     parsing: c:\Users\raych\.vscode\extensions\ms-python.vscode-pylance-2021.10.3\dist\typeshed-fallback\stdlib\_typeshed\__init__.pyi [fs read 3ms] (98ms)
[BG(3)]     binding: c:\Users\raych\.vscode\extensions\ms-python.vscode-pylance-2021.10.3\dist\typeshed-fallback\stdlib\_typeshed\__init__.pyi (4ms)
[BG(3)]     parsing: c:\Users\raych\.vscode\extensions\ms-python.vscode-pylance-2021.10.3\dist\typeshed-fallback\stdlib\typing_extensions.pyi [fs read 28ms] (41ms)
[BG(3)]     binding: c:\Users\raych\.vscode\extensions\ms-python.vscode-pylance-2021.10.3\dist\typeshed-fallback\stdlib\typing_extensions.pyi (3ms)
[BG(3)]     parsing: c:\Users\raych\.vscode\extensions\ms-python.vscode-pylance-2021.10.3\dist\typeshed-fallback\stdlib\abc.pyi [fs read 16ms] (19ms)
[BG(3)]     binding: c:\Users\raych\.vscode\extensions\ms-python.vscode-pylance-2021.10.3\dist\typeshed-fallback\stdlib\abc.pyi (2ms)
[BG(3)]     parsing: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\values\abc.pyi [fs read 1ms] (6ms)
[BG(3)]     binding: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\values\abc.pyi (1ms)
[BG(3)]     parsing: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\values\other.pyi [fs read 1ms] (5ms)
[BG(3)]     binding: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\values\other.pyi (1ms)
[BG(3)]     parsing: c:\Users\raych\.vscode\extensions\ms-python.vscode-pylance-2021.10.3\dist\typeshed-fallback\stdlib\enum.pyi [fs read 4ms] (39ms)
[BG(3)]     binding: c:\Users\raych\.vscode\extensions\ms-python.vscode-pylance-2021.10.3\dist\typeshed-fallback\stdlib\enum.pyi (2ms)
[BG(3)]     parsing: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\values\quaternion.pyi [fs read 4ms] (7ms)
[BG(3)]     binding: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\values\quaternion.pyi (4ms)
Background analysis message: setConfigOptions
Background analysis message: setImportResolver
Background analysis message: ensurePartialStubPackages
Background analysis message: setTrackedFiles
Background analysis message: markAllFilesDirty
Background analysis message: analyze
Background analysis message: analyze
[BG(3)]     parsing: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\render.pyi [fs read 2ms] (109ms)
[BG(3)]     binding: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\render.pyi (2ms)
[BG(3)]     parsing: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\scenes\__init__.pyi [fs read 1ms] (8ms)
[BG(3)]     binding: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\scenes\__init__.pyi (0ms)
[BG(3)]     parsing: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\scenes\sceneManager.pyi [fs read 4ms] (17ms)
[BG(3)]     binding: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\scenes\sceneManager.pyi (1ms)
[BG(3)]     parsing: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\scenes\scene.pyi [fs read 2ms] (4ms)
[BG(3)]     binding: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\scenes\scene.pyi (1ms)
[BG(3)]     parsing: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\meshes.pyi [fs read 1ms] (11ms)
[BG(3)]     binding: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\meshes.pyi (0ms)
[BG(3)]   checking: c:\Ray-Data\python\pyunity-tools\pyunity_tools\apps\color\color_source.py (904ms)
[BG(3)] analyzing: c:\Ray-Data\python\pyunity-tools\pyunity_tools\apps\color\color_source.py (3866ms)
[Info  - 20:26:49] [BG(3)] Long operation: analyzing: c:\Ray-Data\python\pyunity-tools\pyunity_tools\apps\color\color_source.py (3866ms)
Background analysis message: getSemanticTokens delta
[BG(3)] getSemanticTokens delta previousResultId:1635448846669 at c:\Ray-Data\python\pyunity-tools\pyunity_tools\apps\color\color_source.py (77ms)
Background analysis message: resumeAnalysis
[FG] parsing: c:\Ray-Data\python\pyunity-tools\pyunity_tools\apps\color\color_source.py (76ms)
[FG] parsing: c:\Users\raych\.vscode\extensions\ms-python.vscode-pylance-2021.10.3\dist\typeshed-fallback\stdlib\builtins.pyi [fs read 1ms] (557ms)
[FG] binding: c:\Users\raych\.vscode\extensions\ms-python.vscode-pylance-2021.10.3\dist\typeshed-fallback\stdlib\builtins.pyi (219ms)
[FG] binding: c:\Ray-Data\python\pyunity-tools\pyunity_tools\apps\color\color_source.py ...
[FG]   parsing: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\__init__.pyi [fs read 2ms] (174ms)
[FG]   binding: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\__init__.pyi ...
[FG]     parsing: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\audio.pyi [fs read 22ms] (95ms)
[FG]     binding: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\audio.pyi (1ms)
[FG]     parsing: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\core.pyi [fs read 2ms] (37ms)
[FG]     binding: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\core.pyi (9ms)
[FG]     parsing: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\gui.pyi [fs read 2ms] (71ms)
[FG]     binding: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\gui.pyi (6ms)
[FG]     parsing: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\input.pyi [fs read 1ms] (17ms)
[FG]     binding: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\input.pyi (4ms)
[FG]     parsing: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\physics\__init__.pyi [fs read 1ms] (16ms)
[FG]     binding: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\physics\__init__.pyi ...
[FG]       parsing: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\physics\core.pyi [fs read 1ms] (21ms)
[FG]       binding: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\physics\core.pyi (2ms)
[FG]     binding: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\physics\__init__.pyi (28ms)
[FG]     parsing: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\errors.pyi [fs read 1ms] (2ms)
[FG]     binding: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\errors.pyi (0ms)
[FG]     parsing: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\files.pyi [fs read 1ms] (16ms)
[FG]     binding: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\files.pyi (2ms)
[FG]     parsing: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\values\__init__.pyi [fs read 1ms] (10ms)
[FG]     binding: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\values\__init__.pyi ...
[FG]       parsing: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\values\vector.pyi [fs read 1ms] (26ms)
[FG]       binding: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\values\vector.pyi (5ms)
[FG]       parsing: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\values\texture.pyi [fs read 3ms] (11ms)
[FG]       binding: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\values\texture.pyi (1ms)
[FG]     binding: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\values\__init__.pyi (51ms)
[FG]   binding: c:\Users\raych\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyunity\__init__.pyi (394ms)
[FG] binding: c:\Ray-Data\python\pyunity-tools\pyunity_tools\apps\color\color_source.py (578ms)
Background analysis message: getDiagnosticsForRange
Background analysis message: getDiagnosticsForRange
Background analysis message: getDiagnosticsForRange

Code Snippet / Additional information

# foo/bar.pyi

from typing import TypeVar, Type

class Baz:
    T = TypeVar("T", bound=Baz)
    def AddComponent(self, class_: Type[T]) -> T: ...

When I call AddComponent, it returns a variable of type T@AddComponent image Shouldn't Pylance recognise T as being the class type?

erictraut commented 3 years ago

I can't tell how you're calling AddComponent. Could you provide a more complete sample that demonstrates where you're seeing the problem?

You've defined a TypeVar T that is bound to Baz, which means that the TypeVar constraint solver will verify that the argument corresponding to parameter class_ is the Baz class or some class that derives from it.

Here's an example that demonstrates how this works.

from typing import TypeVar, Type

class Baz:
    T = TypeVar("T", bound="Baz")
    def AddComponent(self, class_: Type[T]) -> T:
        ...

class SubclassOfBaz(Baz):
    ...

result = Baz().AddComponent(SubclassOfBaz)
reveal_type(result)  # SubclassOfBaz
rayzchen commented 3 years ago

Oh, I thought Type[T] was just a generic class which you could reference with T. For example:

from typing import TypeVar, Type

class Baz:
    T = TypeVar("T", bound="Baz")
    def AddComponent(self, class_: Type[T]) -> T:
        ...

result = Baz().AddComponent(int)
reveal_type(result)  # int

EDIT: Just checked the docs of Mypy, seems that bound restricts it to subclasses only. Still, removing bound doesn't fix anything and the return type is still T@AddComponent according to vscode.

rayzchen commented 3 years ago

Ahhhh, a reload fixed the problem. I'm having a bad day today... just opened two issues that were resolved with a reload. I really need to get a better computer that doesn't take 5 minutes to reload vscode. 😩 At least I learned that bound restricts TypeVars to a subclass of a specific type.