Skip to content

Unbound TypeVar apparently undetected in function overload #3288

@AlexWaygood

Description

@AlexWaygood

I just merged a PR over at typeshed to improve the stub for builtins.sum. The first draft of the PR proposed defining sum like this:

import sys
from typing import *

class _SupportsSum(Protocol):
     def __add__(self, __x: Any) -> Any: ...

 _SumT = TypeVar("_SumT", bound=_SupportsSum)
 _SumS = TypeVar("_SumS", bound=_SupportsSum)

 @overload
 def sum(__iterable: Iterable[_SumT]) -> Any | Literal[0]: ...

if sys.version_info >= (3, 8):
     @overload
     def sum(__iterable: Iterable[_SumT], start: _SumS) -> _SumT | _SumS: ...

 else:
     @overload
     def sum(__iterable: Iterable[_SumT], __start: _SumS) -> _SumT | _SumS: ...

I think this should have triggered pyright's reportInvalidTypeVarUse check, as the TypeVar in the first overload is only used once. However, pyright reported no errors in our CI check.

(The PR was revised to correct this mistake before it was merged.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    as designedNot a bug, working as intended

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions