Skip to content
Merged
Prev Previous commit
Next Next commit
ClassVar must be defined before Any in Python2
  • Loading branch information
ilevkivskyi committed Sep 9, 2016
commit 69a45026d11516543bb7b5abff048b3633991f7f
126 changes: 63 additions & 63 deletions python2/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,69 @@ def _type_repr(obj):
return repr(obj)


class _ClassVar(object):
"""Special type construct to mark class variables.

An annotation wrapped in ClassVar indicates that a given
attribute is intended to be used as a class variable and
should not be set on instances of that class. Usage::

class Starship:
stats = {} # type: ClassVar[Dict[str, int]] # class variable
damage = 10 # type: int # instance variable

ClassVar accepts only types and cannot be further subscribed.

Note that ClassVar is not a class itself, and should not
be used with isinstance() or issubclass().
"""

__metaclass__ = TypingMeta

def __init__(self, tp=None, _root=False):
cls = type(self)
if _root:
self.__type__ = tp
else:
raise TypeError('Cannot initialize {}'.format(cls.__name__[1:]))

def __getitem__(self, item):
cls = type(self)
if self.__type__ is None:
return cls(_type_check(item,
'{} accepts only types.'.format(cls.__name__[1:])),
_root=True)
raise TypeError('{} cannot be further subscripted'
.format(cls.__name__[1:]))

def _eval_type(self, globalns, localns):
return type(self)(_eval_type(self.__type__, globalns, localns),
_root=True)

def _get_type_vars(self, tvars):
if self.__type__:
_get_type_vars(self.__type__, tvars)

def __repr__(self):
cls = type(self)
if not self.__type__:
return '{}.{}'.format(cls.__module__, cls.__name__[1:])
return '{}.{}[{}]'.format(cls.__module__, cls.__name__[1:],
_type_repr(self.__type__))

def __hash__(self):
return hash((type(self).__name__, self.__type__))

def __eq__(self, other):
if not isinstance(other, _ClassVar):
return NotImplemented
if self.__type__ is not None:
return self.__type__ == other.__type__
return self is other

ClassVar = _ClassVar(_root=True)


class AnyMeta(TypingMeta):
"""Metaclass for Any."""

Expand Down Expand Up @@ -1133,69 +1196,6 @@ def __new__(cls, *args, **kwds):
return obj


class _ClassVar(object):
"""Special type construct to mark class variables.

An annotation wrapped in ClassVar indicates that a given
attribute is intended to be used as a class variable and
should not be set on instances of that class. Usage::

class Starship:
stats = {} # type: ClassVar[Dict[str, int]] # class variable
damage = 10 # type: int # instance variable

ClassVar accepts only types and cannot be further subscribed.

Note that ClassVar is not a class itself, and should not
be used with isinstance() or issubclass().
"""

__metaclass__ = TypingMeta

def __init__(self, tp=None, _root=False):
cls = type(self)
if _root:
self.__type__ = tp
else:
raise TypeError('Cannot initialize {}'.format(cls.__name__[1:]))

def __getitem__(self, item):
cls = type(self)
if self.__type__ is None:
return cls(_type_check(item,
'{} accepts only types.'.format(cls.__name__[1:])),
_root=True)
raise TypeError('{} cannot be further subscripted'
.format(cls.__name__[1:]))

def _eval_type(self, globalns, localns):
return type(self)(_eval_type(self.__type__, globalns, localns),
_root=True)

def _get_type_vars(self, tvars):
if self.__type__:
_get_type_vars(self.__type__, tvars)

def __repr__(self):
cls = type(self)
if not self.__type__:
return '{}.{}'.format(cls.__module__, cls.__name__[1:])
return '{}.{}[{}]'.format(cls.__module__, cls.__name__[1:],
_type_repr(self.__type__))

def __hash__(self):
return hash((type(self).__name__, self.__type__))

def __eq__(self, other):
if not isinstance(other, _ClassVar):
return NotImplemented
if self.__type__ is not None:
return self.__type__ == other.__type__
return self is other

ClassVar = _ClassVar(_root=True)


def cast(typ, val):
"""Cast a value to a type.

Expand Down