Comments (11)
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.
Sorry, I don't like any of that. Can we just drop it?
- Using
Any[t1, t2, ...]
instead ofUnion[t1, t2, ...]
would be confusing if we kept plainAny
for its current meaning (it's consistent with everything). - Using
T = OneOf[t1, t2, ...]
instead ofTypeVar('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 fromdef 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.
(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.
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.
Ł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.
Agreed. Let's skip it for now then.
from typing.
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.
OK, that covers it. No Intersection for now, names stay as they were.
from typing.
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.
from typing.
There is an open issue about this, #213.
from typing.
+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)
- Out-of-date description of how to indicate positional-only parameters
- Typing spec should be clearer that type checkers are not expected to support PEP 3141 HOT 22
- Incorporate PEP 483 into the spec HOT 5
- Conformance tests: Add mechanism to allow errors on one of multiple lines HOT 2
- Conformance tests: Add mechanism to ignore errors
- Conformance tests: Align automated and manual scoring HOT 1
- Introduce a `Language` type to provide consistent language information of strings. HOT 5
- Spec: Version and platform checking underspecified HOT 6
- Support list comprehension for `TypeVarTuple` HOT 2
- Clarify the float/int/complex special case HOT 5
- Clarify the implications of subclassing Any HOT 1
- Allow multiple `TypeVarTuple` in Generic Classes when they are wrapped. HOT 5
- pytype conformance tests crash if no `python3.11` executable is in `PATH` HOT 5
- Conformance test: make dataclass_hash.py not rely on `typing.Hashable`? HOT 1
- Proposal: Add coerced type narrowing similar to 'cast' HOT 1
- Idenity Type HOT 2
- [spec] clarify assert_type behavior as asserting type equivalence
- [spec] better clarify the difference between a runtime type object and a static type HOT 1
- [spec] a protocol type should be assignable to object? HOT 1
- [spec] define or replace the term "concrete type" HOT 1
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.