Skip to content

Commit

Permalink
Started refactoring package-structure to a proper pypi-package. Start…
Browse files Browse the repository at this point in the history
…ed moving general-purpose-stuff to util.py. Started moving config-stuff and monkeypatching-stuff to __init__.py.
  • Loading branch information
Stewori committed Dec 13, 2016
1 parent 11cf05b commit dee7202
Show file tree
Hide file tree
Showing 17 changed files with 398 additions and 353 deletions.
52 changes: 52 additions & 0 deletions pytypes/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'''
Created on 12.12.2016
@author: Stefan Richthofer
'''

from typing import Sequence, Union, Generic, GenericMeta, TupleMeta

checking_enabled = False
def set_checking_enabled(flag = True):
global checking_enabled
checking_enabled = flag
return checking_enabled

# This way we glue typechecking to activeness of the assert-statement by default,
# no matter what conditions it depends on (or will depend on, e.g. currently -O flag).
assert(set_checking_enabled())

python3_5_executable = 'python3' # Must be >= 3.5.0

# Search-path for stubfiles.
stub_path = []

# Directory to collect generated stubs. If None, tempfile.gettempdir() is used.
stub_gen_dir = None

# Monkeypatch Generic to circumvent type-erasure:
_Generic__new__ = Generic.__new__
def __Generic__new__(cls, *args, **kwds):
res = _Generic__new__(cls, args, kwds)
res.__gentype__ = cls
return res
Generic.__new__ = __Generic__new__

# Monkeypatch GenericMeta.__subclasscheck__ to work properly with Tuples:
_GenericMeta__subclasscheck__ = GenericMeta.__subclasscheck__
def __GenericMeta__subclasscheck__(self, cls):
if isinstance(cls, TupleMeta):
if _GenericMeta__subclasscheck__(self, Sequence[Union[cls.__tuple_params__]]):
return True
return _GenericMeta__subclasscheck__(self, cls)
GenericMeta.__subclasscheck__ = __GenericMeta__subclasscheck__


# Some exemplary overrides for this modules's global settings:

# Set custom Python3-executable like this:
#pytypes.python3_5_executable = '/data/workspace/linux/Python-3.5.2/python'

# Set custom directory to store generated stubfiles like this:
# Unlike in tmp-directory mode, these are kept over distinct runs.
#stub_gen_dir = '../py2_stubs'
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import sys, os, imp, inspect, numbers, typing
import typechecker as tpc
from typing import Any
if __name__ == '__main__':
sys.path.append(sys.path[0]+os.sep+'..')
from pytypes import util


silent = False
Expand Down Expand Up @@ -99,7 +102,7 @@ def _write_class(clss, lines, inc = 0):
def convert(in_file, out_file = None):
_print('in_file: '+in_file)
assert(os.path.isfile(in_file))
checksum = tpc._md5(in_file)
checksum = util._md5(in_file)
if out_file is None:
out_file = in_file+'2'
_print('out_file: '+out_file)
Expand Down
28 changes: 15 additions & 13 deletions src/test_typechecker.py → pytypes/test_typechecker.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
'''

import unittest, sys, os
if __name__ == '__main__':
sys.path.append(sys.path[0]+os.sep+'..')
maindir = os.path.dirname(sys.modules['__main__'].__file__)
if maindir == '':
sys.path.append('testhelpers')
else:
sys.path.append(maindir+os.sep+'testhelpers')
import typechecker
import typechecker, util
typechecker.check_override_at_class_definition_time = False
typechecker.check_override_at_runtime = True
from typechecker import typechecked, override, get_types, get_type_hints, deep_type, \
from typechecker import typechecked, override, get_types, get_type_hints, \
InputTypeError, ReturnTypeError, OverrideError, no_type_check
import typing; from typing import Tuple, List, Union, Any
from numbers import Real
Expand Down Expand Up @@ -500,19 +502,19 @@ def test_get_types(self):

def test_various(self):
self.assertEqual(get_type_hints(testfunc), {'a': int, 'c': str, 'b': Real, 'return': Tuple[int, Real]})
self.assertEqual(deep_type(('abc', [3, 'a', 7], 4.5)), Tuple[str, List[Union[int, str]], float])
self.assertEqual(util.deep_type(('abc', [3, 'a', 7], 4.5)), Tuple[str, List[Union[int, str]], float])
tc2 = testClass2('bbb')
self.assertEqual(typechecker.get_class_that_defined_method(tc2.testmeth2c), testClass2)
self.assertEqual(typechecker.get_class_that_defined_method(testClass2.testmeth2c), testClass2)
self.assertEqual(typechecker.get_class_that_defined_method(tc2.testmeth2b), testClass2)
self.assertEqual(typechecker.get_class_that_defined_method(testClass2.testmeth2b), testClass2)
self.assertEqual(typechecker.get_class_that_defined_method(tc2.testmeth3), testClass2)
self.assertEqual(typechecker.get_class_that_defined_method(testClass2.testmeth3), testClass2)
self.assertRaises(ValueError, lambda: typechecker.get_class_that_defined_method(testfunc))
self.assertEqual(util.get_class_that_defined_method(tc2.testmeth2c), testClass2)
self.assertEqual(util.get_class_that_defined_method(testClass2.testmeth2c), testClass2)
self.assertEqual(util.get_class_that_defined_method(tc2.testmeth2b), testClass2)
self.assertEqual(util.get_class_that_defined_method(testClass2.testmeth2b), testClass2)
self.assertEqual(util.get_class_that_defined_method(tc2.testmeth3), testClass2)
self.assertEqual(util.get_class_that_defined_method(testClass2.testmeth3), testClass2)
self.assertRaises(ValueError, lambda: util.get_class_that_defined_method(testfunc))
# old-style:
tc3 = testClass3()
self.assertEqual(typechecker.get_class_that_defined_method(tc3.testmeth), testClass3)
self.assertEqual(typechecker.get_class_that_defined_method(testClass3.testmeth), testClass3)
self.assertEqual(util.get_class_that_defined_method(tc3.testmeth), testClass3)
self.assertEqual(util.get_class_that_defined_method(testClass3.testmeth), testClass3)


class TestTypecheck_class(unittest.TestCase):
Expand Down Expand Up @@ -838,7 +840,7 @@ def test_get_types_py3(self):
def test_various_py3(self):
self.assertEqual(get_type_hints(testfunc),
{'a': int, 'c': str, 'b': Real, 'return': Tuple[int, Real]})
self.assertEqual(deep_type(('abc', [3, 'a', 7], 4.5)), Tuple[str, List[Union[int, str]], float])
self.assertEqual(util.deep_type(('abc', [3, 'a', 7], 4.5)), Tuple[str, List[Union[int, str]], float])


@unittest.skipUnless(sys.version_info.major >= 3 and sys.version_info.minor >= 5,
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit dee7202

Please sign in to comment.