@@ -5206,6 +5206,99 @@ instantiated from the type::
5206
5206
.. versionadded :: 3.10
5207
5207
5208
5208
5209
+ .. _types-intersection :
5210
+
5211
+ Intersection Type
5212
+ -----------------
5213
+
5214
+ .. index ::
5215
+ object: Intersection
5216
+ pair: intersection; type
5217
+
5218
+ An intersection object holds the value of the ``& `` (bitwise and) operation on
5219
+ multiple :ref: `type objects <bltin-type-objects >`. These types are intended
5220
+ primarily for :term: `type annotations <annotation> `.
5221
+
5222
+ .. describe :: X & Y & ...
5223
+
5224
+ Defines an intersection object which holds types *X *, *Y *, and so forth. ``X & Y ``
5225
+ means both X and Y.
5226
+ For example, the following function expects an argument of type
5227
+ ``Resettable `` and ``Growable[str] ``::
5228
+
5229
+ class Resettable(Protocol):
5230
+ def reset() -> None: ...
5231
+
5232
+ class Growable(Protocol[T]):
5233
+ def grow(t: T) -> None: ...
5234
+
5235
+ def f(x: Resettable & Growable[str]) -> None:
5236
+ x.reset()
5237
+ x.grow("first")
5238
+
5239
+ .. describe :: intersection_object == other
5240
+
5241
+ Intersection objects can be tested for equality with other intersection objects. Details:
5242
+
5243
+ * Intersections of intersections are flattened::
5244
+
5245
+ (A & B) & C == A & B & C
5246
+
5247
+ * Redundant types are removed::
5248
+
5249
+ A & B & A == A & B
5250
+
5251
+ * When comparing intersections, the order is ignored::
5252
+
5253
+ A & B == B & A
5254
+
5255
+ .. describe :: isinstance(obj, intersection_object)
5256
+ .. describe :: issubclass(obj, intersection_object)
5257
+
5258
+ Calls to :func: `isinstance ` and :func: `issubclass ` are also supported with an
5259
+ intersection object::
5260
+
5261
+ >>> isinstance("", object & str)
5262
+ True
5263
+
5264
+ However, intersection objects containing :ref: `parameterized generics
5265
+ <types-genericalias>` cannot be used::
5266
+
5267
+ >>> isinstance (1 , int & list[int ])
5268
+ Traceback (most recent call last):
5269
+ File "<stdin>", line 1, in <module>
5270
+ TypeError: isinstance() argument 2 cannot contain a parameterized generic
5271
+
5272
+ The user-exposed type for the intersection object can be accessed from
5273
+ :data: `types.IntersectionType ` and used for :func: `isinstance ` checks. An object can be
5274
+ instantiated from the type::
5275
+
5276
+ >>> import types
5277
+ >>> isinstance(int & str, types.IntersectionType)
5278
+ True
5279
+ >>> types.IntersectionType(int, str)
5280
+ int & str
5281
+
5282
+ .. note ::
5283
+ The :meth: `__and__ ` method for type objects was added to support the syntax
5284
+ ``X & Y ``. If a metaclass implements :meth: `__and__ `, the Intersection may
5285
+ override it::
5286
+
5287
+ >>> class M(type):
5288
+ ... def __and__(self, other):
5289
+ ... return "Hello"
5290
+ ...
5291
+ >>> class C(metaclass=M):
5292
+ ... pass
5293
+ ...
5294
+ >>> C & int
5295
+ 'Hello'
5296
+ >>> int & C
5297
+ int & __main__.C
5298
+
5299
+ .. versionadded :: 3.13
5300
+
5301
+
5209
5302
.. _typesother :
5210
5303
5211
5304
Other Built-in Types
0 commit comments