Git Product home page Git Product logo

Comments (4)

JukkaL avatar JukkaL commented on May 27, 2024

@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.

ambv avatar ambv commented on May 27, 2024

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.

gvanrossum avatar gvanrossum commented on May 27, 2024

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.

gvanrossum avatar gvanrossum commented on May 27, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.