8000 improve Introduction section · CarliJoy/intersection_examples@fa4e6f6 · GitHub
[go: up one dir, main page]

Skip to content

Commit fa4e6f6

Browse files
authored
improve Introduction section
1 parent 8c7699b commit fa4e6f6

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

specification.rst

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,8 @@ being addressed.]
4242
Introduction
4343
============
4444

45-
PEP-484 introduced the concept of a union type, written `Union[A, B]` which describes values of
46-
either type `A` or type `B`.
47-
Intersection types provide a different (complementary) way of combining types.
48-
The type `A & B` describes values which have both type `A` and type `B`.
45+
PEP-484 introduced the concept of a union type, written ``Union[A, B]`` which describes values of
46+
either type ``A`` or type ``B``.
4947

5048
For example,
5149

@@ -56,11 +54,30 @@ For example,
5654
class C(A, B): ...
5755
class D(A): ...
5856

59-
def f(value: A & B): ...
57+
# PEP-604 was introduced to allow writing `Union[X, Y]` as `X | Y`
58+
def fu(value: A | B): ...
59+
60+
fu(A()) # Valid
61+
fu(B()) # Valid
62+
fu(C()) # Valid
63+
fu(D()) # Valid
64+
65+
Intersection types provide a different (complementary) way of combining types.
66+
The type ``A & B`` describes values which have both type ``A`` and type ``B``.
67+
68+
For example,
69+
70+
::
71+
72+
def fi(value: A & B): ...
6073

74+
fi(A()) # Invalid
75+
fi(B()) # Invalid
76+
fi(C()) # Valid
77+
fi(D()) # Invalid
6178

62-
here it is valid to call `f` on an instance of `C`, but invalid to call it with instances of `A`,
63-
`B` or `D`.
79+
here it is valid to call ``fi`` on an instance of ``C``, but invalid to call it with instances of
80+
``A``, ``B`` or ``D``.
6481

6582
Motivation
6683
==========

0 commit comments

Comments
 (0)
0