Comments (4)
@overload
(or something sufficiently similar) is still useful in certain cases, including certain built-in methods.
For example, consider the signature of __init__
of str
:
@overload
def __init__(self) -> None: pass
@overload
def __init__(self, o: object) -> None: pass
@overload
def __init__(self, o: _byte_types, encoding: str = None, errors: str = 'strict') -> None: pass
Union types are not sufficient to represent the fact that encoding
and errors
are only valid if the first argument is a bytes-like object.
__getitem__
of bytes
is another example:
@overload
def __getitem__(self, i: int) -> int: pass
@overload
def __getitem__(self, s: slice) -> bytes: pass
Again, union types (or type variables) are not sufficient.
map
is a third example where we use overloading to support different numbers of arguments:
@overload
def map(func: Function[[_T1], _S], iter1: Iterable[_T1]) -> Iterator[_S]: pass
@overload
def map(func: Function[[_T1, _T2], _S], iter1: Iterable[_T1],
iter2: Iterable[_T2]) -> Iterator[_S]: pass
# ... and we could add more items to support more than two iterables
I think we need at least a stub-level feature for representing the above cases. A potential approach is to use multiple signatures for a function in a stub file, without a decorator. For example:
def __getitem__(self, i: int) -> int: pass
def __getitem__(self, s: slice) -> bytes: pass
from typing.
The examples that @JukkaL listed here are one of the reasons pytypedecl went with a separate file for type definitions.
In general I saw some minor interest in having multiple dispatch implemented in functools after we introduced singledispatch. I'd be happy to work on that separate PEP as a follow-up.
Can we leave it out for now to not risk introducing a confusing feature that's impossible to remove later?
from typing.
We need something for stubs; there are a fair number of stdlib things
that are naturally expressed as overloads. (And then you have map() and
filter(), which are technically an overload of an infinite number of
different variants. But I'm okay with just listing 10 in the stub file.
This was discussed elsewhere, though I can't find it now.)
Okay to disallow it for non-stub files and doing a separate PEP for multi-dispatch.
from typing.
Closing .The decision is made. We'll support @overload
in stubs only. The editing TODO is in README.rst. We'll also separately try to work on a PEP for multiple dispatch.
from typing.
Related Issues (20)
- Spec: Specify how inheriting from Any works HOT 2
- Specify what happens for binary operators (e.g. `__add__`) HOT 3
- Specify NamedTuple HOT 2
- Allow unary `+` in integer Literals HOT 1
- Pass variable as final HOT 4
- Spec: Allow `NoReturn` to appear outside of function return type annotations HOT 6
- Treat alternative syntax for unions (and other "new-semantics-old-syntax" typing features) in explicit `TypeAlias`es inside `if TYPE_CHECKING` blocks as stringized annotations. HOT 6
- Spec: Annotating the `self` argument in `__init__` methods HOT 17
- FYI: I removed the "stable" docs version
- Pre-draft proposal: Amend the prior removal of support for unpacking of typevartuples in union. (revisiting pep 646) HOT 1
- PR preview HOT 3
- Outdated stub package guidance HOT 1
- Integrate the Type Stubs document into the spec HOT 1
- Types for "truthy" and "falsy" values HOT 6
- Abstract classmethod returning object of type cls: "is not a supertype of its class" HOT 1
- hello
- Spec: Add PEP 696 (Type parameter defaults) HOT 2
- Spec: Add PEP 705 (Read-only TypedDict items) HOT 3
- Extra key of TypedDict HOT 2
- Reason given for disallowing non-concrete subtype assignment is unsound HOT 10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typing.