Skip to content

Commit

Permalink
builtins: rename _LT (#4579)
Browse files Browse the repository at this point in the history
Having an obscure type variable name is causing some pretty inscrutable
errors. For instance:
```
xarray/core/utils.py:466: error: Value of type variable "_LT" of "sorted" cannot be "K"
tornado/simple_httpclient.py:324: error: Value of type variable "_LT" of "min" cannot be "Optional[float]"
```

I think having a more descriptive type variable name here is better for
user experience and helps address the "why" of an error.
  • Loading branch information
hauntsaninja authored Sep 26, 2020
1 parent c5cc223 commit d6d2249
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 30 deletions.
34 changes: 19 additions & 15 deletions stdlib/2/__builtin__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ _T3 = TypeVar("_T3")
_T4 = TypeVar("_T4")
_T5 = TypeVar("_T5")
_TT = TypeVar("_TT", bound="type")
_LT = TypeVar("_LT", bound=_SupportsLessThan)
_SupportsLessThanT = TypeVar("_SupportsLessThanT", bound=_SupportsLessThan)
_TBE = TypeVar("_TBE", bound="BaseException")

class object:
Expand Down Expand Up @@ -962,7 +962,7 @@ class list(MutableSequence[_T], Generic[_T]):
def reverse(self) -> None: ...
if sys.version_info >= (3,):
@overload
def sort(self: List[_LT], *, key: None = ..., reverse: bool = ...) -> None: ...
def sort(self: List[_SupportsLessThanT], *, key: None = ..., reverse: bool = ...) -> None: ...
@overload
def sort(self, *, key: Callable[[_T], _SupportsLessThan], reverse: bool = ...) -> None: ...
else:
Expand Down Expand Up @@ -1440,17 +1440,19 @@ else:

if sys.version_info >= (3,):
@overload
def max(__arg1: _LT, __arg2: _LT, *_args: _LT, key: None = ...) -> _LT: ...
def max(
__arg1: _SupportsLessThanT, __arg2: _SupportsLessThanT, *_args: _SupportsLessThanT, key: None = ...
) -> _SupportsLessThanT: ...
@overload
def max(__arg1: _T, __arg2: _T, *_args: _T, key: Callable[[_T], _LT]) -> _T: ...
def max(__arg1: _T, __arg2: _T, *_args: _T, key: Callable[[_T], _SupportsLessThanT]) -> _T: ...
@overload
def max(__iterable: Iterable[_LT], *, key: None = ...) -> _LT: ...
def max(__iterable: Iterable[_SupportsLessThanT], *, key: None = ...) -> _SupportsLessThanT: ...
@overload
def max(__iterable: Iterable[_T], *, key: Callable[[_T], _LT]) -> _T: ...
def max(__iterable: Iterable[_T], *, key: Callable[[_T], _SupportsLessThanT]) -> _T: ...
@overload
def max(__iterable: Iterable[_LT], *, key: None = ..., default: _T) -> Union[_LT, _T]: ...
def max(__iterable: Iterable[_SupportsLessThanT], *, key: None = ..., default: _T) -> Union[_SupportsLessThanT, _T]: ...
@overload
def max(__iterable: Iterable[_T1], *, key: Callable[[_T1], _LT], default: _T2) -> Union[_T1, _T2]: ...
def max(__iterable: Iterable[_T1], *, key: Callable[[_T1], _SupportsLessThanT], default: _T2) -> Union[_T1, _T2]: ...

else:
@overload
Expand All @@ -1460,17 +1462,19 @@ else:

if sys.version_info >= (3,):
@overload
def min(__arg1: _LT, __arg2: _LT, *_args: _LT, key: None = ...) -> _LT: ...
def min(
__arg1: _SupportsLessThanT, __arg2: _SupportsLessThanT, *_args: _SupportsLessThanT, key: None = ...
) -> _SupportsLessThanT: ...
@overload
def min(__arg1: _T, __arg2: _T, *_args: _T, key: Callable[[_T], _LT]) -> _T: ...
def min(__arg1: _T, __arg2: _T, *_args: _T, key: Callable[[_T], _SupportsLessThanT]) -> _T: ...
@overload
def min(__iterable: Iterable[_LT], *, key: None = ...) -> _LT: ...
def min(__iterable: Iterable[_SupportsLessThanT], *, key: None = ...) -> _SupportsLessThanT: ...
@overload
def min(__iterable: Iterable[_T], *, key: Callable[[_T], _LT]) -> _T: ...
def min(__iterable: Iterable[_T], *, key: Callable[[_T], _SupportsLessThanT]) -> _T: ...
@overload
def min(__iterable: Iterable[_LT], *, key: None = ..., default: _T) -> Union[_LT, _T]: ...
def min(__iterable: Iterable[_SupportsLessThanT], *, key: None = ..., default: _T) -> Union[_SupportsLessThanT, _T]: ...
@overload
def min(__iterable: Iterable[_T1], *, key: Callable[[_T1], _LT], default: _T2) -> Union[_T1, _T2]: ...
def min(__iterable: Iterable[_T1], *, key: Callable[[_T1], _SupportsLessThanT], default: _T2) -> Union[_T1, _T2]: ...

else:
@overload
Expand Down Expand Up @@ -1671,7 +1675,7 @@ def setattr(__obj: Any, __name: Text, __value: Any) -> None: ...

if sys.version_info >= (3,):
@overload
def sorted(__iterable: Iterable[_LT], *, key: None = ..., reverse: bool = ...) -> List[_LT]: ...
def sorted(__iterable: Iterable[_SupportsLessThanT], *, key: None = ..., reverse: bool = ...) -> List[_SupportsLessThanT]: ...
@overload
def sorted(__iterable: Iterable[_T], *, key: Callable[[_T], _SupportsLessThan], reverse: bool = ...) -> List[_T]: ...

Expand Down
34 changes: 19 additions & 15 deletions stdlib/2and3/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ _T3 = TypeVar("_T3")
_T4 = TypeVar("_T4")
_T5 = TypeVar("_T5")
_TT = TypeVar("_TT", bound="type")
_LT = TypeVar("_LT", bound=_SupportsLessThan)
_SupportsLessThanT = TypeVar("_SupportsLessThanT", bound=_SupportsLessThan)
_TBE = TypeVar("_TBE", bound="BaseException")

class object:
Expand Down Expand Up @@ -962,7 +962,7 @@ class list(MutableSequence[_T], Generic[_T]):
def reverse(self) -> None: ...
if sys.version_info >= (3,):
@overload
def sort(self: List[_LT], *, key: None = ..., reverse: bool = ...) -> None: ...
def sort(self: List[_SupportsLessThanT], *, key: None = ..., reverse: bool = ...) -> None: ...
@overload
def sort(self, *, key: Callable[[_T], _SupportsLessThan], reverse: bool = ...) -> None: ...
else:
Expand Down Expand Up @@ -1440,17 +1440,19 @@ else:

if sys.version_info >= (3,):
@overload
def max(__arg1: _LT, __arg2: _LT, *_args: _LT, key: None = ...) -> _LT: ...
def max(
__arg1: _SupportsLessThanT, __arg2: _SupportsLessThanT, *_args: _SupportsLessThanT, key: None = ...
) -> _SupportsLessThanT: ...
@overload
def max(__arg1: _T, __arg2: _T, *_args: _T, key: Callable[[_T], _LT]) -> _T: ...
def max(__arg1: _T, __arg2: _T, *_args: _T, key: Callable[[_T], _SupportsLessThanT]) -> _T: ...
@overload
def max(__iterable: Iterable[_LT], *, key: None = ...) -> _LT: ...
def max(__iterable: Iterable[_SupportsLessThanT], *, key: None = ...) -> _SupportsLessThanT: ...
@overload
def max(__iterable: Iterable[_T], *, key: Callable[[_T], _LT]) -> _T: ...
def max(__iterable: Iterable[_T], *, key: Callable[[_T], _SupportsLessThanT]) -> _T: ...
@overload
def max(__iterable: Iterable[_LT], *, key: None = ..., default: _T) -> Union[_LT, _T]: ...
def max(__iterable: Iterable[_SupportsLessThanT], *, key: None = ..., default: _T) -> Union[_SupportsLessThanT, _T]: ...
@overload
def max(__iterable: Iterable[_T1], *, key: Callable[[_T1], _LT], default: _T2) -> Union[_T1, _T2]: ...
def max(__iterable: Iterable[_T1], *, key: Callable[[_T1], _SupportsLessThanT], default: _T2) -> Union[_T1, _T2]: ...

else:
@overload
Expand All @@ -1460,17 +1462,19 @@ else:

if sys.version_info >= (3,):
@overload
def min(__arg1: _LT, __arg2: _LT, *_args: _LT, key: None = ...) -> _LT: ...
def min(
__arg1: _SupportsLessThanT, __arg2: _SupportsLessThanT, *_args: _SupportsLessThanT, key: None = ...
) -> _SupportsLessThanT: ...
@overload
def min(__arg1: _T, __arg2: _T, *_args: _T, key: Callable[[_T], _LT]) -> _T: ...
def min(__arg1: _T, __arg2: _T, *_args: _T, key: Callable[[_T], _SupportsLessThanT]) -> _T: ...
@overload
def min(__iterable: Iterable[_LT], *, key: None = ...) -> _LT: ...
def min(__iterable: Iterable[_SupportsLessThanT], *, key: None = ...) -> _SupportsLessThanT: ...
@overload
def min(__iterable: Iterable[_T], *, key: Callable[[_T], _LT]) -> _T: ...
def min(__iterable: Iterable[_T], *, key: Callable[[_T], _SupportsLessThanT]) -> _T: ...
@overload
def min(__iterable: Iterable[_LT], *, key: None = ..., default: _T) -> Union[_LT, _T]: ...
def min(__iterable: Iterable[_SupportsLessThanT], *, key: None = ..., default: _T) -> Union[_SupportsLessThanT, _T]: ...
@overload
def min(__iterable: Iterable[_T1], *, key: Callable[[_T1], _LT], default: _T2) -> Union[_T1, _T2]: ...
def min(__iterable: Iterable[_T1], *, key: Callable[[_T1], _SupportsLessThanT], default: _T2) -> Union[_T1, _T2]: ...

else:
@overload
Expand Down Expand Up @@ -1671,7 +1675,7 @@ def setattr(__obj: Any, __name: Text, __value: Any) -> None: ...

if sys.version_info >= (3,):
@overload
def sorted(__iterable: Iterable[_LT], *, key: None = ..., reverse: bool = ...) -> List[_LT]: ...
def sorted(__iterable: Iterable[_SupportsLessThanT], *, key: None = ..., reverse: bool = ...) -> List[_SupportsLessThanT]: ...
@overload
def sorted(__iterable: Iterable[_T], *, key: Callable[[_T], _SupportsLessThan], reverse: bool = ...) -> List[_T]: ...

Expand Down

0 comments on commit d6d2249

Please sign in to comment.