Skip to content

Commit 8b01594

Browse files
committed
Remove typing.Protocol from the public API (2.7)
1 parent 9157277 commit 8b01594

File tree

2 files changed

+35
-36
lines changed

2 files changed

+35
-36
lines changed

lib-typing/2.7/test_typing.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from typing import (
66
List, Dict, Set, Tuple, Pattern, Match, Any, Callable, Generic,
7-
AbstractGeneric, Protocol, Sized, Iterable, Iterator, Sequence,
7+
AbstractGeneric, _Protocol, Sized, Iterable, Iterator, Sequence,
88
AbstractSet, Mapping, BinaryIO, TextIO, SupportsInt, SupportsFloat,
99
SupportsAbs, Reversible, Undefined, AnyStr, annotations, builtinclass,
1010
cast, disjointclass, ducktype, forwardref, overload, typevar
@@ -438,7 +438,7 @@ def test_reversible(self):
438438
self.assertNotIsInstance('', Reversible)
439439

440440
def test_simple_protocol(self):
441-
class P(Protocol):
441+
class P(_Protocol):
442442
def f(self): pass
443443

444444
class A(object):
@@ -455,16 +455,16 @@ def g(self): pass
455455

456456
self.assertTrue(issubclass(A, P))
457457
self.assertFalse(issubclass(B, P))
458-
self.assertTrue(issubclass(P, Protocol))
459-
self.assertTrue(issubclass(Protocol, Protocol))
460-
self.assertTrue(issubclass(A, Protocol))
458+
self.assertTrue(issubclass(P, _Protocol))
459+
self.assertTrue(issubclass(_Protocol, _Protocol))
460+
self.assertTrue(issubclass(A, _Protocol))
461461

462462
def test_issubclass_of_protocol(self):
463463
class A(object): pass
464-
self.assertTrue(issubclass(A, Protocol))
464+
self.assertTrue(issubclass(A, _Protocol))
465465

466466
def test_protocol_with_two_attrs(self):
467-
class P(Protocol):
467+
class P(_Protocol):
468468
def __int__(self): pass
469469
x = 0
470470

@@ -486,9 +486,9 @@ class C(object):
486486
self.assertNotIsInstance(C(), P)
487487

488488
def test_protocol_inheritance(self):
489-
class P(Protocol):
489+
class P(_Protocol):
490490
def f(self): pass
491-
class PP(P, Protocol):
491+
class PP(P, _Protocol):
492492
def g(self): pass
493493

494494
class A(object):
@@ -508,18 +508,18 @@ def g(self): pass
508508
self.assertNotIsInstance(A(), PP)
509509
self.assertNotIsInstance(C(), PP)
510510

511-
class AA(Protocol):
511+
class AA(_Protocol):
512512
def f(self): return 1
513513
class BB(AA): pass
514514

515515
self.assertEqual(BB().f(), 1)
516516

517517
class CC(AA): pass
518-
# BB is not a protocol since it doesn't explicitly subclass Protocol.
518+
# BB is not a protocol since it doesn't explicitly subclass _Protocol.
519519
self.assertNotIsInstance(CC(), BB)
520520

521521
def test_builtin_class_and_protocol(self):
522-
class P(Protocol):
522+
class P(_Protocol):
523523
def __add__(self): pass
524524

525525
self.assertIsInstance('', P)
@@ -532,14 +532,14 @@ def __add__(self): pass
532532

533533
def test_generic_protocol(self):
534534
t = typevar('t')
535-
class P(Protocol[t]):
535+
class P(_Protocol[t]):
536536
x = 1
537537
class A(object):
538538
x = 2
539539
self.assertIsInstance(A(), P)
540540

541541
def test_indexing_in_protocol(self):
542-
class P(Protocol):
542+
class P(_Protocol):
543543
def __getitem__(self): pass
544544
class A(object):
545545
def __getitem__(self): pass
@@ -609,11 +609,11 @@ class C(A, B): pass
609609
self.assertNotIsInstance(B(), Iterator)
610610

611611
def test_multiple_protocol_inheritance(self):
612-
class P(Protocol):
612+
class P(_Protocol):
613613
x = 1
614-
class P2(Protocol):
614+
class P2(_Protocol):
615615
y = 1
616-
class P3(P, P2, Protocol): pass
616+
class P3(P, P2, _Protocol): pass
617617

618618
class A(object):
619619
x = 1
@@ -628,7 +628,7 @@ class C(object):
628628
self.assertNotIsInstance(C(), P3)
629629

630630
def test_protocol_docstrings(self):
631-
class P(Protocol):
631+
class P(_Protocol):
632632
u"""blah"""
633633
def f(self): pass
634634
class A(object):
@@ -769,15 +769,15 @@ def f(self): pass
769769
B()
770770

771771
def test_protocol_with_abstract_method(self):
772-
class A(Protocol):
772+
class A(_Protocol):
773773
@abstractmethod
774774
def f(self): pass
775775

776776
with self.assertRaises(TypeError):
777777
A() # No implementation for abstract method.
778778

779779
def test_protocol_inheritance_with_abstract_method(self):
780-
class A(Protocol):
780+
class A(_Protocol):
781781
@abstractmethod
782782
def f(self): pass
783783
class B(A):

lib-typing/2.7/typing.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
'Match',
2424
'NamedTuple',
2525
'Pattern',
26-
'Protocol',
2726
'Set',
2827
'Tuple',
2928
'Undefined',
@@ -33,7 +32,7 @@
3332
'forwardref',
3433
'overload',
3534
'typevar',
36-
# Protocols and abstract base classes
35+
# _Protocols and abstract base classes
3736
'Container',
3837
'Iterable',
3938
'Iterator',
@@ -92,17 +91,17 @@ class AbstractGenericMeta(ABCMeta):
9291

9392
def __new__(mcls, name, bases, namespace):
9493
cls = ABCMeta.__new__(mcls, name, bases, namespace)
95-
# 'Protocol' must be an explicit base class in order for a class to
94+
# '_Protocol' must be an explicit base class in order for a class to
9695
# be a protocol.
97-
cls._is_protocol = name == u'Protocol' or Protocol in bases
96+
cls._is_protocol = name == u'_Protocol' or _Protocol in bases
9897
return cls
9998

10099
def __getitem__(self, args):
101100
# Just ignore args; they are for compile-time checks only.
102101
return self
103102

104103

105-
class Protocol(object):
104+
class _Protocol(object):
106105
__metaclass__ = AbstractGenericMeta
107106
"""Base class for protocol classes."""
108107

@@ -112,7 +111,7 @@ def __subclasshook__(cls, c):
112111
# No structural checks since this isn't a protocol.
113112
return NotImplemented
114113

115-
if cls is Protocol:
114+
if cls is _Protocol:
116115
# Every class is a subclass of the empty protocol.
117116
return True
118117

@@ -126,10 +125,10 @@ def __subclasshook__(cls, c):
126125

127126
@classmethod
128127
def _get_protocol_attrs(cls):
129-
# Get all Protocol base classes.
128+
# Get all _Protocol base classes.
130129
protocol_bases = []
131130
for c in cls.__mro__:
132-
if getattr(c, '_is_protocol', False) and c.__name__ != 'Protocol':
131+
if getattr(c, '_is_protocol', False) and c.__name__ != '_Protocol':
133132
protocol_bases.append(c)
134133

135134
# Get attributes included in protocol.
@@ -393,42 +392,42 @@ def __hash__(self):
393392
VT = typevar('VT')
394393

395394

396-
class SupportsInt(Protocol):
395+
class SupportsInt(_Protocol):
397396
@abstractmethod
398397
def __int__(self): pass
399398

400399

401-
class SupportsFloat(Protocol):
400+
class SupportsFloat(_Protocol):
402401
@abstractmethod
403402
def __float__(self): pass
404403

405404

406-
class SupportsAbs(Protocol[T]):
405+
class SupportsAbs(_Protocol[T]):
407406
@abstractmethod
408407
def __abs__(self): pass
409408

410409

411-
class Reversible(Protocol[T]):
410+
class Reversible(_Protocol[T]):
412411
@abstractmethod
413412
def __reversed__(self): pass
414413

415414

416-
class Sized(Protocol):
415+
class Sized(_Protocol):
417416
@abstractmethod
418417
def __len__(self): pass
419418

420419

421-
class Container(Protocol[T]):
420+
class Container(_Protocol[T]):
422421
@abstractmethod
423422
def __contains__(self, x): pass
424423

425424

426-
class Iterable(Protocol[T]):
425+
class Iterable(_Protocol[T]):
427426
@abstractmethod
428427
def __iter__(self): pass
429428

430429

431-
class Iterator(Iterable[T], Protocol[T]):
430+
class Iterator(Iterable[T], _Protocol[T]):
432431
@abstractmethod
433432
def next(self): pass
434433

0 commit comments

Comments
 (0)