Skip to content

PEP 604: Add or operator to class type #5151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions stdlib/builtins.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys
import types
from _typeshed import (
AnyPath,
OpenBinaryMode,
Expand Down Expand Up @@ -156,6 +157,8 @@ class type(object):
def __subclasscheck__(self, subclass: type) -> bool: ...
@classmethod
def __prepare__(metacls, __name: str, __bases: Tuple[type, ...], **kwds: Any) -> Mapping[str, Any]: ...
if sys.version_info >= (3, 10):
def __or__(self, t: Any) -> types.Union: ...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def __or__(self, t: Any) -> types.Union: ...
def __or__(self, t: Any) -> Any: ...

As you mentioned, int | int returns int, not a Union. We could do something more clever with overloads, but I'm not sure that's worth it since type checkers will surely special-case this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type of int | int will still be types.Union
image so we can leave it as it is even for such cases.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I was confused because it gets printed as int, but it's indeed a Union object at runtime. Sorry for the confusion.

Jelles-Air:cpython jelle$ ./python.exe 
Python 3.10.0a7+ (heads/master:28d28e053d, Apr  8 2021, 07:40:29) [Clang 11.0.3 (clang-1103.0.32.59)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> int | int
int
>>> type(_)
<class 'types.Union'>


class super(object):
@overload
Expand Down
28 changes: 8 additions & 20 deletions stdlib/types.pyi
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
import sys
from typing import (
Any,
Awaitable,
Callable,
Dict,
Generic,
Iterable,
Iterator,
Mapping,
Optional,
Tuple,
Type,
TypeVar,
Union,
overload,
)
import typing
from typing import Any, Awaitable, Callable, Dict, Generic, Iterable, Iterator, Mapping, Optional, Tuple, Type, TypeVar, overload
from typing_extensions import Literal, final

# ModuleType is exported from this module, but for circular import
Expand Down Expand Up @@ -160,7 +146,7 @@ class GeneratorType:
def send(self, __arg: Any) -> Any: ...
@overload
def throw(
self, __typ: Type[BaseException], __val: Union[BaseException, object] = ..., __tb: Optional[TracebackType] = ...
self, __typ: Type[BaseException], __val: typing.Union[BaseException, object] = ..., __tb: Optional[TracebackType] = ...
) -> Any: ...
@overload
def throw(self, __typ: BaseException, __val: None = ..., __tb: Optional[TracebackType] = ...) -> Any: ...
Expand All @@ -175,7 +161,7 @@ class AsyncGeneratorType(Generic[_T_co, _T_contra]):
def asend(self, __val: _T_contra) -> Awaitable[_T_co]: ...
@overload
def athrow(
self, __typ: Type[BaseException], __val: Union[BaseException, object] = ..., __tb: Optional[TracebackType] = ...
self, __typ: Type[BaseException], __val: typing.Union[BaseException, object] = ..., __tb: Optional[TracebackType] = ...
) -> Awaitable[_T_co]: ...
@overload
def athrow(self, __typ: BaseException, __val: None = ..., __tb: Optional[TracebackType] = ...) -> Awaitable[_T_co]: ...
Expand All @@ -190,7 +176,7 @@ class CoroutineType:
def send(self, __arg: Any) -> Any: ...
@overload
def throw(
self, __typ: Type[BaseException], __val: Union[BaseException, object] = ..., __tb: Optional[TracebackType] = ...
self, __typ: Type[BaseException], __val: typing.Union[BaseException, object] = ..., __tb: Optional[TracebackType] = ...
) -> Any: ...
@overload
def throw(self, __typ: BaseException, __val: None = ..., __tb: Optional[TracebackType] = ...) -> Any: ...
Expand Down Expand Up @@ -220,7 +206,7 @@ class MethodType:
def __call__(self, *args: Any, **kwargs: Any) -> Any: ...

class BuiltinFunctionType:
__self__: Union[object, ModuleType]
__self__: typing.Union[object, ModuleType]
__name__: str
__qualname__: str
def __call__(self, *args: Any, **kwargs: Any) -> Any: ...
Expand Down Expand Up @@ -334,3 +320,5 @@ if sys.version_info >= (3, 10):
from builtins import _NotImplementedType

NotImplementedType = _NotImplementedType # noqa F811 from builtins
class Union:
__args__: Tuple[Any, ...]