microsoft / pyright

Static Type Checker for Python

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Old-style annotation weirdness

svenzorgdoc opened this issue · comments

Describe the bug
Old style annotations in type comments are still supported in pyright, but there seems to be an edge case where the use of TypeVars is not counted correctly.

Code or Screenshots

from typing import TypeVar


T = TypeVar("T")

def old(
    arg,  # type: T
):  # type: (...) -> T
    return arg

def old2(
    arg,
):  # type: (T) -> T
    return arg


def new(
    arg: T
) -> T:
    return arg


def newest[S](
    arg: S
) -> S:
    return arg

pyright complains about the function old, but not about the rest, including old2, which uses type comments in a different way.

VS Code extension or command-line

$ npx pyright@latest /tmp/test.py
Need to install the following packages:
  pyright@1.1.361
Ok to proceed? (y) 
/tmp/test.py
  /tmp/test.py:7:24 - warning: TypeVar "T" appears only once in generic function signature
    Use "object" instead (reportInvalidTypeVarUse)
0 errors, 1 warning, 0 informations 

Apologies if this comes across as preachy, but if you're still using Python 2.x-style type comments, I recommend that you move away from them. They are likely to be deprecated in the near future and eventually no longer supported.

This will be addressed in the next release.

This is addressed in pyright 1.1.362, which I just published.

Apologies if this comes across as preachy, but if you're still using Python 2.x-style type comments, I recommend that you move away from them. They are likely to be deprecated in the near future and eventually no longer supported.

I agree! We still have legacy Python 2.7 code laying around that needs porting to Python 3.12. Using six to make the code syntax Python 3.x compatible, it is possible to run pyright on that old code. By annotating it with type comments, we're more confident that the code is correct before porting and refactoring it. So pyright is helping us move this legacy stuff into the present :-)