Code Monkey home page Code Monkey logo

Comments (11)

JukkaL avatar JukkaL commented on July 20, 2024 1

I think this would be occasionally useful, but clearly this would be a minor feature. I vote for leaving this out for now and waiting until we have seen some real-world use cases that would benefit from intersection types.

from typing.

gvanrossum avatar gvanrossum commented on July 20, 2024 1

Sorry, I don't like any of that. Can we just drop it?

  • Using Any[t1, t2, ...] instead of Union[t1, t2, ...] would be confusing if we kept plain Any for its current meaning (it's consistent with everything).
  • Using T = OneOf[t1, t2, ...] instead of TypeVar('T', t1, t2, ...) hides the fact that it is a type variable (and all arguments must match). E.g. def foo(a1: AnyStr, a2: AnyStr): is very different from def foo(a1: Union[str, bytes], a2: Union[str, bytes]):.
  • In #18 we already decided not to define an intersection type for now. And in #11 we decided not to define protocols for now. So I think they shouldn't be "smuggled back in" this way.
  • Type variables should not use brackets, they should use parentheses. I've explained this already (#5).

from typing.

gvanrossum avatar gvanrossum commented on July 20, 2024

(In README.text, Łukasz suggested to use Any[] instead of Union[] so that All[] can be used as Intersection[]. Feels a bit cute to me.)

from typing.

ambv avatar ambv commented on July 20, 2024

Having intersections is important since we don't have structural sub-classing and protocols will likely not be composable with ABCs.

Any/All is not only cute, it's also shorter to type and more obvious to the reader :)

from typing.

gvanrossum avatar gvanrossum commented on July 20, 2024

Łukasz, can you show a few examples of how Any/All would be used? Your README note was rather cryptic. I agree Intersection is verbose, but it sounds like a minor use case, and I don't want to lose unparameterized Any as the top (or is that bottom? :-) of the type tree/graph.

from typing.

gvanrossum avatar gvanrossum commented on July 20, 2024

Agreed. Let's skip it for now then.

from typing.

ambv avatar ambv commented on July 20, 2024

What I proposed is alternative names: Union becomes Any, Intersection becomes All. typevar(values=) could become OneOf, with the bracket syntax, which would be more consistent.

Examples:

AnyStr = OneOf[bytes, str]
def memcache_get(key:AnyStr) -> Optional[AnyStr]:
  ...

OneOrMany = OneOf[AnyStr, List[AnyStr]]
def memcache_set(key:OneOrMany, value:OneOrMany) -> int:
  ...

# but:
def retry(callback:Callable[AnyArgs, Any[int, None]], timeout:Any[int, None]=None, retries=Any[int, None]=None) -> None:
  ...

# equiv:
OptionalInt = Any[int, None]
def retry(callback:Callable[AnyArgs, OptionalInt], timeout:OptionalInt, retries:OptionalInt) -> None:
  ...

Note that All provides a form of structural typing:

# The following absurd function uses retryable.__len__ and retryable.__call__ 
def retry(retryable:All[Sized, Callable]):
  while len(retryable):
    retryable()

Of course, we could provide more sensible ABCs for "file-like" objects, etc.

I deal with cache invalidation for a living, naming things and off-by-one errors are just a hobby.

from typing.

ambv avatar ambv commented on July 20, 2024

OK, that covers it. No Intersection for now, names stay as they were.

from typing.

zevbo avatar zevbo commented on July 20, 2024

Its been a number of years since this thread was first opened, so I wanted to bump on it. From my perspective, the lack of an intersection type is one of the key flaws of the python type system at current, and its introduction would be majorly beneficial.

@gvanrossum

from typing.

JelleZijlstra avatar JelleZijlstra commented on July 20, 2024

There is an open issue about this, #213.

from typing.

hauntsaninja avatar hauntsaninja commented on July 20, 2024

+1 comments are better done via emoji than by tagging people. In any case, #213 is the issue tracking this feature.

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.