Skip to content

Commit

Permalink
Remove typing.Protocol from the public API (2.7)
Browse files Browse the repository at this point in the history
  • Loading branch information
JukkaL committed Jan 25, 2015
1 parent 9157277 commit 8b01594
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 36 deletions.
40 changes: 20 additions & 20 deletions lib-typing/2.7/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

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

def test_simple_protocol(self):
class P(Protocol):
class P(_Protocol):
def f(self): pass

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

self.assertTrue(issubclass(A, P))
self.assertFalse(issubclass(B, P))
self.assertTrue(issubclass(P, Protocol))
self.assertTrue(issubclass(Protocol, Protocol))
self.assertTrue(issubclass(A, Protocol))
self.assertTrue(issubclass(P, _Protocol))
self.assertTrue(issubclass(_Protocol, _Protocol))
self.assertTrue(issubclass(A, _Protocol))

def test_issubclass_of_protocol(self):
class A(object): pass
self.assertTrue(issubclass(A, Protocol))
self.assertTrue(issubclass(A, _Protocol))

def test_protocol_with_two_attrs(self):
class P(Protocol):
class P(_Protocol):
def __int__(self): pass
x = 0

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

def test_protocol_inheritance(self):
class P(Protocol):
class P(_Protocol):
def f(self): pass
class PP(P, Protocol):
class PP(P, _Protocol):
def g(self): pass

class A(object):
Expand All @@ -508,18 +508,18 @@ def g(self): pass
self.assertNotIsInstance(A(), PP)
self.assertNotIsInstance(C(), PP)

class AA(Protocol):
class AA(_Protocol):
def f(self): return 1
class BB(AA): pass

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

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

def test_builtin_class_and_protocol(self):
class P(Protocol):
class P(_Protocol):
def __add__(self): pass

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

def test_generic_protocol(self):
t = typevar('t')
class P(Protocol[t]):
class P(_Protocol[t]):
x = 1
class A(object):
x = 2
self.assertIsInstance(A(), P)

def test_indexing_in_protocol(self):
class P(Protocol):
class P(_Protocol):
def __getitem__(self): pass
class A(object):
def __getitem__(self): pass
Expand Down Expand Up @@ -609,11 +609,11 @@ class C(A, B): pass
self.assertNotIsInstance(B(), Iterator)

def test_multiple_protocol_inheritance(self):
class P(Protocol):
class P(_Protocol):
x = 1
class P2(Protocol):
class P2(_Protocol):
y = 1
class P3(P, P2, Protocol): pass
class P3(P, P2, _Protocol): pass

class A(object):
x = 1
Expand All @@ -628,7 +628,7 @@ class C(object):
self.assertNotIsInstance(C(), P3)

def test_protocol_docstrings(self):
class P(Protocol):
class P(_Protocol):
u"""blah"""
def f(self): pass
class A(object):
Expand Down Expand Up @@ -769,15 +769,15 @@ def f(self): pass
B()

def test_protocol_with_abstract_method(self):
class A(Protocol):
class A(_Protocol):
@abstractmethod
def f(self): pass

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

def test_protocol_inheritance_with_abstract_method(self):
class A(Protocol):
class A(_Protocol):
@abstractmethod
def f(self): pass
class B(A):
Expand Down
31 changes: 15 additions & 16 deletions lib-typing/2.7/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
'Match',
'NamedTuple',
'Pattern',
'Protocol',
'Set',
'Tuple',
'Undefined',
Expand All @@ -33,7 +32,7 @@
'forwardref',
'overload',
'typevar',
# Protocols and abstract base classes
# _Protocols and abstract base classes
'Container',
'Iterable',
'Iterator',
Expand Down Expand Up @@ -92,17 +91,17 @@ class AbstractGenericMeta(ABCMeta):

def __new__(mcls, name, bases, namespace):
cls = ABCMeta.__new__(mcls, name, bases, namespace)
# 'Protocol' must be an explicit base class in order for a class to
# '_Protocol' must be an explicit base class in order for a class to
# be a protocol.
cls._is_protocol = name == u'Protocol' or Protocol in bases
cls._is_protocol = name == u'_Protocol' or _Protocol in bases
return cls

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


class Protocol(object):
class _Protocol(object):
__metaclass__ = AbstractGenericMeta
"""Base class for protocol classes."""

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

if cls is Protocol:
if cls is _Protocol:
# Every class is a subclass of the empty protocol.
return True

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

@classmethod
def _get_protocol_attrs(cls):
# Get all Protocol base classes.
# Get all _Protocol base classes.
protocol_bases = []
for c in cls.__mro__:
if getattr(c, '_is_protocol', False) and c.__name__ != 'Protocol':
if getattr(c, '_is_protocol', False) and c.__name__ != '_Protocol':
protocol_bases.append(c)

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


class SupportsInt(Protocol):
class SupportsInt(_Protocol):
@abstractmethod
def __int__(self): pass


class SupportsFloat(Protocol):
class SupportsFloat(_Protocol):
@abstractmethod
def __float__(self): pass


class SupportsAbs(Protocol[T]):
class SupportsAbs(_Protocol[T]):
@abstractmethod
def __abs__(self): pass


class Reversible(Protocol[T]):
class Reversible(_Protocol[T]):
@abstractmethod
def __reversed__(self): pass


class Sized(Protocol):
class Sized(_Protocol):
@abstractmethod
def __len__(self): pass


class Container(Protocol[T]):
class Container(_Protocol[T]):
@abstractmethod
def __contains__(self, x): pass


class Iterable(Protocol[T]):
class Iterable(_Protocol[T]):
@abstractmethod
def __iter__(self): pass


class Iterator(Iterable[T], Protocol[T]):
class Iterator(Iterable[T], _Protocol[T]):
@abstractmethod
def next(self): pass

Expand Down

0 comments on commit 8b01594

Please sign in to comment.