Skip to content

Commit

Permalink
Replace past/future with compat (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
frmdstryr authored and MatthieuDartiailh committed Mar 4, 2018
1 parent f10ca8f commit 1479778
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 15 deletions.
3 changes: 1 addition & 2 deletions atom/atom.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
from contextlib import contextmanager

from types import FunctionType
from future.utils import with_metaclass
from past.builtins import basestring
from .compat import with_metaclass, basestring

from .catom import (
CAtom, Member, DefaultValue, PostGetAttr, PostSetAttr, Validate,
Expand Down
77 changes: 77 additions & 0 deletions atom/compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#------------------------------------------------------------------------------
# Copyright (c) 2013-2018, Nucleic Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#------------------------------------------------------------------------------
import sys

IS_PY3 = sys.version_info >= (3,)


# From future.utils
def with_metaclass(meta, *bases):
"""
Function from jinja2/_compat.py. License: BSD.
Use it like this::
class BaseForm(object):
pass
class FormType(type):
pass
class Form(with_metaclass(FormType, BaseForm)):
pass
This requires a bit of explanation: the basic idea is to make a
dummy metaclass for one level of class instantiation that replaces
itself with the actual metaclass. Because of internal type checks
we also need to make sure that we downgrade the custom metaclass
for one level to something closer to type (that's why __call__ and
__init__ comes back from type etc.).
This has the advantage over six.with_metaclass of not introducing
dummy classes into the final MRO.
"""
class MetaClass(meta):
__call__ = type.__call__
__init__ = type.__init__

def __new__(cls, name, this_bases, d):
if this_bases is None:
return type.__new__(cls, name, (), d)
return meta(name, bases, d)
return MetaClass('temporary_class', None, {})


if IS_PY3:
import builtins
bytes = builtins.bytes
int = builtins.int
str = builtins.str
long = int

# Alternatively this could be used
# From past.types.basestring
class BaseBaseString(type):
def __instancecheck__(cls, instance):
return isinstance(instance, (bytes, str))

def __subclasshook__(cls, thing):
raise NotImplemented

class basestring(with_metaclass(BaseBaseString)):
"""
A minimal backport of the Python 2 basestring type to Py3
"""


else:
from __builtin__ import bytes, basestring, int, long
str = unicode

__all__ = ('IS_PY3', 'with_metaclass', 'int', 'long', 'bytes', 'str',
'basestring')
4 changes: 2 additions & 2 deletions atom/dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#
# The full license is in the file COPYING.txt, distributed with this software.
#------------------------------------------------------------------------------
import sys
if sys.version_info >= (3,):
from .compat import IS_PY3
if IS_PY3:
from collections import MutableMapping
else:
from UserDict import DictMixin as MutableMapping
Expand Down
3 changes: 1 addition & 2 deletions atom/intenum.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
else:
import copy_reg as copyreg

from future.utils import with_metaclass
from past.builtins import basestring
from .compat import with_metaclass, basestring


# IntEnum is not defined until the metaclass creates it.
Expand Down
4 changes: 2 additions & 2 deletions atom/scalars.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#------------------------------------------------------------------------------
from .catom import Member, DefaultValue, Validate, SetAttr, DelAttr

from future.builtins import int
from .compat import long


class Value(Member):
Expand Down Expand Up @@ -115,7 +115,7 @@ class Long(Value):
"""
__slots__ = ()

def __init__(self, default=int(0), factory=None, strict=False):
def __init__(self, default=long(0), factory=None, strict=False):
super(Long, self).__init__(default, factory)
if strict:
self.set_validate_mode(Validate.Long, None)
Expand Down
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ def build_extensions(self):
url='https://github.com/nucleic/atom',
description='Memory efficient Python objects',
long_description=open('README.rst').read(),
requires=['future'],
install_requires=['future', 'setuptools'],
install_requires=['setuptools'],
packages=find_packages(exclude=['tests', 'tests.*']),
ext_modules=ext_modules,
cmdclass={'build_ext': BuildExt},
Expand Down
2 changes: 1 addition & 1 deletion tests/test_default_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"""
import pytest
from future.builtins import int
from atom.compat import int

from atom.api import (Atom, Value, Range, FloatRange, List, Dict, Typed,
ForwardTyped, Instance, ForwardInstance, Coerced,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_intenum.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from operator import div

import pytest
from future.utils import with_metaclass
from atom.compat import with_metaclass
from atom.intenum import IntEnum, _IntEnumMeta


Expand Down
6 changes: 3 additions & 3 deletions tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"""
import sys
import pytest
from future.builtins import int
from atom.compat import long

from atom.api import (CAtom, Atom, Value, Bool, Int, Long, Range, Float,
FloatRange, Bytes, Str, Unicode, Enum, Callable, Coerced,
Expand All @@ -52,10 +52,10 @@
[(Value(), ['a', 1, None], ['a', 1, None], []),
(Bool(), [True, False], [True, False], 'r'),
(Int(), [1], [1],
[1.0, int(1)] if sys.version_info < (3,) else [1.0]
[1.0, long(1)] if sys.version_info < (3,) else [1.0]
),
(Int(strict=False), [1, 1.0, int(1)], 3*[1], ['a']),
(Long(strict=True), [int(1)], [int(1)],
(Long(strict=True), [long(1)], [long(1)],
[1.0, 1] if sys.version_info < (3,) else [0.1]),
(Long(strict=False), [1, 1.0, int(1)], 3*[1], ['a']),
(Range(0, 2), [0, 2], [0, 2], [-1, 3]),
Expand Down

0 comments on commit 1479778

Please sign in to comment.