Skip to content

TypeVars in some generic functions can go unsolved, resulting in an Any in the evaluated type #7928

@erictraut

Description

@erictraut

Pyright users have recently reported a couple of issues where they received an Unknown (i.e. an implicit Any) type when evaluating a call expression. These problems were both related to a case where a TypeVar was used to annotate a parameter with a default argument, but the caller didn't provide an argument for that parameter and relied on the default. This left the TypeVar unsolved.

Here's an example from the pyright issue tracker.

@JelleZijlstra suggested that pyright add a check for this condition. I've enhanced the reportInvalidTypeVarUse check to detect and report this case. See this issue for details.

The new check identifies 10 cases in the stdlib typeshed stubs and 27 cases in third-party stubs. Jelle asked that I create this issue with the full list, which I have pasted below.

<redacted>/typeshed/stdlib/argparse.pyi
  <redacted>/typeshed/stdlib/argparse.pyi:215:32 - error: Type variable "_ArgumentParserT" may go unsolved if caller supplies no argument for parameter "parser_class"
    Provide an overload that specifies the return type when the argument is not supplied (reportInvalidTypeVarUse)
<redacted>/typeshed/stdlib/fileinput.pyi
  <redacted>/typeshed/stdlib/fileinput.pyi:32:54 - error: Type variable "AnyStr" may go unsolved if caller supplies no argument for parameter "openhook"
    Provide an overload that specifies the return type when the argument is not supplied (reportInvalidTypeVarUse)
<redacted>/typeshed/stdlib/gettext.pyi
  <redacted>/typeshed/stdlib/gettext.pyi:157:22 - error: Type variable "_T" may go unsolved if caller supplies no argument for parameter "class_"
    Provide an overload that specifies the return type when the argument is not supplied (reportInvalidTypeVarUse)
<redacted>/typeshed/stdlib/optparse.pyi
  <redacted>/typeshed/stdlib/optparse.pyi:243:41 - error: Type variable "AnyStr" may go unsolved if caller supplies no argument for parameter "args"
    Provide an overload that specifies the return type when the argument is not supplied (reportInvalidTypeVarUse)
<redacted>/typeshed/stdlib/tempfile.pyi
  <redacted>/typeshed/stdlib/tempfile.pyi:387:13 - error: Type variable "AnyStr" may go unsolved if caller supplies no argument for parameter "suffix"
    Provide an overload that specifies the return type when the argument is not supplied (reportInvalidTypeVarUse)
  <redacted>/typeshed/stdlib/tempfile.pyi:392:21 - error: Type variable "AnyStr" may go unsolved if caller supplies no argument for parameter "suffix"
    Provide an overload that specifies the return type when the argument is not supplied (reportInvalidTypeVarUse)
  <redacted>/typeshed/stdlib/tempfile.pyi:396:20 - error: Type variable "AnyStr" may go unsolved if caller supplies no argument for parameter "suffix"
    Provide an overload that specifies the return type when the argument is not supplied (reportInvalidTypeVarUse)
<redacted>/typeshed/stdlib/multiprocessing/managers.pyi
  <redacted>/typeshed/stdlib/multiprocessing/managers.pyi:127:38 - error: Type variable "_KT" may go unsolved if caller supplies no argument for parameter "sequence"
    Provide an overload that specifies the return type when the argument is not supplied (reportInvalidTypeVarUse)
  <redacted>/typeshed/stdlib/multiprocessing/managers.pyi:127:43 - error: Type variable "_VT" may go unsolved if caller supplies no argument for parameter "sequence"
    Provide an overload that specifies the return type when the argument is not supplied (reportInvalidTypeVarUse)
  <redacted>/typeshed/stdlib/multiprocessing/managers.pyi:128:39 - error: Type variable "_T" may go unsolved if caller supplies no argument for parameter "sequence"
    Provide an overload that specifies the return type when the argument is not supplied (reportInvalidTypeVarUse)
<redacted>/typeshed/stubs/SQLAlchemy/sqlalchemy/orm/decl_api.pyi
  <redacted>/typeshed/stubs/SQLAlchemy/sqlalchemy/orm/decl_api.pyi:117:76 - error: Type variable "_DeclT" may go unsolved if caller supplies no argument for parameter "metaclass"
    Provide an overload that specifies the return type when the argument is not supplied (reportInvalidTypeVarUse)
  <redacted>/typeshed/stubs/SQLAlchemy/sqlalchemy/orm/decl_api.pyi:128:37 - error: Type variable "_DeclT" may go unsolved if caller supplies no argument for parameter "metaclass"
    Provide an overload that specifies the return type when the argument is not supplied (reportInvalidTypeVarUse)
<redacted>/typeshed/stubs/invoke/invoke/tasks.pyi
  <redacted>/typeshed/stubs/invoke/invoke/tasks.pyi:75:17 - error: Type variable "_TaskT" may go unsolved if caller supplies no argument for parameter "klass"
    Provide an overload that specifies the return type when the argument is not supplied (reportInvalidTypeVarUse)
<redacted>/typeshed/stubs/mock/mock/mock.pyi
  <redacted>/typeshed/stubs/mock/mock/mock.pyi:233:14 - error: Type variable "_T" may go unsolved if caller supplies no argument for parameter "new"
    Provide an overload that specifies the return type when the argument is not supplied (reportInvalidTypeVarUse)
<redacted>/typeshed/stubs/mypy-extensions/mypy_extensions.pyi
  <redacted>/typeshed/stubs/mypy-extensions/mypy_extensions.pyi:24:15 - error: Type variable "_T" may go unsolved if caller supplies no argument for parameter "type"
    Provide an overload that specifies the return type when the argument is not supplied (reportInvalidTypeVarUse)
  <redacted>/typeshed/stubs/mypy-extensions/mypy_extensions.pyi:25:22 - error: Type variable "_T" may go unsolved if caller supplies no argument for parameter "type"
    Provide an overload that specifies the return type when the argument is not supplied (reportInvalidTypeVarUse)
  <redacted>/typeshed/stubs/mypy-extensions/mypy_extensions.pyi:26:20 - error: Type variable "_T" may go unsolved if caller supplies no argument for parameter "type"
    Provide an overload that specifies the return type when the argument is not supplied (reportInvalidTypeVarUse)
  <redacted>/typeshed/stubs/mypy-extensions/mypy_extensions.pyi:27:27 - error: Type variable "_T" may go unsolved if caller supplies no argument for parameter "type"
    Provide an overload that specifies the return type when the argument is not supplied (reportInvalidTypeVarUse)
  <redacted>/typeshed/stubs/mypy-extensions/mypy_extensions.pyi:28:18 - error: Type variable "_T" may go unsolved if caller supplies no argument for parameter "type"
    Provide an overload that specifies the return type when the argument is not supplied (reportInvalidTypeVarUse)
  <redacted>/typeshed/stubs/mypy-extensions/mypy_extensions.pyi:29:17 - error: Type variable "_T" may go unsolved if caller supplies no argument for parameter "type"
    Provide an overload that specifies the return type when the argument is not supplied (reportInvalidTypeVarUse)
<redacted>/typeshed/stubs/psycopg2/psycopg2/_psycopg.pyi
  <redacted>/typeshed/stubs/psycopg2/psycopg2/_psycopg.pyi:409:62 - error: Type variable "_T_cur" may go unsolved if caller supplies no argument for parameter "cursor_factory"
    Provide an overload that specifies the return type when the argument is not supplied (reportInvalidTypeVarUse)
<redacted>/typeshed/stubs/redis/redis/commands/core.pyi
  <redacted>/typeshed/stubs/redis/redis/commands/core.pyi:644:47 - error: Type variable "_ScoreCastFuncReturn" may go unsolved if caller supplies no argument for parameter "score_cast_func"
    Provide an overload that specifies the return type when the argument is not supplied (reportInvalidTypeVarUse)
  <redacted>/typeshed/stubs/redis/redis/commands/core.pyi:651:47 - error: Type variable "_ScoreCastFuncReturn" may go unsolved if caller supplies no argument for parameter "score_cast_func"
    Provide an overload that specifies the return type when the argument is not supplied (reportInvalidTypeVarUse)
  <redacted>/typeshed/stubs/redis/redis/commands/core.pyi:682:47 - error: Type variable "_ScoreCastFuncReturn" may go unsolved if caller supplies no argument for parameter "score_cast_func"
    Provide an overload that specifies the return type when the argument is not supplied (reportInvalidTypeVarUse)
  <redacted>/typeshed/stubs/redis/redis/commands/core.pyi:689:47 - error: Type variable "_ScoreCastFuncReturn" may go unsolved if caller supplies no argument for parameter "score_cast_func"
    Provide an overload that specifies the return type when the argument is not supplied (reportInvalidTypeVarUse)
  <redacted>/typeshed/stubs/redis/redis/commands/core.pyi:859:47 - error: Type variable "_ScoreCastFuncReturn" may go unsolved if caller supplies no argument for parameter "score_cast_func"
    Provide an overload that specifies the return type when the argument is not supplied (reportInvalidTypeVarUse)
  <redacted>/typeshed/stubs/redis/redis/commands/core.pyi:874:47 - error: Type variable "_ScoreCastFuncReturn" may go unsolved if caller supplies no argument for parameter "score_cast_func"
    Provide an overload that specifies the return type when the argument is not supplied (reportInvalidTypeVarUse)
  <redacted>/typeshed/stubs/redis/redis/commands/core.pyi:901:47 - error: Type variable "_ScoreCastFuncReturn" may go unsolved if caller supplies no argument for parameter "score_cast_func"
    Provide an overload that specifies the return type when the argument is not supplied (reportInvalidTypeVarUse)
  <redacted>/typeshed/stubs/redis/redis/commands/core.pyi:935:47 - error: Type variable "_ScoreCastFuncReturn" may go unsolved if caller supplies no argument for parameter "score_cast_func"
    Provide an overload that specifies the return type when the argument is not supplied (reportInvalidTypeVarUse)
  <redacted>/typeshed/stubs/redis/redis/commands/core.pyi:958:47 - error: Type variable "_ScoreCastFuncReturn" may go unsolved if caller supplies no argument for parameter "score_cast_func"
    Provide an overload that specifies the return type when the argument is not supplied (reportInvalidTypeVarUse)
  <redacted>/typeshed/stubs/redis/redis/commands/core.pyi:1023:47 - error: Type variable "_ScoreCastFuncReturn" may go unsolved if caller supplies no argument for parameter "score_cast_func"
    Provide an overload that specifies the return type when the argument is not supplied (reportInvalidTypeVarUse)
  <redacted>/typeshed/stubs/redis/redis/commands/core.pyi:1038:47 - error: Type variable "_ScoreCastFuncReturn" may go unsolved if caller supplies no argument for parameter "score_cast_func"
    Provide an overload that specifies the return type when the argument is not supplied (reportInvalidTypeVarUse)
  <redacted>/typeshed/stubs/redis/redis/commands/core.pyi:1065:47 - error: Type variable "_ScoreCastFuncReturn" may go unsolved if caller supplies no argument for parameter "score_cast_func"
    Provide an overload that specifies the return type when the argument is not supplied (reportInvalidTypeVarUse)
  <redacted>/typeshed/stubs/redis/redis/commands/core.pyi:1099:47 - error: Type variable "_ScoreCastFuncReturn" may go unsolved if caller supplies no argument for parameter "score_cast_func"
    Provide an overload that specifies the return type when the argument is not supplied (reportInvalidTypeVarUse)
  <redacted>/typeshed/stubs/redis/redis/commands/core.pyi:1122:47 - error: Type variable "_ScoreCastFuncReturn" may go unsolved if caller supplies no argument for parameter "score_cast_func"
    Provide an overload that specifies the return type when the argument is not supplied (reportInvalidTypeVarUse)
<redacted>/typeshed/stubs/setuptools/pkg_resources/__init__.pyi
  <redacted>/typeshed/stubs/setuptools/pkg_resources/__init__.pyi:63:104 - error: Type variable "_T" may go unsolved if caller supplies no argument for parameter "installer"
    Provide an overload that specifies the return type when the argument is not supplied (reportInvalidTypeVarUse)
  <redacted>/typeshed/stubs/setuptools/pkg_resources/__init__.pyi:67:83 - error: Type variable "_T" may go unsolved if caller supplies no argument for parameter "installer"
    Provide an overload that specifies the return type when the argument is not supplied (reportInvalidTypeVarUse)
37 errors, 0 warnings, 0 informations 

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority: highThis issue is more important than most

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions